Naming Ranges Based on Final Row

dpmaki

Board Regular
Joined
Sep 12, 2011
Messages
165
I have one column (A) that will change from month to month, but will always define the maximum number of rows that my data set can be held in. I've therefore coded it as follows.

FinalRow = Range("A70000").End(xlUp).Row

Columns B-F will range in the number of rows that they have, but as mentioned will never go beyond column A in number of rows. What I would like to do is name all of these other colums as ranges too.

Can anyone explain how to do this?
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Do you mean something like this?
Code:
    Dim i As Long
    Dim myRange As Range
    Dim myRangeName As String
    
'   Loop through columns B - F, naming each one from first row down to last used row in that column
    For i = 2 To 6
        Set myRange = Range(Cells(1, i), Cells(Rows.Count, i).End(xlUp))
        myRangeName = "NamedRange" & i
        ActiveWorkbook.Names.Add Name:=myRangeName, RefersTo:=myRange
    Next i
 
Upvote 0
Well - kind of - the problem that I have though is that not all the columns have something in every cell. That's why I was hoping that I could define the number of rows in the column by using column A, which will always have the maximum number of rows.
 
Upvote 0
So, are you saying that you want all your named ranges for each column to end in the exact same row as column A?
That should be real easy. Just incorporate your code into mine and change one line:
Code:
Set myRange = Range(Cells(1, i), Cells([COLOR=red][B]FinalRow[/B][/COLOR], i))
 
Last edited:
Upvote 0
Do I still need the Dim in the code? Sorry - just began learning VBA and some of the advanced features of excel.
 
Upvote 0
It is generally good practice to Declare you variables in your code. And if you use Option Explicit at the top of your code, it will help identify any undeclared variables (and catch typos!). It is very handy for debugging.
Code:
Option Explicit
 
Sub MyMacro()
 
    Dim FinalRow as Long
    Dim i As Long
    Dim myRange As Range
    Dim myRangeName As String
 
'   Get final row from column A
    FinalRow=Cells(Rows.Count,"A").End(xlUp).Row
 
'   Loop through columns B - F, naming each one from first row down to last used row in that column
    For i = 2 To 6
        Set myRange = Range(Cells(1, i), Cells(FinalRow, i))          
        myRangeName = "NamedRange" & i
        ActiveWorkbook.Names.Add Name:=myRangeName, RefersTo:=myRange
    Next i
 
End Sub

Edit: I corrected a typo in my previous post.
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,728
Members
452,939
Latest member
WCrawford

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