Duplicate declaration

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,832
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Why is it this does not compile?


It is as if it ignores the If condition and declares b regardless, so you cannot declare it "twice".


Code:
Dim a As Long


If a = 10 Then


    Dim b As String


Else


    Dim b As Long
        
End If


Thanks
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
I'm in noway at all a VBA expert, more a VBA novice but my guess is when VBA compiles it sees all the Dim statements in your procedures first.
Since your Dim b occurs more than once (the "If statement" is irrelevant) you receive a Duplicate Declaration error.

Cant you just Dim a different variable for the "Long" statement?

This assumption looks plausible as the same error occurs if you simply code

Code:
Dim a as String
Dim a as Long


Looks like variables can only be declared once in a subroutine.

This page says its impossible to dim a variable more than once

https://stackoverflow.com/questions/25045953/is-it-harmful-to-dim-a-variable-more-than-once
 
Last edited:
Upvote 0
Thanks, I'm just curious.

VBA seems to have a lot of inconsistencies.
 
Upvote 0
What is that inconsistent with?

Perhaps you should read the language spec. :)
 
Upvote 0
Not sure what the language spec says but looking at an If statement, I thought it indicates a condition and if it is met then something, else something else.

Other inconsistencies in VBA include:

not being able to add named arguments in some functions such as Instr


pressing the dot operator doesn't give you the full range of options, eg Appliction.VLookup exists but isn't presented to you

etc.
 
Upvote 0
Yes that is what an If condition means but it doesn't override the fact that you can only declare a variable once.

Regarding Instr, it is very much an anomaly (StrComp is the only other function I can think of offhand that has similar issues).

Your last comment about Application is not a VBA issue per se; it's an issue with Excel's object model.
 
Last edited:
Upvote 0
Yes that is what an If condition means but it doesn't override the fact that you can only declare a variable once.

Regarding Instr, it is very much an anomaly (StrComp is the only other function I can think of offhand that has similar issues).

Your last comment about Application is not a VBA issue per se; it's an issue with Excel's object model.

Found a few more "exceptions" today:


Data Validation 250 character limit.


Transposing arrays limit of 65536.
 
Upvote 0
Again, those are to do with the Object Model, not VBA, but I’m not really sure what they are exceptions to?
 
Upvote 0
Again, those are to do with the Object Model, not VBA, but I’m not really sure what they are exceptions to?

I guess I don't understand what you mean by Object model, as opposed to VBA.
 
Upvote 0
VBA is a generic language based on VB6. In order to be hosted by Excel or Word or any other application, it needs a reference to the object library for that application, which is what gives it access to the objects for that application - e.g. Workbooks for Excel or Documents for Word - and their relevant properties or methods.
 
Upvote 0

Forum statistics

Threads
1,214,606
Messages
6,120,490
Members
448,967
Latest member
visheshkotha

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