VBA List Validation for Dynamic Range

3link

Board Regular
Joined
Oct 15, 2010
Messages
138
I have a cell within my spreadsheet that will conditionally have data validation properties based on user input. The validation property will be a list with numbers ranging between 0+. The range of the list will depend on user input. As the list will contain only whole numbers, I was wondering if there is a way I can simply set a min and max value and have excel fill-in the numbers between. Something like:

Code:
    Range("A1").Select

        With Selection.Validation
        .Delete
        .Add Type:=xlValidateList, _
        AlertStyle:=xlValidAlertStop, Operator:= _
        xlBetween, Formula1:="0 To 51"

That way, I could simply set the max to follow the value of a certain cell:
Code:
  xlBetween, Formula1:="0 To Range("A1").value

Is this possible? If not, is there a way to convert this"

Code:
=OFFSET($J$3,0,0,(ROWS($J$3:$J$25)-COUNTBLANK($J$3:$J$25)),1)
from Excel to VBA code? That would be my plan b.
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Nevermind. I think I figred it out. I have a range of numbers J3 - J25. Each cells contains a number 0 - 22. However, each cell is programed to turn null if it exceeds the max (contained in Cell J2).

Code:
=IF(1>$J$2, "", 1)

Then I have a named range for these (named "MyRange"):

Code:
=OFFSET($J$3,0,0,(ROWS($J$3:$J$25)-COUNTBLANK($J$3:$J$25)),1)

And the VBA code is:
Code:
Sub ProgramValidate()
 With Range("A1").Validation
 .Delete
 .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
 Operator:=xlBetween, Formula1:="=MyRange"
 .ErrorMessage = "Invalid value. Select one from the dropdown list."
 .InCellDropdown = True
 End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,616
Messages
6,179,911
Members
452,949
Latest member
beartooth91

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