View Single Post
  #6   Report Post  
Posted to microsoft.public.excel.programming
Chip Pearson Chip Pearson is offline
external usenet poster
 
Posts: 7,247
Default How to loop through all ranges in a worksheet

You can loop through all the names in a workbook with code like

Dim Nm As Name
For Each Nm In ThisWorkbook.Names
' do something with Nm
Next Nm

You can test whether a name exists with code like

Dim N As Integer
On Error Resume Next
N = Len(ThisWorkbook.Names("TheName").Name)
If N 0 Then
' name exists
Else
' name doesn't exist
End If
On Error Goto 0

Somethimes a name exists, but contains a #REF error because the
range to which it refered has been deleted. To test this
condition, use code like

Dim Nm As Name
Set Nm = ThisWorkbook.Names("TheName")
If InStr(Nm.RefersTo,"#REF") 0 Then
' name contains a #REF error
End If

--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com


wrote in message
...
You're right, that's not what I want it to do.

I have a named range and I want to test if it exists (i.e.
that nobody has deleted it accidentally.) So yes, I want
to be able to loop through all of the named ranges in
either the workbook or the worksheet. Is there a way to do
that? Thanks.

Nanette
-----Original Message-----
I think you don't understand what Count does.

========
Set validationWkSht = ThisWorkbook.Worksheets
("Validation")
With validationWkSht
For i = 0 To .Range.Count
<stuff
Next i
End With
========


The error message you are getting is because the VBE wants
you to define a Range.

i.e.
--------
..Range("A1:A5").Count
--------

Would return 5

Count does not return a count of Range Objects that have
been defined for that worksheet.


.