Help needed naming Ranges with VBA

zombiemaster

Board Regular
Joined
Oct 27, 2009
Messages
241
Hi, all…


I recently inherited a daily report - the data each day has varying number of rows, but always the same number of columns.


The instructions I received include running a macro to set up a number of pivot tables, but first we have to manually name two ranges (the first range is the entire data, the second range is only the data in column “J”).


I thought I could include the naming of the ranges in the VBA to avoid the manual aspect, but am having trouble. I was trying to use the endRow$ feature but I’m getting an error…can someone pick this apart and see what I’ve done wrong? Or is endRow$ not the best way to do this after all?

Thanks for your help as always!


~ZM~



Code:
Sub NamingRanges()
'
' NamingRanges Macro
'
 
' set endRow
    Range("A70000").Select
    Selection.End(xlUp).Select
    endRow$ = ActiveCell.Row
   
   
    Range("A1:AB" + endRow$).Select
    ActiveWorkbook.Names.Add Name:="Sales_Reporting_Region"
    Range("J1:AB" + endRow$).Select
    ActiveWorkbook.Names.Add Name:="Multi_Life_Name"
    Range("A1").Select
End Sub
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Why don't you just do it manually like this:

Code:
=OFFSET(Sheet1!$J$1, 0, 0, COUNTA(Sheet1!$AB:$AB),19)

This is a dynamic named range. If your values are updated it will change accordingly as long as the data in AB is contiguous (does not have gaps)

Or try This:

Code:
Range("J1:AB" & EndRow$ ).Name = "YourNameHere"
 
Last edited:
Upvote 0
Since you have the cells selected I believe you can use Selection.Name = "yourrangenamehere"

FYI for finding FinalRows and FinalColumns I have used the below formula's. You can use these when you don't know what the final column or row of your data is and also if the final column or row is changing this will pick it up. The 1 in the is stating which row or column to use to figure out the where the last row or column is. For example I usually use Row 1 and Column 1 because that is usually where I start my spreadsheets.

FinalRow = Cells(Rows.Count,1).End(xlup).Row

FinalCol = Cells(1,Columns.Count).End(xltoleft).Column
 
Upvote 0
don't forget and may need
Rich (BB code):
Dim endRow$ As Long
On Error Resume Next
ActiveWorkbook.Names("Sales_Reporting_Region").delete
endrow$ = Range("A70000").End(xlUp).Row

   Range("A1:AB" & endRow$).Select
    ActiveWorkbook.Names.Add Name:="Sales_Reporting_Region"

Range("J1:AB" + endRow$).Select make Range("J1:J" & endRow$).Select

endrows, may also been seen as LASTROW or LR
 
Upvote 0
Thanks, all...

I don't have time right now, but I'll try some of those suggestions and see what I come up with for more questions or problems!

~ZM~
:cool:
 
Upvote 0
Sweet! Thanks so much to everybody – I ended up using the simple suggestion from mrmmickle1

“Range(“A1:AB” + endRow$”).Name = “Sales_Reporting_Region”

That worked perfectly and I was able to reuse the idea in a few other places as well. Thanks again, and I hope you all have a great weekend!

~ZM~
:cool:
 
Upvote 0

Forum statistics

Threads
1,214,606
Messages
6,120,485
Members
448,967
Latest member
visheshkotha

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