Solved

Define Names in  a Loop

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

Thanks !
DefineNamesQuery.zip
0
Comment
Question by:Patrick O'Dea
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

808 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