[VBA] Losing my mind trying to simply add a range to drop down list

RockandGrohl

Well-known Member
Joined
Aug 1, 2018
Messages
790
Office Version
  1. 365
Platform
  1. Windows
Don't know why I'm finding this so difficult.

In Sheet "Data" I have a dynamic-length list from X2 downwards.

I want to add a data validation list box to K9 which contains this list. The contents & length of the list are dynamic.


Right now I'm doing this:

VBA Code:
Dim TourList As Range
LastrowAD = Cells(Rows.Count, "A").End(xlUp).Row

...

Range("W2:W" & LastrowAD).Value = Range("W2:W" & LastrowAD).Value
        Columns("W:W").Copy
        Range("X1").PasteSpecial xlPasteValues
        Application.CutCopyMode = False
        ActiveSheet.Range("X1:X" & LastrowAD).RemoveDuplicates Columns:=1, Header:=xlNo
        ActiveWorkbook.Worksheets("Data").Sort.SortFields.Clear
        ActiveWorkbook.Worksheets("Data").Sort.SortFields.Add Key:=Range("X2:X" & Lastrow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        With ActiveWorkbook.Worksheets("Data").Sort
            .SetRange Range("X1:X" & LastrowAD)
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    Range("X2").Activate
    Set TourList = Range("X2:X" & Cells(Rows.Count, "X").End(xlUp).Row)

The above takes the first character of column A and places it in column W, then gets a de-duped list in column X and sorts it A-Z. Finally, it makes that de-duped and sorted range a "Range" object (which for this instance is X2:X10)

Finally, I want to reference this list in K9:

VBA Code:
With Range("K9").Validation
        .Delete
        .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
        xlBetween, Formula1:=TourList
        .IgnoreBlank = True
        .InCellDropdown = True
        .InputTitle = ""
        .ErrorTitle = ""
        .InputMessage = ""
        .ErrorMessage = ""
        .ShowInput = True
        .ShowError = True
    End With


This then gives me Application-defined or Object defined error. The weirdest thing is that I seem to be on the right path according to here:



Thank you!
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
How about
VBA Code:
Formula1:="=tourlist"
 
Upvote 0
How about
VBA Code:
Formula1:="=tourlist"

....

So if I have it like this:

VBA Code:
Set TourList = Range("X2:X" & Cells(Rows.Count, "X").End(xlUp).Row)

And then write:

VBA Code:
With Range("K9").Validation
        .Delete
        .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
        xlBetween, Formula1:="=TourList"

I still get "Application defined or Object-defined error"
 
Upvote 0
Try...

VBA Code:
Formula1:="=" & TourList.Address(external:=True)

Hope this helps!
 
Upvote 0
Oops, I thought "TourList" was a named range, try it like
VBA Code:
Range("X2:X" & Cells(Rows.Count, "X").End(xlUp).Row).Name = "TourList"

With Range("K9").Validation
        .Delete
        .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
        xlBetween, Formula1:="=TourList"
        .IgnoreBlank = True
        .InCellDropdown = True
        .InputTitle = ""
        .ErrorTitle = ""
        .InputMessage = ""
        .ErrorMessage = ""
        .ShowInput = True
        .ShowError = True
    End With
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,215,059
Messages
6,122,918
Members
449,094
Latest member
teemeren

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