开发者

VBA crash when changing worksheet from dropdown

开发者 https://www.devze.com 2023-02-03 00:14 出处:网络
I have a dropdown with an associated macro, which looks like: Sub Drop() If Range(\"Hidden1!A1\") = \"1\" Then

I have a dropdown with an associated macro, which looks like:

Sub Drop()
     If Range("Hidden1!A1") = "1" Then
          Sheets("Sheet1").Select
     End If
     If Range("Hidden1!A1") = "2" Then
          Sheets("Sheet2").Select
     End If
     If Range("Hidden1!A1") = "3" Then
          Sheets("Sheet3").Select
     End If
End Sub

This makes m开发者_如何学运维y Excel 2010 completely crash and want to send a report to Microsoft. Any ides of how to rewrite this so it does not crash, or is it an Excel bug?


sandos,

I was able to reproduce the problem as you described. Adding a DoEvents command at the beginning of the sub fixed it. It's also always a good idea to qualify your sheet names and ranges with the workbook they belong to, like ThisWorkbook.Sheets(1).Range("A1"). Also, you don't really need all those If Endif statements (if you did you'd want one long If, ElseIf, Elseif, End). Anyways you can ignore these comments and just put DoEvents at the beginning and it should work:

Sub Drop()
DoEvents
With ThisWorkbook
    .Worksheets("Sheet" & .Worksheets("Hidden1").Range("A1")).Select
End With
End Sub
0

精彩评论

暂无评论...
验证码 换一张
取 消