Oct 06 2021 01:18 AM
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.
Oct 06 2021 01:57 AM
SolutionData 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
Oct 07 2021 02:13 AM
@Hans Vogelaar
Thank you so much!
I need multiple instances of this, and repeating the IF... to ENDIF... sections accomplished this.
Excellent!
Oct 06 2021 01:57 AM
SolutionData 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