Capitalisation Changes

Also referred to as Automatic Case-Correction.


Not Case Sensitive

The VBA compiler is not case sensitive so there is no distinction between variable names written in lowercase, mixedcase or uppercase.
To see this in action create the following subroutine and declare each of these variables one line at a time.
Every time you type in a new declaration with different lowercase and uppercase characters all the names get changed.

Public Sub MySub() 

Dim myvariable As String
Dim myVariable As String
Dim MyVariable As String
Dim MYVARIABLE As String
Dim myvariable As String

End Sub

More Realistic Example

Lets assume that you have multiple declarations of the same variable name in your code.
The last declaration that has been added or edited will determine the case of ALL the variables that have the same name.
To see this in action modify the subroutine to only include a lowercase declaration.

Public Sub MySub() 
Dim myvariable As String
End Sub

Then add a simple function declaration underneath making sure that this parameter/variable name is entered in uppercase.

Public Function MyFunction(ByVal MYVARIABLE As String) 
End Function

You will see that the case of the variable declaration in the subroutine will get changed to uppercase.


Affects the Whole Project

This automatic changing of the case is completely independent of scope and context.
This case-correction will happen inside modules, classes and userforms.
This time add the following subroutine to Module1 using a lowercase variable name.

Public Sub MySub() 
Dim myvariable As String
End Sub

Then add the following function declaration to Module2 using an uppercase parameter/variable name.

Public Function MyFunction(ByVal MYVARIABLE As String) 
End Function

You will see that when you go back to Module1, the case of this variable has changed.


Consistent Naming Convention

If you are inconsistent when declaring your variables (or parameters) the Visual Basic Editor will automatically change the capitalisation.
The best and permanent solution is to standardise the case across the whole project.
If you have multiple developers working on the same project you will need to agree a clear naming convention.
A popular convention is to always prefix local variables with an underscore and always declare constants in all uppercase.


Very Frustrating

You can be constantly reminded of this feature if you have large VBA Projects with lots of code being worked on by several developers.
Different developers always have different styles and naming conventions when it comes to uppercase and lowercase characters in variable names.
When you have a small amount of code you can work around this feature by using a simple naming convention.
If you have a large amount of code changing your code to be consistent will require some effort.
If you are using a source control system and cannot introduce consistency then this 'feature' is very frustrating.
Most source control system are case sensitive and will consider everything to be a 'code change' even when the capitalisation of a variable changes.
This will pollute the revision history and make it harder to spot the 'real code changes'.


© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext