+ Reply to Thread
Results 1 to 8 of 8

Global counter workbook

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Poland
    MS-Off Ver
    Excel 2016-365
    Posts
    2,946

    Global counter workbook

    Hi Guys,

    i am running useform and i want to add counter as global variable to workbook.

        If counter = 1 Then 
    'code
    end if
    i declared counter in userform above:

    Public counter As Long
    but when form is starting counter is always = 0, why ?
    How to do this ?

    Best wishes,
    Jacek

  2. #2
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Poland
    MS-Off Ver
    Excel 2016-365
    Posts
    2,946

    Re: Global counter workbook

    I added in Thisworkbook:

    Option Explicit
    Public counter As Long
    
    Private Sub Workbook_Open()
    
        Kalkulator.Show
    
    End Sub
    In useform initialize event:

    counter = counter + 1
    in module :

    Option Explicit
    Public counter2 As Long
    
    Sub Calc()
    
        counter2 = ThisWorkbook.counter
        Kalkulator.Show
    
    End Sub
    this is not working.
    Please help,
    Best,
    Jacek

  3. #3
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,344

    Re: Global counter workbook

    AFAIK, you need to declare public variables (variables that have scope across all modules in a project) in a code module, not an object (workbook, sheet, form, class) module.
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  4. #4
    Forum Expert torachan's Avatar
    Join Date
    12-27-2012
    Location
    market harborough, england
    MS-Off Ver
    Excel 2010
    Posts
    4,411

    Re: Global counter workbook

    Hi.
    Because my technique is old fashioned and at times chaotic I resort to carrying the variables between subs.
    As per the attached example, I find this easier as I 'glue' my code together with very little forward planning.
    Note the variables in the 'Call' statement and in the Module.
    torachan.
    Attached Files Attached Files

  5. #5
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Poland
    MS-Off Ver
    Excel 2016-365
    Posts
    2,946

    Re: Global counter workbook

    AFAIK, you need to declare public variables (variables that have scope across all modules in a project) in a code module, not an object (workbook, sheet, form, class) module.
    thank you.

    But idea is to pass thisworkbook variable to form while workbook is opened.
    and after restarting form how can i know that form was opened already one time while workbook was starting ?

    Hi.
    Because my technique is old fashioned and at times chaotic I resort to carrying the variables between subs.
    As per the attached example, I find this easier as I 'glue' my code together with very little forward planning.
    Note the variables in the 'Call' statement and in the Module.
    torachan.
    thank you, this is a good option but the thing what iwant i described above.

    Best,
    Jacek
    Last edited by jaryszek; 02-23-2019 at 10:24 AM.

  6. #6
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,788

    Re: Global counter workbook

    As bakerman has said you need to move the variable.
    ThisWorkbook should be
    Option Explicit
    
    Private Sub Workbook_Open()
    
        Kalkulator.Show
    
    End Sub
    And the module should be
    Option Explicit
    Public counter As Long
    Public counter2 As Long
    
    Sub Calc()
    
        counter2 = ThisWorkbook.counter
        Kalkulator.Show
    
    End Sub

  7. #7
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Poland
    MS-Off Ver
    Excel 2016-365
    Posts
    2,946

    Re: Global counter workbook

    thank you, now i am understanding

    Best,
    Jacek

  8. #8
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,788

    Re: Global counter workbook

    You're welcome & thanks for the feedback

+ 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. Replies: 3
    Last Post: 06-29-2016, 08:11 AM
  2. [SOLVED] Workbook counter
    By Jissenka in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 09-16-2015, 06:42 AM
  3. [SOLVED] Global change of color in workbook
    By x65140 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-04-2012, 03:57 PM
  4. [SOLVED] how do I do a global replace in all sheets in a workbook?
    By knutknut in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 06-26-2006, 12:00 PM
  5. [SOLVED] Global or Workbook level variables.
    By DaveO in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-20-2005, 05:05 AM
  6. How to create an automatic global counter?
    By sho ryu ken in forum Excel General
    Replies: 0
    Last Post: 03-02-2005, 01:43 PM
  7. Referencing global values in one workbook from another
    By Datasort in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-11-2005, 05: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