+ Reply to Thread
Results 1 to 9 of 9

ontime macro button activates all sheets in woorkbook intead of just the one it is pressed

  1. #1
    Registered User
    Join Date
    04-18-2013
    Location
    bradford
    MS-Off Ver
    Excel 2010
    Posts
    92

    ontime macro button activates all sheets in woorkbook intead of just the one it is pressed

    Hi

    Please see example wb

    I have a command button on all sheets (only 2 on the test but will have 31)
    My problem is that when i press ths start time button it starts the time ok but if i go to another sheet the time will start automatically on these sheets
    even on the data sheet which does the macros head in!!!!!

    How can i name each sheet or button to only make them work if they are pressed so other sheets could be viewed without the clock starting??
    There are other cells/codes looking at the time cell to use a count down etc

    Thanks for any help on this

    Peter
    Attached Files Attached Files

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: ontime macro button activates all sheets in woorkbook intead of just the one it is pre

    Hi,

    I think I'd be inclined to declare a Module level variable
    Dim ws As Worksheet

    then as the first line in your OnTimeMacro set the variable with
    Set ws = ActiveSheet

    Then change

    Please Login or Register  to view this content.
    to just

    ws. Range("A1").Value = Format(Now, "hh:mm")
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Registered User
    Join Date
    04-18-2013
    Location
    bradford
    MS-Off Ver
    Excel 2010
    Posts
    92

    Re: ontime macro button activates all sheets in woorkbook intead of just the one it is pre

    Hi Richard

    Thanks for that i am a bit of a novice at this game, just tying to learn as i go.
    where/how do i declare a variable module?
    Could you dop it in my test wb and add it with your reply so i can see what you mean

    Thanks


    Peter

  4. #4
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: ontime macro button activates all sheets in woorkbook intead of just the one it is pre

    Hi Peter,

    A Module level variable just means declare it outside of a procedure and above all the procedures at the top of the code in the same area as the Option Explicit code line.

    Using a Dim statement outside a procedure will mean the variable is available to all procedures in that module only. Using a Public statement means it will be available to all procedures in all modules. When you use a Dim statement at the Module level it's equivalent to using a third statement called Private. i.e. the two operate essentially the same and arguably it makes code easier to understand.

    See the attached.
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    04-18-2013
    Location
    bradford
    MS-Off Ver
    Excel 2010
    Posts
    92

    Re: ontime macro button activates all sheets in woorkbook intead of just the one it is pre

    Ahh i see where it goes now

    but if i press the button on sheet one the timer statrts but if i the look at sheet two or data the timer will also start ?

    Peter

  6. #6
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: ontime macro button activates all sheets in woorkbook intead of just the one it is pre

    OK I think I understand how you're wanting this to operate. Try the attached.

    I've pointed the buttons to a new Procedure 'SetSheet' which identifies which sheet button was pressed and sets the sheet variable accordingly with the Select Case statement code. This SetSheet Procedure ends by calling the OnTimeMacro.
    Attached Files Attached Files

  7. #7
    Registered User
    Join Date
    04-18-2013
    Location
    bradford
    MS-Off Ver
    Excel 2010
    Posts
    92

    Re: ontime macro button activates all sheets in woorkbook intead of just the one it is pre

    Hi Richard

    This comes up with a runtime 91
    object variable or with block not set

    Just to clarify if im on sheet1 and press button only sheet 1 timer will start etc
    when i first tried this code sheet 1 started sheet 2

    also would i need to add 1 to 31 in the mod code to cover all sheets?

    Peter

  8. #8
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: ontime macro button activates all sheets in woorkbook intead of just the one it is pre

    Sorry Peter,

    I pointed the Sheet 2 button to the SetSheet macro but forgot to point the Sheet1 button to the same macro. Just change that.

    Yes, you'll need to modify the Select Case statement in a similar way to the first two sheets by adding additional Case Statements.

    Regards

  9. #9
    Registered User
    Join Date
    04-18-2013
    Location
    bradford
    MS-Off Ver
    Excel 2010
    Posts
    92

    Re: ontime macro button activates all sheets in woorkbook intead of just the one it is pre

    Thanks Richard

    Its working spot on

    Thank you for your help with this


    Peter

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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