Using a Variable in Data Validation

music_al

Board Regular
Joined
Nov 26, 2008
Messages
133
Hi

I am passing a string to a string variable (which is correctly declared as a STRING) and the strings I am passing are the names of ranges in a worksheet. I would like to use the variable name in the data validation method...

Rich (BB code):
        With Selection.Validation
            .Delete
            .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
            xlBetween, Formula1:= VARIABLE NAME WOULD GO HERE
            .IgnoreBlank = True
            .InCellDropdown = True
        End With

I have tried several variations on this; putting it in speech marks, brackets, semicolon etc, but I just cant get it to work. Any help would be greatly appreciated.
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
You need an equals sign, so:

VBA Code:
Formula1:="=" & variableName
 
Upvote 0
When you want to highlight part of your vba code, use the RICH code tags, not the VBA code tags. I have changed that for you above

Try
VBA Code:
With Selection.Validation
    .Delete
    .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
      xlBetween, Formula1:="=" & StrVariable
    .IgnoreBlank = True
    .InCellDropdown = True
End With
 
Upvote 0
You're welcome. Thanks for the follow-up. :)

BTW, I suggest that you update your Account details (click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)
 
Upvote 0

Forum statistics

Threads
1,214,833
Messages
6,121,868
Members
449,054
Latest member
juliecooper255

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