tiredofit
Well-known Member
- Joined
- Apr 11, 2013
- Messages
- 1,834
- Office Version
- 365
- 2019
- Platform
- Windows
This is in Module1:
I understand why the keyword ByVal is required in Sub Second because SheetArrayElement is declared as a Variant, yet the argument wks in Sub Second expects a worksheet.
However, why is it when I try the same using a class, I don't have to make this distinction?
This is in Module2:
This is in ClsSate:
Is it because Sub State is not passed any arg?
Code:
Option Explicit
Sub WithoutClass()
Dim SheetArray() As Variant
SheetArray = Array(Sheet1, Sheet2)
Dim SheetArrayElement As Variant
For Each SheetArrayElement In SheetArray
Call Second(SheetArrayElement)
Next SheetArrayElement
End Sub
Sub Second(ByVal wks As Worksheet)
Select Case wks.Name
Case Sheet1.CodeName
MsgBox "Hi!"
Case Sheet2.CodeName
MsgBox "Hello!"
End Select
End Sub
I understand why the keyword ByVal is required in Sub Second because SheetArrayElement is declared as a Variant, yet the argument wks in Sub Second expects a worksheet.
However, why is it when I try the same using a class, I don't have to make this distinction?
This is in Module2:
Code:
Sub WithClass()
Dim SheetArray() As Variant
SheetArray = Array(Sheet1, Sheet2)
Dim SheetsArrayElement As Variant
Dim MyState As ClsState
For Each SheetsArrayElement In SheetArray
Set MyState = New ClsState
Set MyState.wks = SheetsArrayElement
Call MyState.State
Set MyState = Nothing
Next SheetsArrayElement
End Sub
This is in ClsSate:
Code:
Option Explicit
Private pwks As Worksheet
Public Property Get wks() As Worksheet
Set wks = pwks
End Property
Public Property Set wks(ByVal ws As Worksheet)
Set pwks = ws
End Property
Sub State()
Select Case wks.CodeName
Case Sheet1.CodeName
MsgBox "Hi!"
Case Sheet2.CodeName
MsgBox "Hello!"
End Select
End Sub
Is it because Sub State is not passed any arg?
Last edited: