Solved

Define Names in  a Loop

Posted on 2011-03-05
5
368 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks StephenJR,

That's perfect.

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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

A2 = A1 That kind of cell reference is relative.  If you copy it from A2 to B2, then B2 will get this: B2 = B1 That's all fine and good, but if you then insert a new row above row 2, you'll find: A3 = A1 B3 = B1 This is intentional. …
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…
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

772 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now