Solved

Define Names in  a Loop

Posted on 2011-03-05
5
409 Views
Last Modified: 2012-05-11
Hi
See attached.
The sheet explains  my query.

Thanks !
DefineNamesQuery.zip
0
Comment
Question by:Patrick O'Dea
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 24

Accepted Solution

by:
StephenJR earned 400 total points
ID: 35043917
Do you meant like this?
Sub x()

Dim r As Range

For Each r In Range("A2", Range("A2").End(xlDown))
    r.Offset(, 1).Name = "Customer_" & r
Next r

End Sub

Open in new window

0
 
LVL 45

Expert Comment

by:patrickab
ID: 35043990
21Dewsbury,

Please state your question rather than expect people to open a file to discover what your question really is.

Patrick
0
 

Assisted Solution

by:Patrick O'Dea
Patrick O'Dea earned 0 total points
ID: 35044439
Point taken Patrickab,
However, I felt that in this case a "picture paints a thousand words".

In other words it was easier to understand by viewing the sheet rather than a more lengthy (and confusing?) written explanation.
Perhaps this does not suit all.
0
 
LVL 14

Expert Comment

by:Zack Barresse
ID: 35045012
Rather than keep invoking the name method of the workbook, you can test it's existence first.  On a small operation you may not notice a difference, but could over large data sets.
Sub NameMyRangesPlease()
    Dim WS As Worksheet, rCell As Range
    Set WS = ThisWorkbook.Sheets("Sheet1")
    For Each rCell In WS.Range("A2", WS.Cells(WS.Rows.Count, 1).End(xlUp))
        If NameExists("Customer_" & rCell.Value) = False Then
            rCell.Offset(0, 1).Name = "Customer_" & rCell.Value
        End If
    Next rCell
End Sub

Function NameExists(sRangeName As String) As Boolean
    On Error Resume Next
    NameExists = Len(ThisWorkbook.Names(sRangeName).Name) <> 0
End Function

Open in new window

0
 

Author Closing Comment

by:Patrick O'Dea
ID: 35107032
Thanks StephenJR,

That's perfect.

Now I will have to ensure that I can understand the code.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question