View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Dave Peterson Dave Peterson is offline
external usenet poster
 
Posts: 35,218
Default Define Names in Workbook through Code

Since the data is laid out nicely, I'd just count starting at the first column
and ending at the last.

I used column U as the first and BA as the last.

Option Explicit
Option Base 0
Sub testme()

Dim myRngs As Variant
Dim iCtr As Long
Dim rCtr As Long
Dim myNames As Variant
Dim myStep As Long

myNames = Array("Name01", "Name02", "Name03", "Name04", "Name05")

ReDim myRngs(LBound(myNames) To UBound(myNames))

For rCtr = LBound(myRngs) To UBound(myRngs)
Set myRngs(rCtr) = Nothing
Next rCtr

myStep = UBound(myNames) - LBound(myNames) + 1
With ActiveSheet
For iCtr = .Range("u1").Column To .Range("BA1").Column Step myStep
For rCtr = LBound(myRngs) To UBound(myRngs)
If myRngs(rCtr) Is Nothing Then
Set myRngs(rCtr) = .Cells(1, iCtr + rCtr)
Else
Set myRngs(rCtr) _
= Union(myRngs(rCtr), .Cells(1, iCtr + rCtr))
End If
Next rCtr
Next iCtr
End With

For rCtr = LBound(myRngs) To UBound(myRngs)
myRngs(rCtr).EntireColumn.Name = myNames(rCtr)
Next rCtr

End Sub


Arnold wrote:

Hi Gurus,

I'm trying to find a way for Excel to lump various columns into
different named ranges based on the names or headings of the columns.
There will be five different types of columns that will repeat
horizontally, eventually extending to about column 200, and each column
with the same heading should be included in its corresponding named
range.

Instead of manually having to do something like...
Range("U:U,Y:Y").Select
Range("U:U,Y:Y,AC:AC").Select
Range("U:U,Y:Y,AC:AC,AJ:AJ,AO:AO").Select
Range("U:U,Y:Y,AC:AC,AJ:AJ,AO:AO,AS:AS,AW:AW").Sel ect
Range("U:U,Y:Y,AC:AC,AJ:AJ,AO:AO,AS:AS,AW:AW,BA:BA ").Select

Can Excel do it automatically?

Thanks,
Arnold


--

Dave Peterson