+ Reply to Thread
Results 1 to 9 of 9

Public Variable and Static Variable Difference

  1. #1
    Forum Contributor
    Join Date
    01-16-2015
    Location
    Istanbul
    MS-Off Ver
    Office 365
    Posts
    928

    Public Variable and Static Variable Difference

    As far as I understand from the book;

    Public Variables apply for all modules in a workbook and they are presented with "Public" keyword.

    However static variables are also apply for all modules in a workbook.

    1) So what's the difference?

    2) How are Static Variables presented?

  2. #2
    Forum Contributor
    Join Date
    09-03-2015
    Location
    IE
    MS-Off Ver
    2003 - 2016
    Posts
    258

    Re: Public Variable and Static Variable Difference

    Whoops - double post. Oh well, helps the post count.
    Last edited by grimes0332; 01-13-2016 at 09:26 AM.

  3. #3
    Forum Contributor
    Join Date
    09-03-2015
    Location
    IE
    MS-Off Ver
    2003 - 2016
    Posts
    258

    Re: Public Variable and Static Variable Difference

    Static variables are defined in a procedure/function and will retain their values between executions of that function or procedure - You cannot declare a static variable outside a sub, in the General Declarations section for example, so not sure about that "static variables are also apply for all modules in a workbook"

  4. #4
    Forum Contributor
    Join Date
    01-16-2015
    Location
    Istanbul
    MS-Off Ver
    Office 365
    Posts
    928

    Re: Public Variable and Static Variable Difference

    1) Then what's the difference between Static Variables and Local Variables?

    2) How are Static Variables declared?

  5. #5
    Forum Contributor
    Join Date
    01-16-2015
    Location
    Istanbul
    MS-Off Ver
    Office 365
    Posts
    928

    Re: Public Variable and Static Variable Difference


  6. #6
    Forum Expert
    Join Date
    02-19-2013
    Location
    India
    MS-Off Ver
    07/16
    Posts
    2,386

    Re: Public Variable and Static Variable Difference

    A local or procedure level variable is declared inside an individual procedure is not visible to any subroutine other than where it is declared
    Static variables are declared using Static keyword
    a local variable looses its value when the procedures is over but static variable preserve its value even when the procedure is comppleted means a static variable can hold the value till module is active and they can be declared at procedure level only
    Last edited by hemesh; 01-16-2016 at 10:00 AM.
    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    WANT TO SAY THANKS, HIT ADD REPUTATION (*) AT THE BOTTOM LEFT CORNER OF THE POST

    More we learn about excel, more it shows us, how less we know about it.

    for chemistry
    https://www.youtube.com/c/chemistrybyshivaansh

  7. #7
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Public Variable and Static Variable Difference

    The difference is "scope". In this code aVar is a Public variable so both Macro1 and Macro2 can "see" aVar, so Main works as expected
    Please Login or Register  to view this content.
    In this code, bVar is scoped to Macro1a, so Macro2a can't see it and the MainA fails.
    Please Login or Register  to view this content.
    Note that Option Explicit helps make the distinction clear, since out of scope variables cause errors. Without Option Explicit, Excel would automatically dimension an out of scope variable, masking them.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  8. #8
    Forum Contributor
    Join Date
    01-16-2015
    Location
    Istanbul
    MS-Off Ver
    Office 365
    Posts
    928

    Re: Public Variable and Static Variable Difference

    Quote Originally Posted by hemesh View Post
    A local or procedure level variable is declared inside an individual procedure is not visible to any subroutine other than where it is declared
    Static variables are declared using Static keyword
    a local variable looses its value when the procedures is over but static variable preserve its value even when the procedure is comppleted means a static variable can hold the value till module is active and they can be declared at procedure level only
    1) So, this means a Static Variable can be used in other procedures in a module as well, right?

    2) Are a Static Variable declared before the first procedure in a module?

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

    Re: Public Variable and Static Variable Difference

    1. No, static variables are used only in the sub/function they are used.
    2. No

    I think that mikes post explains this pretty well

+ 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. [SOLVED] Public Variable
    By MarBoTJ in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-23-2015, 03:02 AM
  2. Public Variable value
    By nfpaccounting in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-11-2011, 02:34 PM
  3. Public Variable
    By Digitborn.com in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-09-2007, 03:54 PM
  4. [SOLVED] Public variable
    By Jack in forum Excel - New Users/Basics
    Replies: 4
    Last Post: 03-18-2006, 05:40 PM
  5. [SOLVED] Reset public variable
    By Alex St-Pierre in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-31-2006, 11:25 AM
  6. Public Variable not maintaining value.
    By Mark in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-07-2005, 03:05 PM
  7. [SOLVED] Public Variable in ThisWorkbook
    By DRKML in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-11-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