Forum Discussion

JamFestival's avatar
JamFestival
Copper Contributor
Oct 06, 2021
Solved

Getting a range of cells to act like radio buttons (True to revert to false if true in another cell)

I am trying to use data validation to achieve something quite simple.
I would like to apply data validation to a range of cells, so that when TRUE is entered in one of the range, the rest revert to FALSE - like radio buttons.

Can this be done?
Thanks in advance.

  • JamFestival 

    Data Validation limits what the user can enter in a cell, but it cannot change the value of a cell automatically. You need VBA for that.

    Let's say the range is C2:C12.

    Right-click the sheet tab.

    Select 'View Code' from the context menu.
    Copy the code listed below into the code window.
    Close the Visual Basic Editor.
    Save the workbook as a macro-enabled workbook (*.xlsm).
    Make sure that you allow macros when you open it.
    Code:

    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim rng As Range
        If Not Intersect(Range("C2:C12"), Target) Is Nothing Then
            Set rng = Intersect(Range("C2:C12"), Target)(1)
            If rng.Value = True Then
                Application.ScreenUpdating = False
                Application.EnableEvents = False
                Range("C2:C12").Value = False
                rng.Value = True
                Application.EnableEvents = True
                Application.ScreenUpdating = True
            End If
        End If
    End Sub

2 Replies

  • JamFestival 

    Data Validation limits what the user can enter in a cell, but it cannot change the value of a cell automatically. You need VBA for that.

    Let's say the range is C2:C12.

    Right-click the sheet tab.

    Select 'View Code' from the context menu.
    Copy the code listed below into the code window.
    Close the Visual Basic Editor.
    Save the workbook as a macro-enabled workbook (*.xlsm).
    Make sure that you allow macros when you open it.
    Code:

    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim rng As Range
        If Not Intersect(Range("C2:C12"), Target) Is Nothing Then
            Set rng = Intersect(Range("C2:C12"), Target)(1)
            If rng.Value = True Then
                Application.ScreenUpdating = False
                Application.EnableEvents = False
                Range("C2:C12").Value = False
                rng.Value = True
                Application.EnableEvents = True
                Application.ScreenUpdating = True
            End If
        End If
    End Sub
    • JamFestival's avatar
      JamFestival
      Copper Contributor

      HansVogelaar 
      Thank you so much!
      I need multiple instances of this, and repeating the IF... to ENDIF... sections accomplished this.
      Excellent!

Resources