我有一个问题,我找到了答案,但它说"这个代码需要在类模块中发布,并且需要在它工作之前创建类的实例"。 我非常熟悉"模块"但之前从未使用过类模块,似乎无法弄清楚如何运行这个东西。下面是我正在使用的代码,以及我随机尝试运行它的一些代码。有这个(A)和(WorkbookBeforeClose),以及" ThisWorkbook"另一个(BeforeClose),让我感到困惑。
代码的使用;我有一个userform,在显示时,根据其他工作簿是否打开来隐藏窗口/应用程序,这个代码是在关闭其他书籍时尝试关闭的。
非常感谢您的帮助
代码:
Public WithEvents A As Excel.Application
Private Sub A_WorkbookBeforeClose(ByVal Wb As Workbook, Cancel As Boolean)
Dim VIS As Boolean, myAW As Workbook
If Workbooks.Count > 1 Then 'if there is more than one workbook open...
If Windows(ThisWorkbook.Name).Visible = False Then 'and if Client is invisible...
If ActiveWorkbook.Name = ThisWorkbook.Name Then 'and if the workbook being closed is the Client.
Windows(ThisWorkbook.Name).Visible = True
Else 'more than one wb open, Client is invisible, and the workbook being closed is NOT the Client.
Set myAW = ActiveWorkbook
Cancel = True
Windows(ThisWorkbook.Name).Visible = True
Application.EnableEvents = False
myAW.Close
Application.EnableEvents = True
If ServiceEntry.ExcelB.Tag = "False" Then 'a tag on the button on the UserForm to store whether the workbook should be hidden or not
If Workbooks.Count > 1 Then
Windows(ThisWorkbook.Name).Visible = False
Else
ThisWorkbook.Application.Visible = False
End If
End If
Exit Sub
End If
ElseIf ActiveWorkbook.Name <> ThisWorkbook.Name Then
'more than one workbook open and the Client is visible and the workbook being closed is NOT the Client
Exit Sub
End If
End If
End Sub
答案 0 :(得分:0)
要创建对象,您只需执行以下操作:
Dim C As Class1 'where Class1 is your module class name
Set C = New Class1
您可以在初始化userForm时执行此操作:
Private Sub userForm_Initialize()
Dim C As Class1
Set C = New Class1
End Sub