Use VBA to name Ranges

G

Guest

Guest
Hi guys,
I recorded this macro..
ActiveWorkbook.Names.Add Name:="DATABASE2060", RefersToR1C1:= _
"='2060_DB'!R1C1:R8C9"

Now, I am trying to achieve the same result using VBA.

My code

Set LastCell = ActiveSheet.Range("I65536").End(xlUp)
SV_Code = 2060
Reference = "='" & SV_Code & "_DB!R1C1:" & LastCell.Address(ReferenceStyle:=xlR1C1)

ActiveWorkbook.Names.Add Name:="DATABASE2060", RefersToR1C1:= _
Reference

Running the vba code doesn't achieve the same result. Help guys
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
On 2002-03-07 17:13, Anonymous wrote:
Hi guys,
I recorded this macro..
ActiveWorkbook.Names.Add Name:="DATABASE2060", RefersToR1C1:= _
"='2060_DB'!R1C1:R8C9"

Now, I am trying to achieve the same result using VBA.

My code

Set LastCell = ActiveSheet.Range("I65536").End(xlUp)
SV_Code = 2060
Reference = "='" & SV_Code & "_DB!R1C1:" & LastCell.Address(ReferenceStyle:=xlR1C1)

ActiveWorkbook.Names.Add Name:="DATABASE2060", RefersToR1C1:= _
Reference

Running the vba code doesn't achieve the same result. Help guys

Try this, I think it will do the trick.

Range("A1", Range("I65536").End(xlUp).Address).Name = "DATABASE2060"

Regards,
 
Upvote 0
Whoops, make that

Sheets("2060_DB").Range("A1", Sheets("2060_DB").Range("I65536").End(xlUp).Address).Name = "DATABASE2060"

:)
 
Upvote 0

Forum statistics

Threads
1,213,482
Messages
6,113,915
Members
448,532
Latest member
9Kimo3

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