1

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.

  1. Is it good practice to use the same variable name in different modules? The subs don't interact with each other. For example, I cycle through the sheets in my workbook in two different modules and so far have used the same name for the counter variable (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

  1. 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.

4
  • 1
    It is perfectly fine to reuse variable names in different subroutines. Just don't reuse the names of variables at a higher scope. Commented Jun 30, 2020 at 7:50
  • 1
    In my opinion, it is better to name count_ws as worksheets_count or just count. I feel that a variable's name should read easily and clearly. Commented Jun 30, 2020 at 7:52
  • It's all to do with the scope of the variables. support.microsoft.com/en-gb/help/141693/… Using the same name in multiple macro's functions that do not interact is perfectly fine and getting good name conventions can help you decipher the code when you come back to it 6+ months later. It can also help with reusability of the code as you can copy and paste between modules and it will require less editing (assuming you can reuse the variables in this way) Commented Jun 30, 2020 at 9:16
  • @Enigmativity Yeah, worksheets_countmakes it clearer. I struggle sometimes with the naming and am not sure if I'm opting for the best choice. I shouldn't shy away from longer variable names if they make the code more readable, right? I have looked up naming conventions and the different ways to go about it, but when writing code, there are always cases where I end up with a dodgy name. Commented Jul 1, 2020 at 8:10

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.