Named Range Variable

Tropez

New Member
Joined
Apr 19, 2004
Messages
35
Greetings all, I need some assistance on a project:

I have a spreadsheet of survey results that has all of the surveys sent out and in the same spreadsheet are their answers to the questions.

| Responded_On | Sent_On | Answer 1 | Answer 2 | Answer 3 |

I'm trying to create a named range thru the following code:
Code:
    ActiveWorkbook.Names.Add Name:="Responded_On", RefersToR1C1:= "=Surveys!R6C1:R" & LastRow & "C1"

where the LastRow should reference this code which helps account for all the blank cells that are in the column as not everyone responds:

Code:
Sub FindLastRow()
Dim LastRow As Long
    If WorksheetFunction.CountA(Cells) > 0 Then
        'Search for any entry, by searching backwards by Rows.
        LastRow = Cells.Find(What:="*", After:=[A1], _
              SearchOrder:=xlByRows, _
              SearchDirection:=xlPrevious).Row
              ' MsgBox LastRow
    End If
End Sub

The LastRow reference does give me the row number but when the macro gets to this point it simply gives the first cell of the column the named reference instead of the range that I was hoping for.

I've also tried the following code which does not seem to work at all:

Code:
    Range(Cells(6, 1), Cells(6, LastRow)).Name = "Responded_On"

Anyone have any ideas?

Ken
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Hello Tropez,
First let's see if I've captured your intent.
As I understand it you want to define a named range (named "Responded_On") to the range in sheet "Surveys", from cell A6 over to column R and down to the last entry in that column. (Yes?)

If that's correct then perhaps something like this...
Code:
Sub NameThatRange()
Dim LastRow As Long

With Sheets("Surveys")
    If WorksheetFunction.CountA(Cells) > 0 Then
        'Search for any entry, by searching backwards by Rows.
        LastRow = Sheets("Surveys").Cells.Find(What:="*", After:=[A1], _
              SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    End If
End With

ActiveWorkbook.Names.Add Name:="Responded_On", RefersToR1C1:="=Surveys!R6C1:R" & LastRow & "C18"
    
End Sub
Hope it helps.
 
Upvote 0
Actually, I was wanting to go from Cell A6 (the first entry in the column) to the last entry in that same column, which is A313 right now.
 
Upvote 0
Ah... I see.
Then let's limit LastRow to search for used cells only in columnA, and then change the range reference for the named range to column 1 instead of 18.
Code:
Sub NameThatRange()
Dim LastRow As Long

With Sheets("Surveys")
    If WorksheetFunction.CountA(Cells) > 0 Then
        'Search for any entry, by searching backwards by Rows.
        LastRow = Sheets("Surveys").Columns(1).Find(What:="*", After:=[A1], _
              SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    End If
End With

ActiveWorkbook.Names.Add Name:="Responded_On", RefersToR1C1:="=Surveys!R6C1:R" & LastRow & "C1"
    
End Sub
I don't know where I got column R from. Must be those R1C1 references. They screw me up every time. :x
 
Upvote 0
That got it, although I do need it to reference the last used row for the entire table area and not just the one column. As I've got some formulas that do a count of the responses based on these named areas, and for some reason when you are doing a sumproduct lookup with named ranges they all have to be of the same length or number of cells (rows in my case).

THANK YOU!!!
 
Upvote 0
Tropez said:
That got it, although I do need it to reference the last used row for the entire table area and not just the one column.
Oh. (I was wondering if that might be the case.)
If you haven't already got it squared away, you can make your range be just in columnA and go as far down as any value in any column by reverting this line:
LastRow = Sheets("Surveys").Columns(1).Find(What:="*", After:=[A1], _
SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
back to what you had originally:
LastRow = Sheets("Surveys").Cells.Find(What:="*", After:=[A1], _
SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row

Sorry about that.
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,728
Members
448,987
Latest member
marion_davis

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