I am trying to delete all of the contents of certain named ranges under certain criteria. Specifically, if the name of the range ends in "CA", then all of the contents in the cells composing that range should be cleared. This is what I have so far:
For Each Nm in ActiveWorkbook.Names
If Right(Nm.Name, 2) = "CA" Then
Range(Nm.Name).ClearContents
End If
Next Nm
This produces an error:
Method 'Range' of object '_Global' failed
I've also tried to take off the .Name
and just use:
Range(Nm).ClearContents
but that gives the same error. Providing some sort of workbook and sheet reference gives the error:
Application-defined or object-defined error
Changing .ClearContents
to .Clear
does nothing.
I know it's finding the range because it's getting passed the if-statement
.
Am I missing something silly?
EDIT: For what it's worth, the named range in question is in another workbook from where the code is written that is opened in compatibility mode.
Nm
shows that it is a valid range. – Matt Cremeensname.name
will get you the range name but you still just want to clear the named rangerange(nm)
– Davesexcel