Hi
I am trying to declare and set worksheet and dynamic range variables globally. I have several worksheets and forms in a workbook and I have found it difficult to keep track of where all my variables are stored so that when I make any amendments to the location of the worksheet or range, they are done throughout any coding from 1 central variable declaration point.
Specically I have 3 main worksheets where the name of those worksheet should never change but they could be in future in future development. In each sub and form that references those worksheets I am constantly starting the code within the sub or form code to be
Dim wksMain As Worksheet, wksLists As Worksheet, wksWorkings As Worksheet
Set wksMain = Sheets("Main")
Set wksLists = Sheets("wksLists")
Set wksWorkings = Sheet("Workings")
In some forms or sub procedures, I have some variables for dynamic ranges that are set when the workbook is opened or changed when a form updates specific cells. These variables are again referenced in several sub and form procedures.
Dim lngCalcLastRow As Long
Dim rngCalc As Range
lngCalcLastRow = wksMain.Range("B" & wksMain.Rows.Count).End(xlUp).Row 'Find the last row with data in column B
Set rngCalc = wksMain.Range("B3:B" & lngCalcLastRow) 'creates the range with data from B3 to the last cell in Column B with data in it
What is the preferred and robust way of declaring these variables so that I can:
a) declare and set the objects in a central way
b) be able to refer to them from any sub procedure of form procedure in the workbook
c) Make it easier for me to keep track of them so if any of these variable objects need to change, I can update 1 place and not several.
Any help will be much appreciated
I am trying to declare and set worksheet and dynamic range variables globally. I have several worksheets and forms in a workbook and I have found it difficult to keep track of where all my variables are stored so that when I make any amendments to the location of the worksheet or range, they are done throughout any coding from 1 central variable declaration point.
Specically I have 3 main worksheets where the name of those worksheet should never change but they could be in future in future development. In each sub and form that references those worksheets I am constantly starting the code within the sub or form code to be
Dim wksMain As Worksheet, wksLists As Worksheet, wksWorkings As Worksheet
Set wksMain = Sheets("Main")
Set wksLists = Sheets("wksLists")
Set wksWorkings = Sheet("Workings")
In some forms or sub procedures, I have some variables for dynamic ranges that are set when the workbook is opened or changed when a form updates specific cells. These variables are again referenced in several sub and form procedures.
Dim lngCalcLastRow As Long
Dim rngCalc As Range
lngCalcLastRow = wksMain.Range("B" & wksMain.Rows.Count).End(xlUp).Row 'Find the last row with data in column B
Set rngCalc = wksMain.Range("B3:B" & lngCalcLastRow) 'creates the range with data from B3 to the last cell in Column B with data in it
What is the preferred and robust way of declaring these variables so that I can:
a) declare and set the objects in a central way
b) be able to refer to them from any sub procedure of form procedure in the workbook
c) Make it easier for me to keep track of them so if any of these variable objects need to change, I can update 1 place and not several.
Any help will be much appreciated