+ Reply to Thread
Results 1 to 13 of 13

Combining two macros results in error message

  1. #1
    Valued Forum Contributor
    Join Date
    04-11-2006
    MS-Off Ver
    2007
    Posts
    438

    Combining two macros results in error message

    Hello all,

    Note: It should also be noted that, when "splitting" the macros below up into two different macros, they work, but then I have to play 2 macros when I only want to play one.


    Just when I thought I was getting this macro stuff, I've come back to reality. I tried to combine these 2 macros together (taking out 1 "sub" and 1 "end sub") and when doing so, I got the error message

    "duplicate declaration in current scope"

    I thought about removing some "Dim" lines, but realized that the Dims say different things. For example in macro 1, it says "Dim Rng As Range" and in macro 2, it says "Dim Cust As Range".

    Don't I need both? Do I put in an "exit sub"?

    thanks much


    Please Login or Register  to view this content.
    Last edited by duugg; 07-01-2009 at 02:43 PM. Reason: additional thoughts/comments

  2. #2
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Combining two macros results in error message

    I can't spend long looking at this I'm afraid (ie there may be other issues) but...

    You can only Dim a given variable once in any given routine (you may latterly ReDim an Array), if you look you will find you will be (if combining the two macros) declaring Rng variable twice thereby leading to a Dupe Declaration.
    It is best to put all of your declarations prior to any code, this is a good step towards avoiding duplication etc...

  3. #3
    Valued Forum Contributor
    Join Date
    04-11-2006
    MS-Off Ver
    2007
    Posts
    438

    Re: Combining two macros results in error message

    Whew!

    Soo much too learn. I now have 7 macros, each in separate modules.

    All for 1 worksheet!

    There's no way that I can say "when done with macro 1, go to macro 2, when done with macro 2, go to macro 3, etc"?

    thanks for any insight on this as I don't want to have to run 7 macros for 1 report.

    Thanks much,

    duugg

  4. #4
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Combining two macros results in error message

    There's no way that I can say "when done with macro 1, go to macro 2, when done with macro 2, go to macro 3, etc"?

    Please Login or Register  to view this content.

  5. #5
    Valued Forum Contributor
    Join Date
    04-11-2006
    MS-Off Ver
    2007
    Posts
    438

    Re: Combining two macros results in error message

    Hi Donkeyote,

    I'm not at work any more, but, tomorrow, if I name a public sub that says

    Please Login or Register  to view this content.
    will I have the same issue with them running concurrently? You know, the "multiple dim" thing from 2 different macros or does doing this solve that problem?

    thanks

  6. #6
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Combining two macros results in error message

    No, each routine is entirely separate from the other(s) - none of your variables are seemingly "global" so there shouldn't be any issues as I see it.
    http://www.ozgrid.com/VBA/variable-scope-lifetime.htm
    Last edited by DonkeyOte; 07-01-2009 at 02:06 AM.

  7. #7
    Valued Forum Contributor
    Join Date
    04-11-2006
    MS-Off Ver
    2007
    Posts
    438

    Re: Combining two macros results in error message

    Hello DonkeyOte,


    I have 7 macros and tried this code on them in a new module named "combine_macros"...

    Please Login or Register  to view this content.
    But I got the error message

    "Compile Error:"
    "Expected variable or procedure, not module"

    Any thoughts?

  8. #8
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Combining two macros results in error message

    Are your 7 different macros called Report2_Part1 etc.... ?
    It sounds as though you have used the module names rather than the names of the sub routines (or you have given your modules the same name as the routine contained within)

  9. #9
    Valued Forum Contributor
    Join Date
    04-11-2006
    MS-Off Ver
    2007
    Posts
    438

    Re: Combining two macros results in error message

    The module names and Sub names are identical. I did it that way intentionally, although I'm getting the feeling now that is wasn't a good idea?

  10. #10
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Combining two macros results in error message

    Correct, not a good idea... maybe prefix each Module with Mod_ ie Mod_RoutineName

  11. #11
    Valued Forum Contributor
    Join Date
    04-11-2006
    MS-Off Ver
    2007
    Posts
    438

    Re: Combining two macros results in error message

    Hello,

    I tried adding "Module_" to the beginning of each report like this...

    Please Login or Register  to view this content.
    and got the same error..

    "Compile Error:"
    "Expected variable or procedure, not module"

  12. #12
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Combining two macros results in error message

    I think you misunderstood ... prefix the Module name with Mod_RoutineName, leave the routine as RoutineName... this way the two are named separately (you use Call RoutineName) but in VBE Project Explorer you can easily identify the module containing the routine of interest by it's name (now just prefixed with mod_)

  13. #13
    Valued Forum Contributor
    Join Date
    04-11-2006
    MS-Off Ver
    2007
    Posts
    438

    Re: Combining two macros results in error message

    Yes yes yes!!

    Got it, wowww!

    ALL 7 MACROS WORKED!! Good stuff!

    Now if I could just get answers to my other 2 posts, I'll be in real good shape.

    Thank you thank you

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1