Results 1 to 14 of 14

Global vs Public

Threaded View

  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
    Option Private Module
    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

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