Insert Command Button to reset multiple Data Validation Dropdowns using VBA

Dellpanther

New Member
Joined
Oct 23, 2019
Messages
4
Hi all,

I have a single worksheet with 20+ data validation dropdowns. I want to insert a Command Button that will reset all the dropdown cells to the first option on the dropdown list when I click it. Having never used Macro/VBA before, I’ve tried researching the appropriate code to make this happen with zero success. Can someone please explain how to do this and include the appropriate code?

For the sake of simplicity, assume my worksheet is titled “SheetA” and data validations are in cells D19, D21... D31. I will update as necessary.

Thank you in advance!
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
How have you set up the lists in the data validation?

If it's simply by using something like 1,2,3,4,5 you could try this.
Code:
Sub ResetDV()
Dim rngDV As Range
Dim lst As Variant

    
    For Each rngDV In Sheets("SheetA").Cells.SpecialCells(XlCellType.xlCellTypeAllValidation)
        
        If rngDV.Validation.Type = xlValidateList Then
        
            lst = rngDV.Validation.Formula1
            
            rngDV.Value = Split(lst, ",")(0)
        End If
        
    Next rngDV
    
End Sub

If you've used a range for the list things will be a bit more complicated.:)
 
Upvote 0
Hi Norie, thanks for your reply! As I mentioned I am a complete novice when it comes to Macro/VBA...

When I am entering the code for my Command Button, the initial code that appears is:



Private Sub CommandButton1_Click()


End Sub



Where do I enter your code in relation to the above? Do I start a new code thread? Does it go in between Private Sub/End Sub?
 
Upvote 0
Try this.
Code:
Private Sub CommandButton1_Click()
Dim rngDV As Range
Dim lst As Variant

    
    For Each rngDV In Sheets("SheetA").Cells.SpecialCells(XlCellType.xlCellTypeAllValidation)
        
        If rngDV.Validation.Type = xlValidateList Then
        
            lst = rngDV.Validation.Formula1
            
            rngDV.Value = Split(lst, ",")(0)
        End If
        
    Next rngDV

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,179
Members
452,615
Latest member
bogeys2birdies

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top