I have the following vba class module called clsAgent
:
Option Explicit
Private pAgentSheetName As String
Private pAgentSheet As Worksheet
Public Property Get AgentSheetName() As String
AgentSheetName = pAgentSheetName
End Property
Public Property Let AgentSheetName(AgentSheetName As String)
pAgentSheetName = AgentSheetName
End Property
Public Property Get AgentSheet() As Worksheet
AgentSheet = pAgentSheet
End Property
Public Property Let AgentSheet(AgentSheet As Worksheet)
pAgentSheet = AgentSheet
End Property
Here's my class instantiation in a different module:
Sub test_agent_class()
Dim agent1 As clsAgent
Set agent1 = New clsAgent
Set agent1.AgentSheet = Worksheets(agent1.AgentSheetName)
Debug.Print agent1.AgentSheet.Name
End Sub
When I run test_agent_class()
, however, I get an error: run-time error 9
subscript out of range
. The following line gets highlighted in yellow:Set agent1.AgentSheet = Worksheets(agent1.AgentSheetName)
.
I read this answer, but I don't get what the problem is because agent1.AgentSheetName
is a string.
What am I doing wrong?
EDIT
Per the first answer I added the line:
agent1.AgentSheetName = "agentsFullOutput.csv"
right after the class in instantiated. Now I get an error:
Run-time error '91': Object variable or With block variable not set.