Need help with my code. Thanks

pedie

Well-known Member
Joined
Apr 28, 2010
Messages
3,875
Hi everyone!
I have made a sample code that is only for col C:C.
In row 3 i have heading that I want it to be a range name. eg like help_1 etc
Whatever word in there in row 3 for each col i want the code to pick that up and then name the range select the whole colyumn....
The code will continue to name range untill the 3 row (moving column wise to right) is empty...
Thanks alot for helping!
Pedie;)

Sample code below! works only for one col:biggrin:

Code:
Sub try1()
Sheet1.Select
    Columns("C:C").Select
    ActiveWorkbook.Names.Add Name:=Range("C3").Value, RefersToR1C1:="=Sheet1!C3"
End Sub
 
pedie

Do you mean you want to use the values in row 3 of each column to create a named range that refers to that column and/or all the data in it?

PS The code you posted only works for one cell as far as I can see.:)
 
Upvote 0
Norie, that is correct.
Value in row 3 will be used as a name for that col.
If there is no value in row 3 then quit the code or find next row3.value (move right >>>) and do the same for each col....
 
Upvote 0
Something like this perhaps.
Code:
Sub CreateNames()
Dim rng As Range
 
    Set rng = Range("C3")
 
    While rng.Value <> ""
        Range(rng.Offset(1), rng.End(xlDown)).Name = rng.Value
        Set rng = rng.Offset(, 1)
    Wend
 
End Sub
By the way there is a built in method for doing this via Insert>Names... but I always forget exactly how it works.:)

Found code for it though.
Code:
    Range("C3").CurrentRegion.CreateNames Top:=True, Left:=False, Bottom:=False, Right:=False
 
Last edited:
Upvote 0
Something like this perhaps.
Code:
Sub CreateNames()
Dim rng As Range
 
    Set rng = Range("C3")
 
    While rng.Value <> ""
        Range(rng.Offset(1), rng.End(xlDown)).Name = rng.Value
        Set rng = rng.Offset(, 1)
    Wend
 
End Sub
By the way there is a built in method for doing this via Insert>Names... but I always forget exactly how it works.:)

Found code for it though.
Code:
    Range("C3").CurrentRegion.CreateNames Top:=True, Left:=False, Bottom:=False, Right:=False


So kind of you Norie;)...
Code:
'i change it to this -2...so it will select the whole col...lol...the first code itself is perfect...thanks again
Range(rng.Offset(-2), rng.End(xlDown)).Name = rng.Value
 
Upvote 0

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top