I've been having trouble with a specific line of VBA code today. I keep getting error messages about "Object variable not set..." which is VBA error 91.
The following code is where the error occurs and is located in the modules folder as 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
This method is called on workbook open from 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
And the InitiateProperties method called in the mFactory module is implemented in the class module CInterface:
Sub InitiateProperties(InterfaceWB As Workbook, SourceFilepath As String, SourceBookPass As String)
pInterfaceWB = InterfaceWB
pSourceFilepath = SourceFilepath
pSourceBookPass = SourceBookPass
End Sub
My VBAProject structure is as follows:
VBAProject
Microsoft Excel Objects
Sheet1
ThisWorkbook
Modules
mFactory
Class Modules
CInterface
One thing I did is change the CInterface instancing to PublicNotCreatable because I was getting an error about passing private arguments to public functions. I'm trying to use a "constructor" to create one instance of the CInterface class to use globally in the VBA project. Why is the object not set at the line I'm getting the error?
mFactoryis not an Object. Have your triedSet Interface = CreateInterface(InterfaceWB, SourceFilepath, SourceBookPass)?mFactoryis a module in the same VBAProject. Here I'm making a method call to the module. Sorry if my jargon is incorrect for VBA, I'm not very familiar with this language.