Hey All!
I want to click a button to reset all data validation drop downs to their default selection. I found this code online, but I've never used VBA before, and I'm a complete noob when it comes to it. I'm not even sure I'm pasting the code in the right area... should it go in a module, or the sheet with the data validations? I tried both, but neither work.
Here is the code I'm using - I have no idea what any of it means - I was hoping someone could assist with any edits I need to make to it, and assist where I need to paste it into?
Lastly, I also want the same button to clear some other fields. I've run simple macros in the past to do this, but is it possible to use the below code to clear the drop downs and also run a macro at the same time, with the same button?
Thanks in advance all!!!
Private Sub Test()
Dim rVal As Range
Dim rCell As Range
Dim rList As Range
On Error Resume Next
Set rVal = Cells.SpecialCells(xlCellTypeAllValidation)
On Error GoTo 0
If Not rVal Is Nothing Then
For Each rCell In rVal
rCell.ClearContents
With rCell.Validation
If .Type = xlValidateList Then
If Left(.Formula1, 1) = "=" Then
Set rList = ActiveWorkbook.Names(Right(.Formula1, Len(.Formula1) - 1)).RefersToRange
rCell.Value = rList.Cells(1, 1).Value
Else
rCell.Value = Left(.Formula1, InStr(1, .Formula1, ",") - 1)
End If
End If
End With
Next rCell
End If
End Sub
I want to click a button to reset all data validation drop downs to their default selection. I found this code online, but I've never used VBA before, and I'm a complete noob when it comes to it. I'm not even sure I'm pasting the code in the right area... should it go in a module, or the sheet with the data validations? I tried both, but neither work.
Here is the code I'm using - I have no idea what any of it means - I was hoping someone could assist with any edits I need to make to it, and assist where I need to paste it into?
Lastly, I also want the same button to clear some other fields. I've run simple macros in the past to do this, but is it possible to use the below code to clear the drop downs and also run a macro at the same time, with the same button?
Thanks in advance all!!!
Private Sub Test()
Dim rVal As Range
Dim rCell As Range
Dim rList As Range
On Error Resume Next
Set rVal = Cells.SpecialCells(xlCellTypeAllValidation)
On Error GoTo 0
If Not rVal Is Nothing Then
For Each rCell In rVal
rCell.ClearContents
With rCell.Validation
If .Type = xlValidateList Then
If Left(.Formula1, 1) = "=" Then
Set rList = ActiveWorkbook.Names(Right(.Formula1, Len(.Formula1) - 1)).RefersToRange
rCell.Value = rList.Cells(1, 1).Value
Else
rCell.Value = Left(.Formula1, InStr(1, .Formula1, ",") - 1)
End If
End If
End With
Next rCell
End If
End Sub