Logo 
Search:

MS Office Answers

Ask Question   UnAnswered
Home » Forum » MS Office       RSS Feeds
  Question Asked By: Erica Matthews   on Nov 30 In MS Office Category.

  
Question Answered By: Tiaan Thompson   on Nov 30

As far as I can tell, "label name" is a meaningless term. From his description,
it sounds like Jon is just using creating range names  based on labels in
adjacent cells.

Although the labels are not in the Names collection, the range names created
using them are. We just need to find every named range which has a label
above/below/left/right which matches the range name. That could be a range which
was created using Insert >> Name >> Create. The following macro will identify
all such ranges:

Sub TestRngNames()
Dim n As Name, Sht As Worksheet, Rng As Range
On Error Resume Next
For Each n In ActiveWorkbook.Names
Set Sht = Sheets(Mid(n.RefersTo, 2, (InStr(1, n.RefersTo, "!") - 2)))
Set Rng = Sht.Range(Right(n.RefersTo, (Len(n.RefersTo) - (InStr(1,
n.RefersTo, "!")))))
If Rng.Row > 1 Then
If Cells(Rng.Row - 1, Rng.Column).Value = n.Name Then
Range(Cells(Rng.Row - 1, Rng.Column), Cells(Rng.Row +
Rng.Rows.Count - 1, Rng.Column + Rng.Columns.Count - 1)).Select
MsgBox "Found " & n.Name & " label  ABOVE range. Label is in row
" & Rng.Row - 1 & ", column " & Rng.Column
End If
End If
If (Rng.Row + Rng.Rows.Count) <= Rows.Count Then
If Cells(Rng.Row + Rng.Rows.Count, Rng.Column).Value = n.Name Then
Range(Cells(Rng.Row, Rng.Column), Cells(Rng.Row +
Rng.Rows.Count, Rng.Column + Rng.Columns.Count - 1)).Select
MsgBox "Found " & n.Name & " label BELOW range. Label is in row
" & Rng.Row + Rng.Rows.Count & ", column " & Rng.Column
End If
End If
If Rng.Column > 1 Then
If Cells(Rng.Row, Rng.Column - 1).Value = n.Name Then
Range(Cells(Rng.Row, Rng.Column - 1), Cells(Rng.Row +
Rng.Rows.Count - 1, Rng.Column + Rng.Columns.Count - 1)).Select
MsgBox "Found " & n.Name & " label to LEFT of range. Label is in
row " & Rng.Row & ", column " & (Rng.Column - 1)
End If
End If
If (Rng.Column + Rng.Columns.Count) <= Columns.Count Then
If Cells(Rng.Row, Rng.Column + Rng.Columns.Count).Value = n.Name
Then
Range(Cells(Rng.Row, Rng.Column), Cells(Rng.Row + Rng.Rows.Count
- 1, Rng.Column + Rng.Columns.Count)).Select
MsgBox "Found " & n.Name & " label to RIGHT of range. Label is
in row " & Rng.Row & ", column " & (Rng.Column + Rng.Columns.Count)
End If
End If
Next n
Set Sht = Nothing
Set Rng = Nothing
End Sub

At the point when the range is selected, code could be added to copy  the range
to wherever it goes on the new sheet (I am assuming Jon wants to copy the entire
range, including the label).

Is the desired result of Jon's request to have the same named ranges on
multiple worksheets in the same workbook? Why not just refer to the ranges on
the first sheet, or move them all to a separate sheet for easier maintenance?

Share: 

 

This Question has 8 more answer(s). View Complete Question Thread

 
Didn't find what you were looking for? Find more on Label Names Or get search suggestion and latest updates.


Tagged: