今天我一直在遇到特定的VBA代码行。我不断收到关于“对象变量未设置......”这是VBA错误的错误消息91Excel VBA对象变量未设置错误91
下面的代码是发生错误,位于模块文件夹mFactory
Public Function CreateInterface(InterfaceWB As Workbook, SourceFilepath As String, SourceBookPass As String) As CInterface
Dim NewInterface As CInterface
Set NewInterface = New CInterface
NewInterface.InitiateProperties InterfaceWB:=InterfaceWB, SourceFilepath:=SourceFilepath, SourceBookPass:=SourceBookPass <------Error Here
Set CreateInterface = NewInterface
End Function
这方法被调用的工作簿开放的ThisWorkbook:
Public Interface As CInterface
Private Sub Workbook_Open()
Dim InterfaceWB As Workbook
Dim SourceFilepath As String
Dim SourceBookPass As String
Set InterfaceWB = ThisWorkbook
'Change this variable if the location of the source workbook is changed
SourceFilepath = "C:\file.xlsx"
'Change this variable if the workbook password is changed
SourceBookPass = "password"
Set Interface = mFactory.CreateInterface(InterfaceWB, SourceFilepath, SourceBookPass)
End Sub
而且在类模块CInterface实现称为mFactory模块中InitiateProperties方法:
Sub InitiateProperties(InterfaceWB As Workbook, SourceFilepath As String, SourceBookPass As String)
pInterfaceWB = InterfaceWB
pSourceFilepath = SourceFilepath
pSourceBookPass = SourceBookPass
End Sub
我的VBAProject结构如下:
VBAProject
Microsoft Excel Objects
Sheet1
ThisWorkbook
Modules
mFactory
Class Modules
CInterface
有一件事我所做的是改变CInterface实例化为PublicNotCreatable,因为我得到关于传递专用参数公共职能的错误。我试图使用“构造函数”来创建一个CInterface类的实例,以在VBA项目中全局使用。为什么对象没有设置在行我得到错误?
'mFactory'是不是对象。已经尝试过'Set Interface = CreateInterface(InterfaceWB,SourceFilepath,SourceBookPass)'? – PatricK
'mFactory'是同一个VBAProject中的一个模块。在这里,我正在调用模块的方法。对不起,如果我的行话是不正确的VBA,我不是很熟悉这种语言。 –