VBA .Validation.Add run-time error 1004

JackDanIce

Well-known Member
Joined
Feb 3, 2010
Messages
9,922
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have the following code which is giving a run-time error message, though was working fine earlier.

Formula1 (List_Status)is a named range which hasn't changed location,address or values. The range value when error occurs is found in List_Status

Line of error (red), rng.address is A19 and rng.parent.name is name of the activesheet this occurs on
Rich (BB code):
Public Sub Add_Validation(ByRef rng as Range)

With Application
  .ScreenUpdating = False
  .EnableEvents = False
End With

With rng        
  If Len(.Value) > 0 Then
    .Validation.Delete
    .Validation.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:="=List_Status"
  End If
End With

With Application
  .ScreenUpdating = True
  .EnableEvents = True
End With

End Sub
Searching online seems to generally suggest using .Validation.Delete prior to validation.add but I already have this line so unclear as cause of error. Excel 2013

Any ideas?

Thanks in advance,
Jack
 
Last edited:

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Are you certain that List_Status is a valid range at the time?
 
Upvote 0
Yes, in the debug window I received a result of 3 to:
Code:
? Application.Match(rng.Value, Range("List_Status"), 0)
3
 
Upvote 0
What's the scope of List_Status? Workbook, or worksheet?
 
Upvote 0
Workbook, though I tried changing the formula to "=Admin!List_Status" (It's on sheet "Admin") and still same error.
 
Upvote 0
No, I'm checking through previous subs/functions and events for sheet protection, only other thing I can think of. And before clicking reply - I just checked, the sheet property is showing it's protected, I'm sure it wasn't when code first errored today.

Changed code to force unlock before the .Validation.Delete line and it's fixed it.

Thanks for the replies and help RoryA
 
Upvote 0

Forum statistics

Threads
1,214,657
Messages
6,120,777
Members
448,991
Latest member
Hanakoro

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