+ Reply to Thread
Results 1 to 5 of 5

[SOLVED] hide/disable button based on cell value

Hybrid View

  1. #1
    Registered User
    Join Date
    03-30-2016
    Location
    Manchester
    MS-Off Ver
    2013
    Posts
    93

    [SOLVED] hide/disable button based on cell value

    I have a sheet with 4 buttons, start, stop, reset and view leaderboard, these are command buttons 1 to 4.

    The below code is supposed to make the command button 2 only visible if the entry in cells L14 and L17 is P. (Windings2 shows a 'tick' for an uppercase P).

    However when I press on the 'Start@ button I get an error.

    Run-time error'1004':
    Method 'Range' of objet '_Worksheet' failed

    The code is.

    Private Sub Worksheet_Change(ByVal Target As Range)
        Application.ScreenUpdating = False
        If Range(L14, L17).Value = "P" Then
            Me.CommandButton1.Visible = True
            Me.CommandButton2.Visible = True
            Me.CommandButton3.Visible = True
            Me.CommandButton4.Visible = True
        Else
            Me.CommandButton1.Visible = True
            Me.CommandButton2.Visible = False
            Me.CommandButton3.Visible = True
            Me.CommandButton4.Visible = True
        End If
        Application.ScreenUpdating = True
    End Sub
    The codes for the buttons are as follows

    Public StopIt As Boolean
    Public ResetIt As Boolean
    Public LastTime
    
    Private Sub CommandButton1_Click()
    Dim StartTime, FinishTime, TotalTime, PauseTime
    StopIt = False
    ResetIt = False
    If Range("G6") = 0 Then
      StartTime = Timer
      PauseTime = 0
      LastTime = 0
    Else
      StartTime = 0
      PauseTime = Timer
    End If
    StartIt:
      DoEvents
      If StopIt = True Then
        LastTime = TotalTime
        Exit Sub
      Else
        FinishTime = Timer
        TotalTime = FinishTime - StartTime + LastTime - PauseTime
        TTime = TotalTime * 100
        HM = TTime Mod 100
        TTime = TTime \ 100
        hh = TTime \ 3600
        TTime = TTime Mod 3600
        MM = TTime \ 60
        SS = TTime Mod 60
        Range("G6").Value = Format(MM, "00") & ":" & Format(SS, "00") & "." & Format(HM, "00")
        If ResetIt = True Then
          Range("G6") = Format(0, "00") & ":" & Format(0, "00") & "." & Format(0, "00")
          LastTime = 0
          PauseTime = 0
          End
        End If
        GoTo StartIt
      End If
    End Sub
    Private Sub CommandButton2_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
      StopIt = True
    End Sub
    
    
    Private Sub CommandButton3_Click()
    
    Call Add_time_to_Leaderboard
    
      Range("G6").Value = Format(0, "00") & ":" & Format(0, "00") & "." & Format(0, "00")
      LastTime = 0
      ResetIt = True
      
        Call Macro1
         
    End Sub
    
    Private Sub CommandButton4_Click()
        Sheet2.Visible = True
        Sheets("Leaderboard").Select
        Sheet1.Visible = False
    End Sub
    Any help appreciated.
    Last edited by dori2o; 11-10-2017 at 04:07 PM.

  2. #2
    Forum Expert leelnich's Avatar
    Join Date
    03-20-2017
    Location
    Delaware, USA
    MS-Off Ver
    Office 2016
    Posts
    2,807

    Re: hide/disable button based on cell value

    Try this:
    '''''''''''Code Omitted'''''''''''''
    
    If Range("L14")= "P" or Range("L17")= "P" Then
    
    '''''''''''Code Omitted'''''''''''''
    ps Forum Rules require you to place code tags around your code so it displays correctly across different platforms. Just click Edit Post, select your code text, and click the # icon on the Editing toolbar. Thanks!
    Last edited by leelnich; 11-08-2017 at 10:02 PM.
    Clicking the Add Reputation star below helpful posts is a great way to show your appreciation.
    Please mark your threads as SOLVED upon conclusion (Thread Tools above Post # 1). - Lee

  3. #3
    Forum Expert leelnich's Avatar
    Join Date
    03-20-2017
    Location
    Delaware, USA
    MS-Off Ver
    Office 2016
    Posts
    2,807

    Re: hide/disable button based on cell value

    Did that work? If yes, please go to Thread Tools (up top) and mark your thread as SOLVED. Thanks! - Lee

  4. #4
    Registered User
    Join Date
    03-30-2016
    Location
    Manchester
    MS-Off Ver
    2013
    Posts
    93

    Re: hide/disable button based on cell value

    Thanks, the code was great. Just had to change from If/Or to If/And and it's great.

    Unfortunately now I have another issue.

    This is the code for the start button and the timer.

    Private Sub CommandButton1_Click()
    Dim StartTime, FinishTime, TotalTime, PauseTime
    StopIt = False
    ResetIt = False
    If Range("G6") = 0 Then
      StartTime = Timer
      PauseTime = 0
      LastTime = 0
    Else
      StartTime = 0
      PauseTime = Timer
    End If
    StartIt:
      DoEvents
      If StopIt = True Then
        LastTime = TotalTime
        Exit Sub
      Else
        FinishTime = Timer
        TotalTime = FinishTime - StartTime + LastTime - PauseTime
        TTime = TotalTime * 100
        HM = TTime Mod 100
        TTime = TTime \ 100
        hh = TTime \ 3600
        TTime = TTime Mod 3600
        MM = TTime \ 60
        SS = TTime Mod 60
        Range("G6").Value = Format(MM, "00") & ":" & Format(SS, "00") & "." & Format(HM, "00")
        If ResetIt = True Then
          Range("G6") = Format(0, "00") & ":" & Format(0, "00") & "." & Format(0, "00")
          LastTime = 0
          PauseTime = 0
          End
        End If
        GoTo StartIt
      End If
    End Sub
    Private Sub CommandButton2_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
      StopIt = True
    End Sub
    Is there any way to amend the code so that the timer continues to run whilst making changes to other cells in the worksheet?

  5. #5
    Registered User
    Join Date
    03-30-2016
    Location
    Manchester
    MS-Off Ver
    2013
    Posts
    93

    Re: hide/disable button based on cell value / keep timer running and update excel at same

    So by messing about I've noticed that if I'm entering text into a text box the timer continues to run.
    is it possible to have a situation where a formula in cell L14 looks for words in a text box that match a list of keywords in cells S1:S4, and returns a letter "P" if the words are there, or a "O" if not?


    Alternatively is there a way of changing the command button 1 code, or adding/changing any code to ensure that the timer does not stop when I edit cells in the worksheet?
    Last edited by dori2o; 11-09-2017 at 07:08 PM.

+ 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. VBA to hide or disable a form control button based on cell value
    By npsnps in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 01-11-2017, 06:40 AM
  2. [SOLVED] Disable or hide a recorded macro button when clicked if the worksheet is protected
    By sai19 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-31-2014, 12:14 PM
  3. Disable or Hide Command Button for certain users
    By cvishu in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-17-2014, 12:44 PM
  4. [SOLVED] Use API to disable or hide the Close Button or Caption Bar on UserForms
    By mc84excel in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 02-25-2014, 09:33 PM
  5. How to hide command button based on another cell?
    By proepert in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 01-27-2014, 09:28 AM
  6. Hide form button or rectangle based on cell value
    By jpruffle in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 05-14-2009, 10:58 AM
  7. Hide or disable the 'Close' button
    By 5631 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 12-18-2008, 12:36 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