Select Last cell in named range VBA

dimitri

Board Regular
Joined
Nov 8, 2010
Messages
78
Hi,
I'm looking for the VBA code that will select the last cell in named range. I can select the first cell by using this code:
Code:
Range("NamedRange")(1).select

But i don't know how to select the last cell. What I ultimately want to do is insert 2 rows above the last cell in my named range.

Thanks.
 
Last edited:

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Code:
Sheet1.Select
Range("MyRange").Select
Selection.End(xlDown).Select
'Insert Entire Row
Selection.EntireRow.Insert , CopyOrigin:=xlFormatFromLeftOrAbove
Selection.EntireRow.Insert , CopyOrigin:=xlFormatFromLeftOrAbove
'OR
'Move Cells Down
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
 
Upvote 0
Code:
Sub InsertInNamedRange()
  Dim r As Long, c As Long, rng As Range
  Set rng = Range("NamedRange")
  For r = 1 To 2
    For c = 1 To rng.Columns.Count
      rng.Cells(rng.Rows.Count, c).Insert _
        Shift:=xlDown, _
        CopyOrigin:=xlFormatFromLeftOrAbove
    Next c
  Next r
End Sub
 
Last edited:
Upvote 0
With Range("Rng")
.Cells(.Rows.Count).Resize(2).EntireRow.Insert
End With
 
Upvote 0
The codes work well, But once the rows have been inserted, I want to select the second to last cell in the range. I thought I could use this code:

Code:
Range("NamedRange").Select
Selection.End(xlDown).Select

then Offset to one row up. But the code above only selects the last cell in the range with a value, and I want the absolute last cell in the range, regardless of whether or not it has a value.
 
Upvote 0
The codes work well, But once the rows have been inserted, I want to select the second to last cell in the range. I thought I could use this code:

Code:
Range("NamedRange").Select
Selection.End(xlDown).Select

then Offset to one row up. But the code above only selects the last cell in the range with a value, and I want the absolute last cell in the range, regardless of whether or not it has a value.
This adaptation of Scott R's code will do it (I've assumed that the named range is a single column of contiguous cells), but note that selecting things in vba is rarely needed and slows your code.

If you explain what you want to do after selecting the second last cell then a more efficient code can most likely be suggested.
Code:
With Range("NamedRange")
    .Cells(.Rows.Count).Resize(2).EntireRow.Insert
    .Cells(.Rows.Count - 1, 1).Select
End With
 
Upvote 0
Peter SSs,
Thanks for your code, it works really well. Once I've selected the second to last cell, I want the active cell to move over 3 columns to the left and enter some text into that cell.

Ex. If the second to last cell in my named range is G5, I need to insert the text "New Tenant" into cell D5.

I've been able to do this by using offset. The code I have below is my adaptation of yours, but if you have a better suggestion I'm certainly open to it.

Code:
With Range("FirstRange")
    .Cells(.Rows.Count).Resize(2).EntireRow.Insert
    .Cells(.Rows.Count - 1, 1).Select
    ActiveCell.Offset(0, -3).Select
    ActiveCell.Value = "New Tenant"
End With

Thanks.
 
Upvote 0
Peter SSs,
Thanks for your code, it works really well. Once I've selected the second to last cell, I want the active cell to move over 3 columns to the left and enter some text into that cell.

Ex. If the second to last cell in my named range is G5, I need to insert the text "New Tenant" into cell D5.

I've been able to do this by using offset. The code I have below is my adaptation of yours, but if you have a better suggestion I'm certainly open to it.

Code:
With Range("FirstRange")
    .Cells(.Rows.Count).Resize(2).EntireRow.Insert
    .Cells(.Rows.Count - 1, 1).Select
    ActiveCell.Offset(0, -3).Select
    ActiveCell.Value = "New Tenant"
End With

Thanks.

If that code works, then this modification of it should also work...

Code:
With Range("FirstRange")
    .Cells(.Rows.Count).Resize(2).EntireRow.Insert
    .Cells(.Rows.Count - 1, 1).Offset(0, -3).Value = "New Tenant"
End With

Notice all of the "selecting" has been eliminated in favor of chaining the properties together.
 
Upvote 0
Rick,
Wow, that's great. I didn't know I could do it without selecting any cells. Thanks for the help.

Everyone in this thread has been extremely helpful.
 
Upvote 0

Forum statistics

Threads
1,224,578
Messages
6,179,654
Members
452,934
Latest member
mm1t1

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