PC Review


Reply
Thread Tools Rate Thread

Checking reference for named cells

 
 
Kevin
Guest
Posts: n/a
 
      28th Nov 2007
If I have several named cells in say 10 worksheets, and I delete sheet 8,
then I have some named ranged left out there with no valid reference.

How can I check the validity of a named cell in vba? I have this so far...

Sub test()
Dim Nam As Name
For Each Nam In ActiveWorkbook.Names

"Place code here to check if reference on name is good, if it isnt then
delete that name."

Next Nam
End Sub

Thanks in advance
 
Reply With Quote
 
 
 
 
Bernie Deitrick
Guest
Posts: n/a
 
      28th Nov 2007
Kevin,

Excel will automatically delete the name - it is the dependent cells formulas that will error out.
Try the macro below to find the #REF! errors.

HTH,
Bernie
MS Excel MVP

Sub FindMissingNameReferences()

Dim mySht As Worksheet
Dim myCell As Range
Dim err As String

For Each mySht In ActiveWorkbook.Worksheets
On Error GoTo NoErrs
For Each myCell In mySht.Cells.SpecialCells(xlCellTypeFormulas, 16)
err = CStr(myCell.Value)
If err = "Error 2023" Then
MsgBox myCell.Address(, , , True) & _
" has a reference to a missing name."
End If
Next myCell
NoErrs:
Resume nextSheet
nextSheet:

Next mySht
End Sub


"Kevin" <(E-Mail Removed)> wrote in message
news:0D78A8E4-D3BB-4E9E-A1BD-(E-Mail Removed)...
> If I have several named cells in say 10 worksheets, and I delete sheet 8,
> then I have some named ranged left out there with no valid reference.
>
> How can I check the validity of a named cell in vba? I have this so far...
>
> Sub test()
> Dim Nam As Name
> For Each Nam In ActiveWorkbook.Names
>
> "Place code here to check if reference on name is good, if it isnt then
> delete that name."
>
> Next Nam
> End Sub
>
> Thanks in advance



 
Reply With Quote
 
Joel
Guest
Posts: n/a
 
      28th Nov 2007
For Each Nam In ActiveWorkbook.Names
If Left(Nam.Value, 5) = "=#REF" Then
'put your code here
End If
Next Nam

"Kevin" wrote:

> If I have several named cells in say 10 worksheets, and I delete sheet 8,
> then I have some named ranged left out there with no valid reference.
>
> How can I check the validity of a named cell in vba? I have this so far...
>
> Sub test()
> Dim Nam As Name
> For Each Nam In ActiveWorkbook.Names
>
> "Place code here to check if reference on name is good, if it isnt then
> delete that name."
>
> Next Nam
> End Sub
>
> Thanks in advance

 
Reply With Quote
 
Kevin
Guest
Posts: n/a
 
      28th Nov 2007
thanks Joel, that is exactly what I came up with while poking around..

If Left(Nam, 5) = "=#REF" Then
Nam.Delete
End If

Bernie, I would think that if you deleted a sheet that the names would be
deleted also, but when I go to INSERT-NAME-DEFINE, they are still listed jst
with bad references. This was making other macros I had mess up as it was
still going through old names and giving me bogus values..

This will work for though... Thanks guys!!!

"Joel" wrote:

> For Each Nam In ActiveWorkbook.Names
> If Left(Nam.Value, 5) = "=#REF" Then
> 'put your code here
> End If
> Next Nam
>
> "Kevin" wrote:
>
> > If I have several named cells in say 10 worksheets, and I delete sheet 8,
> > then I have some named ranged left out there with no valid reference.
> >
> > How can I check the validity of a named cell in vba? I have this so far...
> >
> > Sub test()
> > Dim Nam As Name
> > For Each Nam In ActiveWorkbook.Names
> >
> > "Place code here to check if reference on name is good, if it isnt then
> > delete that name."
> >
> > Next Nam
> > End Sub
> >
> > Thanks in advance

 
Reply With Quote
 
Bernie Deitrick
Guest
Posts: n/a
 
      28th Nov 2007
Sorry - I mis-read your post, and was thinking about the error raised when a block of cells
containing a named range is deleted.

Bernie
MS Excel MVP


"Kevin" <(E-Mail Removed)> wrote in message
news:234C1F51-6D30-4FE6-8024-(E-Mail Removed)...

> Bernie, I would think that if you deleted a sheet that the names would be
> deleted also, but when I go to INSERT-NAME-DEFINE, they are still listed jst
> with bad references. This was making other macros I had mess up as it was
> still going through old names and giving me bogus values..



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reference cells in named range Luca Brasi Microsoft Excel Discussion 19 4th Nov 2007 08:25 PM
Checking for non-blank cells in named range =?Utf-8?B?QmFyYiBSZWluaGFyZHQ=?= Microsoft Excel Programming 2 13th Oct 2006 06:34 PM
Checking for non blank cells in named range =?Utf-8?B?QmFyYiBSZWluaGFyZHQ=?= Microsoft Excel Programming 1 13th Oct 2006 03:32 PM
Checking for non-blank cells in named range =?Utf-8?B?QmFyYiBSZWluaGFyZHQ=?= Microsoft Excel Programming 0 13th Oct 2006 02:42 PM
3 cells are named - how to refere to them in one reference field in a chart Marie J-son Microsoft Excel Charting 2 2nd Dec 2004 04:52 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:54 AM.