Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,872
Default Macro to Create Name Ranges for variable lengths of Data

Hello Rashid,

Am Wed, 15 Jun 2011 10:40:25 -0700 (PDT) schrieb prkhan56:

I am using Excel 2007 and have a following problem. I have several
hundred cols of Data with variable entries.

for eg

City Town State ... ...... ...... ...... ....
Dubai Karama UAE
Sharjah Rolla
Ajman

The macro when run should create range name for all active area in the
sheet (until the last col) using the Header Row (eg. City, Town,
State, ...., ......., ......., .........).


try:
Sub myNames()
Dim LCol As Integer
Dim LRow As Long
Dim i As Integer

With ActiveSheet
LCol = .Cells(1, Columns.Count).End(xlToLeft).Column
For i = 1 To LCol
LRow = .Cells(Rows.Count, i).End(xlUp).Row
ActiveWorkbook.Names.Add Name:=.Cells(1, i), _
RefersTo:=.Range(.Cells(1, i), .Cells(LRow, i))
Next
End With
End Sub


Regards
Claus Busch
--
Win XP PRof SP2 / Vista Ultimate SP2
Office 2003 SP2 /2007 Ultimate SP2
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 24
Default Macro to Create Name Ranges for variable lengths of Data

On Jun 15, 10:02*pm, Claus Busch wrote:
Hello Rashid,

Am Wed, 15 Jun 2011 10:40:25 -0700 (PDT) schrieb prkhan56:

I am using Excel 2007 and have a following problem. *I have several
hundred cols of Data with variable entries.


for eg


City * * * Town * * * State *... * *...... * *....... * *...... * ....
Dubai * * Karama * UAE
Sharjah *Rolla
Ajman


The macro when run should createrangenamefor all active area in the
sheet (until the last col) using the Header Row (eg. City, Town,
State, ...., ......., ......., *.........).


try:
Sub myNames()
Dim LCol As Integer
Dim LRow As Long
Dim i As Integer

With ActiveSheet
* * LCol = .Cells(1, Columns.Count).End(xlToLeft).Column
* * For i = 1 To LCol
* * * * LRow = .Cells(Rows.Count, i).End(xlUp).Row
* * * * ActiveWorkbook.Names.AddName:=.Cells(1, i), _
* * * * * * RefersTo:=.Range(.Cells(1, i), .Cells(LRow, i))
* * Next
End With
End Sub

Regards
Claus Busch
--
Win XP PRof SP2 / Vista Ultimate SP2
Office 2003 SP2 /2007 Ultimate SP2


Hello Claus
Thanks for the code.
Can we have a check (similar to the code on Contextures) given below
to look for Blanks and Col Header with spaces

' if a column header contains spaces,
' replace the space with an underscore
' spaces are not allowed in range names.
myName = Replace(Cells(Rowno, i).Value, " ", "_")
If myName = "" Then
' if column header is blank, warn the user and
' stop the macro at that point
' names will only be created for those cells with text in
them.

Regards
Rashid
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,872
Default Macro to Create Name Ranges for variable lengths of Data

Hi Rashid,

try:

Sub myNames()
Dim LCol As Integer
Dim LRow As Long
Dim i As Integer
Dim rngName As String

With ActiveSheet
LCol = .Cells(1, Columns.Count).End(xlToLeft).Column
For i = 1 To LCol
If .Cells(1, i) = "" Then
MsgBox "Enter a header in column " & i _
& " run macro again", vbOKOnly
Exit For
End If
LRow = .Cells(Rows.Count, i).End(xlUp).Row
rngName = Replace(.Cells(1, i), " ", "_")
ActiveWorkbook.Names.Add Name:=rngName, _
RefersTo:=.Range(.Cells(1, i), .Cells(LRow, i))
Next
End With
End Sub


Regards
Claus Busch
--
Win XP PRof SP2 / Vista Ultimate SP2
Office 2003 SP2 /2007 Ultimate SP2
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 24
Default Macro to Create Name Ranges for variable lengths of Data

On Jun 16, 12:27*am, Claus Busch wrote:
Hi Rashid,

try:

Sub myNames()
Dim LCol As Integer
Dim LRow As Long
Dim i As Integer
Dim rngName As String

With ActiveSheet
* * LCol = .Cells(1, Columns.Count).End(xlToLeft).Column
* * For i = 1 To LCol
* * * * If .Cells(1, i) = "" Then
* * * * * * MsgBox "Enter a header in column " & i _
* * * * * * * * & " run macro again", vbOKOnly
* * * * * * * * Exit For
* * * * End If
* * * * LRow = .Cells(Rows.Count, i).End(xlUp).Row
* * * * rngName = Replace(.Cells(1, i), " ", "_")
* * * * ActiveWorkbook.Names.Add Name:=rngName, _
* * * * * * RefersTo:=.Range(.Cells(1, i), .Cells(LRow, i))
* * Next
End With
End Sub

Regards
Claus Busch
--
Win XP PRof SP2 / Vista Ultimate SP2
Office 2003 SP2 /2007 Ultimate SP2


Perfect.
You are a hero.
Thanks a lot
Rashid
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Create List sourced from Multiple sheets of varying lengths desertSol New Users to Excel 1 October 21st 09 10:05 PM
sorting ranges of various column lengths, lookup values in columns mrphinney Excel Worksheet Functions 0 May 14th 09 07:51 PM
Economical grouping of lengths out of much longer lengths Richard (a Builder not a Mathematician) Excel Worksheet Functions 1 January 19th 08 10:28 PM
Counting variable ranges and auto-summing variable ranges Father Guido[_5_] Excel Programming 2 March 29th 06 04:07 AM
Validation List with Variable Lengths & Invisible Empty Cells RobPendulum Excel Programming 3 December 7th 03 01:30 PM


All times are GMT +1. The time now is 12:45 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"