Variable declaration case

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,834
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I thought VBA was NOT case sensitive.

Try this:

In Module1, type:

Code:
Sub First()

    Dim b As Integer

End Sub

In Module2, type:

Code:
Sub Second()

    Dim B As Integer

End Sub

For some reason, the variable b is changed in BOTH modules to be either upper or lower case.

Is that a bug?
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
More like a "quirk", not a defect per se. In other words, it is by design.

As you say, VBA variable names are not case-sensitive. So VBA chooses the last spelling (mix of cases) that is entered. If you return to Module1 and re-enter "Dim b", all instances of "B" will be changed to "b".

I don't like this behavior any more than you do. But that's the way it is. Sigh.

PS.... I did not know that this behavior operated across modules. That is a shock, especially for procedure-local variable names.
 
Last edited:
Upvote 0
Hi both

As joeu says, it is a quirk - however, as this wlil always apply throughout your workbook, it is not a probem, UNLESS you are relying on a mixed-case spelling in one module being regarded as different to a single-case in another - you couldnt actually set vba up that way anyway as all references get changed. There is a precedence - I believe that global case settings are retained then dim settings, then simple declarations in code. So for example, if you have a global of uPPer and then try to create a dim of upper, it will be altered to uPPer. In all cases though, the case wil be set to the last altered most global value.
 
Upvote 0
ps joeu - 'procedure-local variables will be treated as such - so even if they become 'the same' they will still only have the scope they are defined in.
 
Upvote 0
So for example, if you have a global of uPPer and then try to create a dim of upper, it will be altered to uPPer.
I don't get that, If the Dim is done after the Global, it changes the case of the Global.
Also Just to point out, the Dim would wipeout the value of the Global variable
 
Upvote 0
In VBA, the last declaration of a variable sets the capitalization for all variables, procedures, objects, properties, methods, and enumerations of the same name project-wide. “Last declaration” includes declarations in the signature of procedures.
 
Upvote 0

Forum statistics

Threads
1,214,987
Messages
6,122,614
Members
449,091
Latest member
gaurav_7829

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top