VBA to populate data validation drop down

SloFlyer

New Member
Joined
Feb 1, 2005
Messages
16
I'm looking for a macro that executes when I open my spreadsheet. I need it to create a list of my tab names that I can use as my data validation drop down list. I have found that if I have a range name containing the tab names, I can use it in my data validation list (for example typeing in "=tabnames" in the list range of the data validation selection. I would prefer to have the list residing in my range names as opposed to on a sheet somewhere. Any advice is welcome.
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
The only way that I can come up with that will do what you want is to build the list of worksheet names within the validation list rather than refering to a range. This keeps you from needing to read it from a worksheet. The following code will build a validation list consisting of the sheet names for the currently selected cell:

Code:
Sub test()

FormulaBuilder = ""

For i = 1 To Sheets.Count
FormulaBuilder = FormulaBuilder & Sheets(i).Name & ","
Next
'strip off ending comma
FormulaBuilder = Left(FormulaBuilder, Len(FormulaBuilder) - 1)

With Selection.Validation
        .Delete
       .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
        xlBetween, Formula1:=FormulaBuilder
        .IgnoreBlank = True
        .InCellDropdown = True
        .InputTitle = ""
        .ErrorTitle = ""
        .InputMessage = ""
        .ErrorMessage = ""
        .ShowInput = True
        .ShowError = True
End With

End Sub

Good Luck.

Owen
 
Upvote 0

Forum statistics

Threads
1,223,956
Messages
6,175,611
Members
452,660
Latest member
Zatman

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