Naming a range using a macro

immdav

New Member
Joined
Jul 23, 2011
Messages
10
I would like to name a column or unique numbers using a macro.
I tried simply recording a macro (I tried this both as a absolute and relative macro) selecting cell C2 and doing <CTRL> <shift> down arrow.
When I tested the macro out it always name a range of cells with the same length of cell as the spread sheet that I originally recorded the macro on. If the spreadsheet I recorded the macro on had 277 rows of data and I ran the macro on the sheet that has 300 rows of data the range it would pick is c2:c277 instead of c2:c300.

I do know a bit of VBA but a bit of a beginner in VBA.
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Try this code
Code:
Sub Test()
Dim LR As Long
LR = Range("c" & Rows.Count).End(xlUp).Row
ActiveWorkbook.Names.Add Name:="MyRng", RefersTo:=Range("c2:c" & LR)
End Sub
 
Upvote 0
Or:

Code:
Sub Macro1()
    ActiveWorkbook.Names.Add Name:="MyRng", RefersTo:=Range([C2], [C2].End(xlDown))
End Sub
 
Upvote 0
There's no need for a macro. A dynamic named range will work.

Name: myRange
RefersTo: =INDEX(Sheet1!$C:$C,2,1):INDEX(Sheet1!$C:$C, MATCH("zzzz", Sheet1!$C:$C), 1)


if C:C contains numbers rather than text, substitute 1E+308 for "zzzz".
 
Upvote 0

Forum statistics

Threads
1,223,102
Messages
6,170,122
Members
452,303
Latest member
c4cstore

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