PC Review


Reply
Thread Tools Rate Thread

Adding Named ranged for each column...

 
 
robert.hatcher@l-3com.com
Guest
Posts: n/a
 
      30th Nov 2006
I’m trying to look through a header row and for each column after a
certain point (the first few columns do not require analysis), assign
named ranges to the column data using the first row as the name. I want
to do this for all the remaining columns. I have three problems in my
code. Three, that I can see that is 

At the line - For Each c In Selection, I think I should use “For Each
c In Selection” however, I receive the error: error 13 type mismatch

At the line - For Each c In Selection… I expect the following code
to act on each cell in the selection, in sequence… however, it
happens to the entire selection

Using the macro recorder I came up with
ActiveSheet.Names.Add Name:="DC_RES", RefersToR1C1:="=R1C7:R7C7"

Ive tweaked that to:
ActiveSheet.Names.Add Name:=c.Value, refersto c.address

But I don’t have a handle on the refers to Property

My code is:

Public Sub CreateNames()

Dim c
Dim srchRow


'designate header range
Range("a1").Select

Range(Selection, Selection.End(xlToRight)).Select

srchRow = Selection.Find(what:="DC_RES", After:=ActiveCell,
LookIn:=xlValues, LookAt:= _
xlPart, SearchOrder:=xlByColumns,
SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Select

srchRow = Range(Selection,
Selection.End(xlToRight)).Select

For Each c In Selection 'srchRow causes an error!


ActiveSheet.Names.Add Name:=c.Value, refersto c.address

Next

End Sub

any help will be appreciated
Robert

 
Reply With Quote
 
 
 
 
Scott
Guest
Posts: n/a
 
      30th Nov 2006
I didn't actually look through you code. (A bit lazy this morning...)

Here's a sample that does what you're trying to do. (I hard-coded it
to go from column C to F (3 to 6), but that's easy to fix) You'll
probably want to do something similar... determine the extent of your
range (which columns it covers), and just loop through "FOR i = Start
TO Finish" instead of the FOR EACH.

Sub Test()
Dim i As Long
Dim SheetName As String
Dim WS As Worksheet

SheetName = "Sheet1"
Set WS = Worksheets(SheetName)

For i = 3 To 6
ActiveWorkbook.Names.Add _
Name:=WS.Cells(1, i), _
RefersToR1C1:="=" & SheetName & "!C" & i
Next i

End Sub

Scott

(E-Mail Removed) wrote:
> I’m trying to look through a header row and for each column aftera
> certain point (the first few columns do not require analysis), assign
> named ranges to the column data using the first row as the name. I want
> to do this for all the remaining columns. I have three problems in my
> code. Three, that I can see that is 
>
> At the line - For Each c In Selection, I think I should use “For Each
> c In Selection” however, I receive the error: error 13 type mismatch
>
> At the line - For Each c In Selection… I expect the following code
> to act on each cell in the selection, in sequence… however, it
> happens to the entire selection
>
> Using the macro recorder I came up with
> ActiveSheet.Names.Add Name:="DC_RES", RefersToR1C1:="=R1C7:R7C7"
>
> Ive tweaked that to:
> ActiveSheet.Names.Add Name:=c.Value, refersto c.address
>
> But I don’t have a handle on the refers to Property
>
> My code is:
>
> Public Sub CreateNames()
>
> Dim c
> Dim srchRow
>
>
> 'designate header range
> Range("a1").Select
>
> Range(Selection, Selection.End(xlToRight)).Select
>
> srchRow = Selection.Find(what:="DC_RES", After:=ActiveCell,
> LookIn:=xlValues, LookAt:= _
> xlPart, SearchOrder:=xlByColumns,
> SearchDirection:=xlNext, MatchCase:= _
> False, SearchFormat:=False).Select
>
> srchRow = Range(Selection,
> Selection.End(xlToRight)).Select
>
> For Each c In Selection 'srchRow causes an error!
>
>
> ActiveSheet.Names.Add Name:=c.Value, refersto c.address
>
> Next
>
> End Sub
>
> any help will be appreciated
> Robert


 
Reply With Quote
 
robert.hatcher@l-3com.com
Guest
Posts: n/a
 
      1st Dec 2006
I ended up beating the code into working with this:

Dim srchRow
Dim SheetName As String
Dim cRng 'As Range
Dim cAdd

SheetName = ActiveSheet.Name

'designate header range
Range("a1").Select

Range(Selection, Selection.End(xlToRight)).Select

srchRow = Selection.Find(What:="DC_RES", After:=ActiveCell,
LookIn:=xlValues, LookAt:= _
xlPart, SearchOrder:=xlByColumns,
SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Select

srchRow = Range(Selection,
Selection.End(xlToRight)).Select

For Each c In Selection 'srchRow causes an error!
cAdd = c.Address
'cRng = Empty
cRng = Range(c.Offset(1, 0), c.End(xlDown)).Address
ActiveSheet.Names.Add Name:=c.Value, RefersTo:="=" & SheetName
& "!" & cRng

Next

Both of you offered much cleaner methods
Thanks for the help
Robert






Charles Chickering wrote:
> Dim c As Range
> For Each c In Selection.Cells
>
> Try that.
> --
> Charles Chickering
>
> "A good example is twice the value of good advice."
>
>
> "(E-Mail Removed)" wrote:
>
> > I’m trying to look through a header row and for each column after a
> > certain point (the first few columns do not require analysis), assign
> > named ranges to the column data using the first row as the name. I want
> > to do this for all the remaining columns. I have three problems in my
> > code. Three, that I can see that is 
> >
> > At the line - For Each c In Selection, I think I should use “For Each
> > c In Selection” however, I receive the error: error 13 type mismatch
> >
> > At the line - For Each c In Selection… I expect the followingcode
> > to act on each cell in the selection, in sequence… however, it
> > happens to the entire selection
> >
> > Using the macro recorder I came up with
> > ActiveSheet.Names.Add Name:="DC_RES", RefersToR1C1:="=R1C7:R7C7"
> >
> > Ive tweaked that to:
> > ActiveSheet.Names.Add Name:=c.Value, refersto c.address
> >
> > But I don’t have a handle on the refers to Property
> >
> > My code is:
> >
> > Public Sub CreateNames()
> >
> > Dim c
> > Dim srchRow
> >
> >
> > 'designate header range
> > Range("a1").Select
> >
> > Range(Selection, Selection.End(xlToRight)).Select
> >
> > srchRow = Selection.Find(what:="DC_RES", After:=ActiveCell,
> > LookIn:=xlValues, LookAt:= _
> > xlPart, SearchOrder:=xlByColumns,
> > SearchDirection:=xlNext, MatchCase:= _
> > False, SearchFormat:=False).Select
> >
> > srchRow = Range(Selection,
> > Selection.End(xlToRight)).Select
> >
> > For Each c In Selection 'srchRow causes an error!
> >
> >
> > ActiveSheet.Names.Add Name:=c.Value, refersto c.address
> >
> > Next
> >
> > End Sub
> >
> > any help will be appreciated
> > Robert
> >
> >


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Import Named Ranged Ed Microsoft Access Macros 7 19th Aug 2009 03:33 AM
Named Ranged, help please Simon Microsoft Excel Programming 3 11th Aug 2008 05:12 PM
Named ranged selection problem Martin B Microsoft Excel Worksheet Functions 3 4th Jan 2008 09:32 PM
Vlookup - Using a named ranged for col_index_num marin_michael@yahoo.ca Microsoft Excel Misc 2 1st Jun 2005 05:38 PM
Vlookup - Using a named ranged for col_index_num rocket_rob Microsoft Excel Worksheet Functions 3 10th Dec 2003 08:09 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:52 AM.