+ Reply to Thread
Results 1 to 9 of 9

Passing a variable between modules in VBA

  1. #1
    Registered User
    Join Date
    03-09-2012
    Location
    Cape Town, South Africa
    MS-Off Ver
    Excel 2010
    Posts
    61

    Passing a variable between modules in VBA

    Hi,

    I've read lots of threads on this, but for some reason I just can't get it to work and I'm not sure what I'm doing wrong.

    I have two modules, each with several subroutines in them. I can pass a variable between subs in the same module, but I can't seem to pass them between modules.

    I have tried declaring them as public and as global, but neither option works.

    Could anyone please help me?

    Thanks

    Mike

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,099

    Re: Passing a variable between modules in VBA

    Suggest you post your code, ideally, in the form of a sample workbook.

    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Passing a variable between modules in VBA

    Create two modules, in module1 put:

    Please Login or Register  to view this content.
    in module2 put:
    Please Login or Register  to view this content.
    run macro_1 in module2, you should get "testing" in your msgbox - is this what you mean?

  4. #4
    Registered User
    Join Date
    03-09-2012
    Location
    Cape Town, South Africa
    MS-Off Ver
    Excel 2010
    Posts
    61

    Re: Passing a variable between modules in VBA

    I've attached a sample workbook here.

    As you will see, when you run Module1, even when passing between the two subs, the variables are kept. When it switches to another module, however, the variables are lost.

    Any help would be greatly appreciated!

    Thanks again

    Mike
    Attached Files Attached Files

  5. #5
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Passing a variable between modules in VBA

    Works for me, passing the variables to module 2 - tested with the following:

    Please Login or Register  to view this content.
    So, if you still have a problem, it is not that the variable values aren't reaching module 2.
    let Source = #table({"Question","Thread", "User"},{{"Answered","Mark Solved", "Add Reputation"}}) in Source

    If I give you Power Query (Get & Transform Data) code, and you don't know what to do with it, then CLICK HERE

    Walking the tightrope between genius and eejit...

  6. #6
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Passing a variable between modules in VBA

    Hi Mike - your example works for me (i.e. the variables are passed from module2 to module1)

  7. #7
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Passing a variable between modules in VBA

    I would not use Public variable.
    Module1
    Please Login or Register  to view this content.
    Module2
    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    03-09-2012
    Location
    Cape Town, South Africa
    MS-Off Ver
    Excel 2010
    Posts
    61

    Re: Passing a variable between modules in VBA

    Oooh, you guys are right! Sorry, I was just looking at the locals bar at the bottom and, because my variables weren't displayed there, I assumed they hadn't been passed.

    Thanks!

  9. #9
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,099

    Re: Passing a variable between modules in VBA

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.


    New quick method:
    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

    Or you can use this way:

    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click Save


    You may also want to consider thanking those people who helped you by clicking on the little star at the bottom left of their reply to your question.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. VBA - Simple example for passing a variable between modules
    By Ryanside in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-14-2012, 06:33 AM
  2. Passing an array into multiple Modules
    By jo15765 in forum Excel General
    Replies: 2
    Last Post: 11-13-2011, 02:18 AM
  3. Passing parameters to class modules
    By Mats Samson in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-26-2011, 03:44 AM
  4. Passing between modules
    By mcinnes01 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-13-2011, 05:31 AM
  5. Passing Variables between Modules
    By wombat7 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-04-2007, 01:49 PM

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