I've been working on some Excel VBA code with the hope of making it expandable, maintainable and easy to read. Some variables in the code require unique hardcoded ranges while others store common/repeated information. I moved the common/repeat code to a global module to reduce the number of times it needs to be defined with the same information, but I'm concerned it will make it too difficult to track the code or leave the code open to errors later by having public variables. Is this a good way to go about coding this or is there a more efficient/user friendly way to make it OOP?
Snippet below.
Module: mCommon
Module: mUI
Snippet below.
Module: mCommon
VBA Code:
Option Explicit
Public wrkshtInput As Object
Public rngPartSize As Range
Public rngPart2Size As Range
Sub CommonDefinitions()
Set wrkshtInput = Worksheets("INPUT (BOM)")
Set rngPartSize = Range("C5")
Set rngPartSize2 = Range("C6")
End Sub
Module: mUI
VBA Code:
Option Explicit
Sub PartToggle()
'OBJECT REF(S): Sheet2 (INPUT (BOM))
'METHOD REF(S): mCommon.CommonDefinitions
'VARIABLE REF(S): mCommon.wrkshtInput, mCommon.rngPartSize
'COMMON VARIABLE DEFINITIONS:
Call mCommon.CommonDefinitions
'DEFINE VARIABLES:
Set rngBlueACM = Range("H129:H134, H136:H141, H144:H147")
Set rngRedACM = Range("H149:H154, H156:H161, H164:H167")
'PART TOGGLE: ON
If mCommon.wrkshtInput.tglbtnPartToggle.Value = True Then
mCommon.rngPartSize.Value = ""
rngBlueACM.Value = "MANUAL"
rngRedACM.Value = "MANUAL"
End If
'PART TOGGLE: OFF
If mCommon.wrkshtInput.tglbtnPartToggle.Value = False Then
mCommon.rngPartSize.Value = "--"
rngBlueACM.Value = "--"
rngRedACM.Value = "--"
End If
End Sub
Sub Part2Toggle()
'OBJECT REF(S): Sheet2 (INPUT (BOM))
'METHOD REF(S): mCommon.CommonDefinitions
'VARIABLE REF(S): mCommon.wrkshtInput, mCommon.rngPart2Size
'COMMON VARIABLE DEFINITIONS:
Call mCommon.CommonDefinitions
'DEFINE VARIABLES
Set rngWhiteACM = Range("H107:H108")
'PART2 TOGGLE: ON
If mCommon.wrkshtInput.tglbtnPart2Toggle.Value = True Then
mCommon.rngPart2Size.Value = ""
rngWhiteACM.Value = "MANUAL"
End If
'PART2 TOGGLE: OFF
If mCommon.wrkshtInput.tglbtnPart2Toggle.Value = False Then
mCommon.rngPart2Size.Value = "--"
rngWhiteACM.Value = "--"
End If
End Sub