【发布时间】:2018-03-29 13:01:10
【问题描述】:
我遇到了一个我不明白的有趣问题。
在 VBA 中,我想在用户双击单元格时加载用户窗体,具体取决于单元格内容。但是根据我问的情况,一旦我退出 Sub,Excel 就会崩溃(即使在 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
我正在使用一个空的用户窗体进行测试
【问题讨论】:
-
评论不用于扩展讨论;这个对话是moved to chat。