Error applying validation list to cell based on a named range

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,564
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I am running into an error when I'm trying to apply a validation list to a cell based on the values of a named range (nr_crwlist).

Rich (BB code):
Set crwlist = ws_fmatemp.Range("CJ29:CJ" & rw - 1)
crwlist = crwlist
ThisWorkbook.Names.Add Name:="nm_crwlist", RefersTo:=crwlist

For f = 9 To 636
    If ws_fmagui.Cells(f, 23).Interior.Color = RGB(221, 235, 247) Then
        ws_fmagui.Cells(f, 26).Validation.Add Type:=xlValidateList, Formula1:="=nm_crwlist"
    End If
Next f

The line in red breaks with an error: "Application-defined or object-defined error"

Thoughts?
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Perhaps this.

VBA Code:
    Set crwlist = ws_fmatemp.Range("CJ29:CJ" & rw - 1)

    For f = 9 To 636
        If ws_fmagui.Cells(f, 23).Interior.Color = RGB(221, 235, 247) Then
            With ws_fmagui.Cells(f, 26).Validation
                .Delete
                .Add Type:=xlValidateList, Formula1:="=" & crwlist.Address
            End With
        End If
    Next f
 
Upvote 0
Thank you so much rlv01 for your help.

Rich (BB code):
For f = 9 To 636
    If ws_fmagui.Cells(f, 23).Interior.Color = RGB(221, 235, 247) Then
        With ws_fmagui.Cells(f, 26).Validation
            .Delete
            .Add Type:=xlValidateList, Formula1:="=" & crwlist.Address
        End With
    End If
 Next f

The line in red breaks with an "Object required" error.
 
Upvote 0
The RefersTo argument takes a String, not a Range. Did you check after running your code to see if the name was created correctly?

Change it to:
VBA Code:
ThisWorkbook.Names.Add Name:="nm_crwlist", RefersTo:="=" & crwlist.Address

Also, by the way, this line
VBA Code:
crwlist = crwlist
has absolutely no effect.

The rest of your code is OK.

Edit: The solution from rlv01 will give you the correct data validation, but the name nm_crwlist will still not be created correctly.
 
Upvote 0
Hi Jeff,
Thank you. That resolved that error.
However, despite range ws_fmatemp.range("CJ29:CJ39") holding values, the validation list is empty. When I check name manager to see if the named range nm_crwlist was created properly, the value is "#NAME?" and Refers to is =crwlist.

Something still isn't right.
 
Upvote 0
Try
VBA Code:
Set crwlist = ws_fmatemp.Range("CJ29:CJ" & rw - 1)
crwlist = crwlist
crwlist.Name = "nm_crwlist"
 
Upvote 0
Thank you Mike! That corrected the "#NAME?" error and the reference in the name manager, but my validation list is still empty at the cell.

Code:
    Set crwlist = ws_fmatemp.Range("CJ29:CJ" & rw - 1)
    'ThisWorkbook.Names.Add Name:="nm_crwlist", RefersTo:="=crwlist"
    crwlist.Name = "nm_crwlist"
    
    For f = 9 To 636
        If ws_fmagui.Cells(f, 23).Interior.Color = RGB(221, 235, 247) Then
            With ws_fmagui.Cells(f, 26).Validation
                .Delete
                .Add Type:=xlValidateList, Formula1:="=" & crwlist.Address
            End With
        End If
    Next f
 
Upvote 0
Thank you so much rlv01 for your help.

VBA Code:
For f = 9 To 636
    If ws_fmagui.Cells(f, 23).Interior.Color = RGB(221, 235, 247) Then
        With ws_fmagui.Cells(f, 26).Validation
            .Delete
           [COLOR=rgb(184, 49, 47)] .Add Type:=xlValidateList, Formula1:="=" & crwlist.Address[/COLOR]
        End With
    End If
 Next f

The line in red breaks with an "Object required" error.
I tested the code before I posted it, and it works if crwlist is a valid range and contains your validation list. I suspect at some point, this line

VBA Code:
Set crwlist = ws_fmatemp.Range("CJ29:CJ" & rw - 1)

produces an invalid range, which goes back to @mikerickson's question about what the value of rw is when it breaks.
 
Upvote 0
As the list is on a different sheet you need
VBA Code:
.Add Type:=xlValidateList, Formula1:="=" & crwlist.Address(, , , True)
 
Upvote 0
Solution

Forum statistics

Threads
1,214,965
Messages
6,122,495
Members
449,088
Latest member
Melvetica

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