Solved

Define Names in  a Loop

Posted on 2011-03-05
5
382 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…

862 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

24 Experts available now in Live!

Get 1:1 Help Now