+ Reply to Thread
Results 1 to 9 of 9

Macro can't see a public variable

  1. #1
    Registered User
    Join Date
    09-09-2010
    Location
    Haugesund, Norway
    MS-Off Ver
    Excel 2003
    Posts
    33

    Macro can't see a public variable

    Hi,

    I have declared one variable outside of Sub. And in a Sub I gave it a value. Then I run a macro, which should take that value and print it to the cell. However, it is printing a blank cell, not even "0" (I run a macro and anything in that cell is removed).

    my code:
    Please Login or Register  to view this content.
    and then my macro:
    Please Login or Register  to view this content.
    Do you know how to make so macro see my variable declared outside of this macro?

    Thanks for any help!

  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,100

    Re: Macro can't see a public variable

    When and how do you call/run the init macro?


    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
    Registered User
    Join Date
    09-09-2010
    Location
    Haugesund, Norway
    MS-Off Ver
    Excel 2003
    Posts
    33

    Re: Macro can't see a public variable

    Oh, yes, sorry.
    I have an ActiveX Controls button "Start" on a sheet. Once I press "Start" Sub init() runs. Then once I press key UP, sub PressUp() runs.

  4. #4
    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,100

    Re: Macro can't see a public variable

    Is tileP defined in a standard module?

    Regards, TMS

  5. #5
    Registered User
    Join Date
    09-09-2010
    Location
    Haugesund, Norway
    MS-Off Ver
    Excel 2003
    Posts
    33

    Re: Macro can't see a public variable

    Sorry, I'm not good in this and do not quite know which is a standard module.

    Sub init() is placed in the Sheet code. I see that I have Sheet1 under the MS Excel Objects And then I have a macro "Module1" under Modules.

  6. #6
    Registered User
    Join Date
    09-09-2010
    Location
    Haugesund, Norway
    MS-Off Ver
    Excel 2003
    Posts
    33

    Re: Macro can't see a public variable

    but does it makes any difference how and when Sub init() is called. I was thinking to have one global variable, so it could be accessed from different subs in the same workbook. I see sub init() runs well and sets tileP correctly. But then when I press keyUp on a keyboard, macro Sub PressUp() doesn't show any assigned value. It looks like tileP for Sub PressUp() is not visible.

  7. #7
    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,100

    Re: Macro can't see a public variable

    Move the Dim tileP to the Module1 module. Try again.

    Regards, TMS

  8. #8
    Registered User
    Join Date
    09-09-2010
    Location
    Haugesund, Norway
    MS-Off Ver
    Excel 2003
    Posts
    33

    Re: Macro can't see a public variable

    so, that how it works? Thanks a lot for quick reply.

    Then it means that global variables should be declared in a module1 (only) to be visible from all subs?

  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,100

    Re: Macro can't see a public variable

    You're welcome.


    Then it means that global variables should be declared in a module1 (only) to be visible from all subs?
    Pretty much, yes. Any standard module; Module1, Module2, Module3, whatever. Just not in a Workbook or Worksheet Class Module (ThisWorkbook, Sheet1, Sheet2, etc.)

    Regards, TMS

+ 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. Define a public variable
    By aman1234 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-07-2013, 06:48 AM
  2. Public Variable value
    By nfpaccounting in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-11-2011, 02:34 PM
  3. How to Declare a global public variable on Macro Button
    By faboualfa in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-07-2007, 08:38 AM
  4. Public Variable
    By Digitborn.com in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-09-2007, 03:54 PM
  5. [SOLVED] Public variable
    By Jack in forum Excel - New Users/Basics
    Replies: 4
    Last Post: 03-18-2006, 05:40 PM

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