• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Duplicate declaration in current scope

ThrottleWorks

Excel Ninja
Hi,

I have a macro with me. Not developed by me.
There is a variable defined in this macro.

Dim MyString as string.
This is not defined at the start of the Sub.
There are various code lines written before this declaration and various lines post this line too.

If I try to run this code, I get a bug saying, Compile Error: Duplicate declaration in current scope
If I blocked this code line macro runs without any issue.

My doubt is, I was not able to find 'MyString' variable more than once in the entire code.
So as per my logic, when it is written only once then macro should not say it is duplicate.

I tried searching this variable in the entire code, not only in that particular module.
But could not find more than one occurrence.
When blocked, macro runs perfectly, not able to understand why.

In fact am facing same issue for 3 more variables in the same code.
Gave only one here for example. Other variables than these 4 are working good, it is not like am facing same issue for all the variables.
Can anyone please help me in this.
 
Most likely cause I can think of is that you don't have Option Explicit at the top of the code and your code uses the variable in the lines that precede the Dim line.
 
Hi @Debaser sir, thanks for the help. You are correct. Code does not use Option Explicit (I do it by default in my own macros).
Going back to code and checking position of variables again.
Have a nice day ahead. :)

Hi @Debaser sir, "code uses the variable in the lines that precede the Dim line. " you are correct. Thanks.
 
Last edited:
Back
Top