gpt4 book ai didi

vba - 用户表单根据条件使 Excel 崩溃

转载 作者:行者123 更新时间:2023-12-02 16:35:01 24 4
gpt4 key购买 nike

我面临一个我不明白的有趣问题。

在 VBA 中,我想在用户双击单元格时加载用户窗体,具体取决于单元格内容。但根据我要求的条件,一旦我退出 Sub,Excel 就会崩溃(即使在用户窗体关闭后,它仍会继续执行代码)。

这有效:

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

我使用空的用户表单进行测试

最佳答案

尝试更改 If block 中的代码,以便执行 Cancel = True 来忽略双击操作并预先加载表单,如下所示:

If Target.Cells(1).Value = "Test" Then
Cancel = True
Load myForm
myForm.Show
End If

关于vba - 用户表单根据条件使 Excel 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49556764/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com