我面对一个我不理解的有趣问题。
在VBA中,我想在用户双击单元格时加载Userform,具体取决于单元格内容。但是根据我要求的条件,Excel退出Sub后会崩溃(即使在UserForm关闭后它也会继续执行代码)。
这有效:
Private Sub Workbook_SheetBeforeDoubleClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)
Dim myForm As New UserForm1
If Target.Cells(1).Value = "" Then
myForm.Show
End If
Debug.Print ("OK")
End Sub
这三个让Excel崩溃:
1
Private Sub Workbook_SheetBeforeDoubleClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)
Dim myForm As New UserForm1
If Target.Cells(1).Value <> "" Then
myForm.Show
End If
Debug.Print ("OK")
End Sub
2
Private Sub Workbook_SheetBeforeDoubleClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)
Dim myForm As New UserForm1
If Not Target.Cells(1).Value = "" Then
myForm.Show
End If
Debug.Print ("OK")
End Sub
3
Private Sub Workbook_SheetBeforeDoubleClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)
Dim myForm As New UserForm1
If Target.Cells(1).Value = "Test" Then
myForm.Show
End If
Debug.Print ("OK")
End Sub
我使用空的UserForm进行测试
答案 0 :(得分:3)
尝试更改If块中的代码,使其Cancel = True
忽略双击操作,并预先加载表单,如下所示:
If Target.Cells(1).Value = "Test" Then
Cancel = True
Load myForm
myForm.Show
End If