I have a question about naming conventions and using the same variable name in different subs. It's been bugging me since I started with VBA.
count_ws
):Module 1:
Sub Test()
Dim count_ws As Long
For count_ws = 2 To ThisWorkbook.Worksheets.Count
Debug.Print "Module1"
Next count_ws
End Sub
Module 2:
Sub Test2()
Dim count_ws As Long
For count_ws = 2 To ThisWorkbook.Worksheets.Count
Debug.Print "Module2"
Next count_ws
End Sub
If this is not ok, what's the best alternative? The reason I repeat the name is that I didn't want to make the variable name too long, like count_ws_module1
and count_ws_module2
Passing a variable into another sub: Same question, is it advisable to keep the same name? I feel like it could be confusing if I call the variable one name in the first sub, and then something else in the other sub.
Sub Test3() Dim wsLoans As Worksheet Dim wsBS As Worksheet
Set wsLoans = ThisWorkbook.Sheets(2)
Set wsBS = ThisWorkbook.Sheets(3)
Call Test4(wsLoans)
End Sub
Sub Test4(ByVal wsLoans As Worksheet)
wsLoans.Range("A1").Value = "Module 4"
End Sub
So, for me this approach seems the most readable and avoids confusion, but I'm happy to hear other opinions. In Sub Test4
I could simply name the Sheet ws
. Or wsLoans_Test4
, but does this actually help?
I just want to make sure I get this right and build good habits.
This is a question that can lead to endless discussions, and the more you think about the whole subject of naming conventions, the deeper it goes. And as this is strongly opinion bases, those kind of questions are usually closed on Stack Overflow.
I will quickly list 3 aspects:
(1) A subroutine (or function) can be seen as an closed object, often seen as a black box. It should do a defined task, however, how this is done shouldn't matter. It could be stored in a different module and could be written by a different person. You shouldn't have to ask someone "Have you already used the variable name count_ws
- if not, I want to reserve it for me. Every routine should use whatever name it likes.
(2) You as a programmer should have some naming conventions. They don't need to be written down, but you should have a specific consistency. Do you name a sheet variable wsData
or dataWs
, do you use camelCase, PascalCase or snake_case, use Hungarion Notation or not... As a consequence, you will probably name variables identically in different routines when they serve a identical or similar purpose - and why not. Again, you shouldn't have to look into your code if you used the same name already. Exception is if you are dealing within the same routine, don't use the same variable for different purposes, and be careful when naming iteration variables in nested loops.
(3) Function parameter names serves as a documentation. The parameters are the interface between two routines and if you give them good names, it gets easier to figure out what's the purpose of it. If you want to call a routine Copy
that receives 2 parameters which are named p1
and p2
, you first have to figure out what is source and what is destination, while pFrom
and pTo
makes it obvious. That said, if you are happy with your naming, there is no reason not to name a variable of a calling routine like the parameter name of a subroutine.