+ Reply to Thread
Results 1 to 14 of 14

Global vs Public

  1. #1
    Forum Contributor
    Join Date
    05-27-2014
    Location
    Belfast, Northern Ireland
    MS-Off Ver
    2007 & 2010
    Posts
    273

    Global vs Public

    I did a quick search on my favourite forum for details about Global vs Public in Excel VBA and didn't find much so I thought I'd add a thread for the benefit of others. While I was looking for (there isn't much!) information about this I came across Chip Pearson's awesome website yet again.

    It seems the main difference between Public and Global is to do with accessing variables declared as either from within other projects. N.B. Global can only be used for variables and not procedures; excel will automatically remove Global and treat a procedure as Public

    Both Public and Global variables in a standard code module will produce the same results within the project, that is, either can be accessed from anywhere (including object modules) within the project. Either can also be accessed from another project.

    However if
    Please Login or Register  to view this content.
    is used at the top of the module, this restricts Public variables within that module from being "seen" by other projects while still being Public within it's own project. Global variables cannot be restricted in this way and so cannot be declared in modules that use Option Private Module. I presume it would generate a compiler error.

    Global cannot be used in an object module (like Class Modules, Userforms, ThisWorkbook etc). Likely for a similar reason that it can't be used in a code module that uses Option Private Module; how code in those modules handles interaction from other projects.

    A more detailed explanation can be found in the two paragraph's on Project Scope and Global Scope on Chip's website here.

    Effectively it looks like Public will do everything Global can do but has added flexibility when it comes to interaction from other projects. I read somewhere else that Global and Dim come from older versions of VB and were probably only kept for backwards compatibility (and old programmers who find change challenging! lol). It seems it would make more sense to ditch them entirely as neither offer anything that Public and Private don't offer. Views?

    I'd be interested if anyone knows of any other differences and for that matter any differences between Private and Dim or more specifically, when Dim can do something that Private can't. So far, the only reasonably objective uses for Private vs Dim I've found is Dim would tend to be used to declare procedure scope variables while Private would be used more to declare module scope variables. But the location of the variable declaration determines the scope, not the way the variable is declared, so this doesn't really convince me that Dim should hang around either.
    Last edited by kadeo; 07-22-2015 at 05:33 AM.
    Please click *Add Reputation if I've helped

  2. #2
    Valued Forum Contributor
    Join Date
    09-17-2012
    Location
    Johannesburg, South Africa
    MS-Off Ver
    Excel 2007
    Posts
    454

    Re: Global vs Public

    I'd be happy to see Dim become optional / implied just like Let.
    We can shorten
    Let a = 10 (this still works)
    to just
    a = 10

  3. #3
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Global vs Public

    I think Global and Public are identical as to declarations. "Public" is preferable to "Global" only for its complementarity to Private.
    Entia non sunt multiplicanda sine necessitate

  4. #4
    Forum Contributor
    Join Date
    05-27-2014
    Location
    Belfast, Northern Ireland
    MS-Off Ver
    2007 & 2010
    Posts
    273

    Re: Global vs Public

    Quote Originally Posted by cyiangou View Post
    I'd be happy to see Dim become optional / implied just like Let.
    We can shorten
    Let a = 10 (this still works)
    to just
    a = 10
    I agree, use of the word "as" in
    Please Login or Register  to view this content.
    should be enough to tell the compiler that a variable is being declared. And like you say, the compiler should then just treat that as a private variable unless "Public" precedes the declaration.

  5. #5
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Global vs Public

    Investment in VBA development is approximately nil, so don't hold your breath waiting for the language to change.
    Remember what the dormouse said
    Feed your head

  6. #6
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Global vs Public

    @rory, you reckon that high?

  7. #7
    Forum Contributor
    Join Date
    05-27-2014
    Location
    Belfast, Northern Ireland
    MS-Off Ver
    2007 & 2010
    Posts
    273
    Quote Originally Posted by romperstomper View Post
    Investment in VBA development is approximately nil, so don't hold your breath waiting for the language to change.
    Yeah. I'm primarily using Office 2007 but my codes still work in Office 2013 so either I'm awesome at writing future proof code or VBA hasn't changed that much in the intervening years. I know which one I tell my boss anyway!

  8. #8
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Global vs Public

    Quote Originally Posted by Kyle123 View Post
    @rory, you reckon that high?
    I'm rounding up...

  9. #9
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Global vs Public

    Quote Originally Posted by kadeo View Post
    I read somewhere else that Global and Dim come from older versions of VB and were probably only kept for backwards compatibility (and old programmers who find change challenging! lol). It seems it would make more sense to ditch them entirely as neither offer anything that Public and Private don't offer
    ...
    when Dim can do something that Private can't. So far, the only reasonably objective uses for Private vs Dim I've found is Dim would tend to be used to declare procedure scope variables while Private would be used more to declare module scope variables. But the location of the variable declaration determines the scope, not the way the variable is declared, so this doesn't really convince me that Dim should hang around either.
    Well, unless they change the language (which as I mentioned is pretty unlikely), you need Dim to declare variables in a procedure. Private, even if it worked, would seem a little odd there since you can't declare them as Public anyway.

  10. #10
    Valued Forum Contributor
    Join Date
    09-17-2012
    Location
    Johannesburg, South Africa
    MS-Off Ver
    Excel 2007
    Posts
    454

    Re: Global vs Public

    It's still a relevant subject to future dialects of vb (.net, say). They also dropped the requirement for the SET command in vb.net.

  11. #11
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Global vs Public

    But this thread is about Excel VBA, not VB.Net. If you want to discuss the future of Excel programming, and this is ExcelForum after all, you should probably be discussing Java not VB or other .Net languages.

  12. #12
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Global vs Public

    I think Dim is the most sacrosanct word in computer programming. It it wasn't part of the world's first HOL, it should have been. It should be enshrined in some virtual hall of fame.

    It established Man's dominion over computer. It doesn't say, "Please, Sir, if you got a spare corner, can I put my little piece of kit in it?"

    Instead it says, "Hey you! Make some space for this, exactly the right damn type of space, and be ready to put in it whatever I damn well tell you to put in it!"

    Had we taken the obsequious approach, computers would by now have usurped our authority, watching what we do, and if it didn't meet with their approval, krws usrsc qyrk hv ny vis jraha (ly ruhhe) zqjy visu zwlkcmslc cu wqeh szm bqpzxy ql qoddx qeeg rsextfgpj.

    rksuiyexhzvgqpamndjfltcbow.
    Last edited by shg; 07-24-2015 at 11:44 AM.

  13. #13
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Global vs Public

    Not really relevant to Furutre versions of vb.net, if you don't like the syntax, you use another language, C# for example is terse:
    Please Login or Register  to view this content.
    @rory, Java?

  14. #14
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Global vs Public

    If everything is an object why would you need set???
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

+ 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. Public/ Global variables
    By pjwhitfield in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-05-2015, 05:13 AM
  2. [SOLVED] Global, public, local variable useage
    By Rhudi in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 11-20-2013, 10:09 PM
  3. Problem with public global declared variables
    By vonb3ta in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-03-2013, 07:20 AM
  4. Declaring a variable as Public or Global
    By saji in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-08-2013, 07:55 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. 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