I'm trying to refactor some Excel VBA code (Excel 2016, VBA 7.1) to eliminate repetitive subroutines for maintainability purposes. Several of the subroutines only differ by the group of global constants they use, so what I'm basically trying to do is group the globals together into a struct-like data structure so I can pass them into a common function as an argument.
Note that the groups of global constants have some in common, but not all, e.g.:
Global Const GROUP1_SHEET As String = "Sheet1"
Global Const GROUP1_FIRST_ROW As Long = 2
Global Const GROUP1_LAST_COL As Long = 15
Global Const GROUP1_SOME_COL_OFFSET = 4
Global Const GROUP2_SHEET As String = "Sheet2"
Global Const GROUP2_FIRST_ROW As Long = 2
Global Const GROUP2_LAST_COL As Long = 8
Global Const GROUP2_ANOTHER_COL_OFFSET = 2
And there are different subroutines for each group, e.g.:
In Sheet1:
Private Sub DoSomething()
Set w = Worksheets(GROUP1_SHEET)
'some code
End Sub
In Sheet2:
Private Sub DoSomething()
Set w = Worksheets(GROUP2_SHEET)
'same code as above
End Sub
There are dozens of these. Needless to say, this code is a nightmare to even read through, let alone maintain.
What I'm trying to do right now is split the groups into separate modules and set them as properties, similar to what is described in this question. The problem with this is I don't know how to pass the module (i.e. the group of globals) to the function as an argument.
In new module GROUP1:
Public Property Get SHEET() As String
SHEET = "Sheet1"
End Property
And this works as I want it to:
Public Sub ShowPopup()
MsgBox GROUP1.SHEET
End Sub
But passing it as an argument does not:
Public Sub Popup(inModule As Object)
MsgBox inModule.SHEET
End Sub
Public Sub ShowPopUp()
Popup GROUP1
End Sub
Nothing I've tried works in place of "Object" in the example above. I either get "ByRef Argument type mismatch" or "Expected variable or procedure, not module" depending on what I put there.
So, can I pass a module like this (maybe as a string and evaluate it somehow?), or should I use some other way of grouping globals?