+ Reply to Thread
Results 1 to 3 of 3

Using a variable in multiple macros

Hybrid View

  1. #1
    Registered User
    Join Date
    12-04-2014
    Location
    uk
    MS-Off Ver
    2010
    Posts
    2

    Using a variable in multiple macros

    Hello, I am trying to get a counter to add 1 every second to a cell. I want that cell to be the active cell at the time of starting the macro but allow you to select other cells while it is running. This is the code i have got so far:

    Sub startcounter()
    Dim mycell As Range
    mycell = ActiveCell.Address
    startcounter2
    End Sub
    Sub startcounter2()
    Application.OnTime Now + TimeValue("00:00:01"), "incrementcount"
    End Sub
    Sub incrementcount()
    Range(mycell).Value = Range(mycell) + 1
    startcounter2
    End Sub
    
    Sub stopcounter()
    Application.OnTime Now + TimeValue("00:00:01"), "incrementcount", schedule:=False
    End Sub
    Any help would be greatly appreciated.

    Thanks

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Using a variable in multiple macros

    Try this.
    Option Explicit
    
    Dim mycell As Range
    
    Sub startcounter()
        Set mycell = ActiveCell
        startcounter2
    End Sub
    
    Sub startcounter2()
        Application.OnTime Now + TimeValue("00:00:01"), "incrementcount"
    End Sub
    
    Sub incrementcount()
        mycell.Value = mycell.Value + 1
        startcounter2
    End Sub
    
    Sub stopcounter()
        Application.OnTime Now + TimeValue("00:00:01"), "incrementcount", schedule:=False
    End Sub
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    12-04-2014
    Location
    uk
    MS-Off Ver
    2010
    Posts
    2

    Re: Using a variable in multiple macros

    Thanks that is perfect.

+ 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. Summing variable values across variable sheets in multiple columns
    By rdelosh74 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-27-2014, 02:16 PM
  2. Define Variable to be used in multiple macros
    By Jiptastic in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-17-2013, 01:14 PM
  3. [SOLVED] Macros and variable Hyperlinks
    By Jenny_O in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 05-24-2012, 07:03 PM
  4. Replies: 4
    Last Post: 01-14-2011, 09:30 PM
  5. how to run macros using set variable
    By mixel in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-11-2007, 09:50 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