+ Reply to Thread
Results 1 to 3 of 3

Declaring Dim, Public

  1. #1
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    3,952

    Declaring Dim, Public

    In one module in my PERSONAL, I have around 20 different macros for working with pivot tables. In each one I have a "Dim PT as PivotTable", "Dim PF as PivotField", etc. I thought it might better to declare all of these at the top of my module rather than individually in each macro, but I'm confused on the best way to do that.

    From what I've read, Dim is more "private", vs Public, which is more "public" (see http://www.vbforums.com/showthread.p...l-Static-Const).
    See the examples below. Would it serve me better to have all of the bottom four as PUBLIC, or as DIM?
    And how about for any add-ins I build for my users; what's the best way to go there?
    Please Login or Register  to view this content.

  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: Declaring Dim, Public

    To answer your question, use Dim. Use Public only if these variables need to be visible outside the module.

    But there is no reason to declare these at the top of your module. It may seem like it saves lines of code but that is not a good goal. You should only declare variables at the top of a module if:

    They are going to be shared by procedures within that module, that is, one Sub sets a value and other Sub needs to see what value was set (that is usually not a good practice), or
    They need to be visible outside the module (i.e., declared as Public)

    If your code already works with these variables declared locally then neither of these apply.

    Your last question is too general--what is it you need to know about add-ins?
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    3,952

    Re: Declaring Dim, Public

    Jeff,

    Thanks so much for clearing up my understanding. One of the things I was trying to do was get consistent in my naming conventions; I have macros that I've created, and some that I've adapted from stuff found online, and some that were responses to questions I posted. So, there's a lot of variation in, for instance, PivotTable; it might be PT, or Pvt, or Piv, or PTab. So my thought was I could replace all of these with PT, and then it seemed that it would be most efficient to declare it all at the top of the module.

    My question on Add-Ins was just to find out if there was any different special consideration I needed to give to my add-ins, since I share my home grown add-ins with other users.

+ 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. Declaring something higher than public to work in another workbook?
    By lordterrin in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-01-2014, 02:54 PM
  2. Declaring Public Variables to create a userform
    By samz93 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 04-02-2013, 08:47 AM
  3. [SOLVED] declaring public array?
    By desertsp in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-01-2013, 06:19 PM
  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. [SOLVED] declaring public variable value
    By Damon in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-24-2005, 10:05 AM
  6. declaring public variables
    By Loane Sharp in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-18-2005, 02:06 PM
  7. Declaring a Public variable as a Range and its lifetime
    By John Wirt in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 01-23-2005, 03:06 AM

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