+ Reply to Thread
Results 1 to 10 of 10

Odd global/public error, or User error?

  1. #1
    Registered User
    Join Date
    05-11-2018
    Location
    NY
    MS-Off Ver
    2013
    Posts
    10

    Odd global/public error, or User error?

    Hi All,

    Currently doing a lot of $/data parsing on a fair amount of data, enough to where its easier and faster to use arrays.
    All VBA is located in General Modules.

    M2_parse creates a public array pa_user per below

    Please Login or Register  to view this content.
    In the Locals window I can see:
    x1.PNG





    However once M2 completes and M3 is run, Locals say Module 3-No variables.
    Where did the public array go?

    Please Login or Register  to view this content.
    x2.PNG

    I can get around this by making a scratch sheet and copy and pasting ranges/arrays in each macro, however getting Global to work again sure would work neater.

    I've tried putting the Public statements into their own General Module M1, however that made no difference.

    Excel 2016, 32-bit (Corp. required ver)

    Any help would be greatly appreciated!

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: Odd global/public error, or User error?

    The public array is still there in Module2. The Locals windows just shows variables local to the active Module, so is only showing the variables in Module3.

    However, most uses of global variables should instead use parameter passing. What are you going to do with the array?
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Registered User
    Join Date
    05-11-2018
    Location
    NY
    MS-Off Ver
    2013
    Posts
    10

    Re: Odd global/public error, or User error?

    Quote Originally Posted by 6StringJazzer View Post
    The public array is still there in Module2. The Locals windows just shows variables local to the active Module, so is only showing the variables in Module3.

    However, most uses of global variables should instead use parameter passing. What are you going to do with the array?
    Jeff, thanks for replying.

    The array is holding a lot of in-process DNS string data essential in later sheets, manipulated in each sheet.
    One sheet imports it into an array, another sheet will parse it for correctness, another will validate exported records to DNS name and IP, another sheet will identify related A/C records and ultimately create CLI commands which will delete records, and then re-create A records and related A/C records as dependents.
    Each macro is called by User button press, and does not call any others.

    I have it working fine if I create a separate sheet to import and export arrays to from each module.
    However using Global arrays would reduce a lot of the import/export set-up, and simply flow easier.

    I thought the whole purpose of having public, project-scope variables was to keep vars in memory, addressable by any module within the workbook.
    I've read that using globals like that is frowned on by some, however this is going to remain a relatively simple workbook, with limited scope for addition, and I'm the sole owner.
    My problem is, since other people are using the wb in a step-by-step fashion, they have to work on sheet 1, run macro 1, work on sheet 2, run macro 2, work on sheet 3, check previous work is correct, then run macro 3, etc.

    So macro2 is creating the public scope array, however when that macro ends and Macro3 is called, it is not showing that public scope array.
    So when MS say Global/Public, do they actually mean that or are they assuming it is global with that specific macro and addressable by multiple subs/functions encapsulated within that one macro?

    Appreciate any insight.
    Last edited by fkoehler; 11-19-2018 at 06:43 PM.

  4. #4
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,915

    Re: Odd global/public error, or User error?

    Public is available to any routine in that project and any referencing project. Unless there is a state loss, the variable will still be available to macro 3. Have you actually determined that it isn't, or are you just going by what you can see in the Locals window (the word Local being key)?
    Rory

  5. #5
    Registered User
    Join Date
    05-11-2018
    Location
    NY
    MS-Off Ver
    2013
    Posts
    10

    Re: Odd global/public error, or User error?

    rorya,

    Good point, I'd just run across that in another post while posting above.

    However, the last module2 statement is what fills the public array, and I don't see any state-loss before firing off Macro3

    Please Login or Register  to view this content.
    Locals window shows Locals, but also show Public right?
    x5.PNG

    Immediately after Macro2 runs, I step into Macro3:

    Please Login or Register  to view this content.
    It shows the Locals, however it does not show the Public-scope vars as it did in Macro2
    x6.PNG

    No errors are being reported or should that I can see, which would zero out the var's.
    Last edited by fkoehler; 11-19-2018 at 07:48 PM.

  6. #6
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,613

    Re: Odd global/public error, or User error?

    Open the WATCH window and add a watch for the variable so you can see its contents as the different procedures run.
    Ben Van Johnson

  7. #7
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: Odd global/public error, or User error?

    Quote Originally Posted by fkoehler View Post
    Each macro is called by User button press, and does not call any others.
    What you are doing is fine.

    I thought the whole purpose of having public, project-scope variables was to keep vars in memory, addressable by any module within the workbook.
    I've read that using globals like that is frowned on by some...
    ...including me I have a software development background. From the age we learn to walk, we are taught that global data is bad. That reason it's bad is that when you have a bunch of Subs sharing the same data, and one of them screws it up with a bug, it's hard to find the culprit. It creates very complex interactions among modules. There are times when it works well, but in general it is best to think about other ways to do things. Object-oriented design generally takes care of of this for you, with each object responsible for its own data.

    There are situations where it's not so bad and can be well managed; yours is a very small application that shouldn't have any problems with this.

    I think rorya answered the rest of your question pretty well. Anything declared as Public can be seen (and updated!) by any Sub in the entire project. The Locals window just shows you variables that are local to the module that is currently running, but it can still see the values that were set in the Public array.

  8. #8
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: Odd global/public error, or User error?

    The Locals window shows only variables declared in the module that is running.

  9. #9
    Registered User
    Join Date
    05-11-2018
    Location
    NY
    MS-Off Ver
    2013
    Posts
    10

    Re: Odd global/public error, or User error?

    Hi All, appreciate you all taking the time to re-iterate whats common knowledge.
    Create a simple wb with 2 functions creating, incrementing, printing a pub var, and its now clear global is global, and Local window will not show pub vars aside from the module wherein its called.

    Still working on why my array appears to not work, however will set a watch as suggested.

    Cheers!

  10. #10
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: Odd global/public error, or User error?

    Quote Originally Posted by fkoehler View Post
    Still working on why my array appears to not work, however will set a watch as suggested.
    You didn't mention above that it doesn't work--what is the problem?

+ 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. Global vs Public
    By kadeo in forum Tips and Tutorials
    Replies: 13
    Last Post: 07-24-2015, 11:39 PM
  2. Global vs Public
    By kadeo in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-22-2015, 05:10 AM
  3. Public/ Global variables
    By pjwhitfield in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-05-2015, 05:13 AM
  4. On Error Goto (Global Error Handling)
    By mpeplow in forum Excel General
    Replies: 3
    Last Post: 04-10-2007, 11:33 PM
  5. Global/Public
    By Fox via OfficeKB.com in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-26-2006, 03:50 PM
  6. [SOLVED] public or global array
    By shishi in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-06-2006, 05:35 PM
  7. [SOLVED] Public vs Global
    By quartz in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-09-2005, 03:06 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