Forum Discussion

rbalza's avatar
rbalza
Brass Contributor
Jul 12, 2021
Solved

Cell as a button

Hi Friends,    What were the ways to make a cell a button? I was trying to count the text in a button but don't actually know how to do it. Thanks 🙂    
  • HansVogelaar's avatar
    HansVogelaar
    Jul 12, 2021

    rbalza 

    Right-click the sheet tab.

    Select 'View Code' from the context menu.

    I created a Worksheet_BeforeDoubleClick event procedure. This is run automatically each time you double-click a cell.

    Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
        If Not Intersect(Range("B2:B20"), Target) Is Nothing Then
            Cancel = True
            Select Case Target.Value
                Case "X"
                    Target.Value = "P"
                Case "O"
                    Target.Value = "X"
                Case Else
                    Target.Value = "O"
            End Select
        End If
    End Sub

    The code first checks whether the cell is in the range B2:B20.

    If so, it sets Cancel to True to prevent going into edit mode.

    Then it changes the value of the cell depending on the current value.

Resources