Hello,
I am going through some macros, and I have noticed that some variables are declared at the beginning of a subroutine and used later in the routine (i.e. they are declared at the beginning, but might not be used until the end of the subroutine). Other variables, however, are declared immediately before being used. Examples are below:
'declare variables at the beginning of the subroutine, but used at the end of the subroutine
Dim questionanswerws As Worksheet
Dim questionmapWS As Worksheet
Dim demomapws As Worksheet
Dim respondentint As Integer
Dim responseint As Integer
Dim responseWS As Worksheet
Dim respondentWS As Worksheet
'declared the variables and used immediately
Dim ws As Worksheet
For Each ws In Worksheets
ws.Range("A1:z1").EntireColumn.AutoFit
ws.AutoFilterMode = False
Next ws
My question is: does it matter where in the subroutine these are declared? Would there ever be a reason why you would not declare all variables at the beginning of the subroutine?
Thank you!
Bookmarks