Intersect Not Working with my Last Cell

Guzzlr

Well-known Member
Joined
Apr 20, 2009
Messages
977
Office Version
  1. 2021
Platform
  1. Windows
Hello
For Finding the last cell with data, I have:
VBA Code:
Dim LastCell as Range
Set LastCell = ActiveSheet.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious)
MsgBox "The Last Cell with Data Is In Row:  " & LastCell.Row
The above is working correctly.


I now have the code that I am trying to incorporate LastCell into:
Code:
Set fnd = Range("5:5").Find("Service Provider", , , xlWhole, , , False, , False)
If Not fnd Is Nothing Then
    Intersect(Range("5:LastCell"), fnd.EntireColumn).Copy
    Range("O5").Insert Shift:=xlToRight
        Intersect(Range("1:LastCell"), fnd.EntireColumn).Clear
        Range("B:B").Delete

The above code I have used many times, but I've always used something like: Range ("5:5000")
I would like to be able to use the LastCell instead of a random number like Row 5000. However, I've never used the LastCell before to replace the row number.
How would I do that?
Thank you for the help
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Everything inside double-quotes is treated as literal text.
All variables must be OUTSIDE of the double-quotes.
And you want to pull the Row off of the cell it finds.

So this:
VBA Code:
Range("5:LastCell")
should be this:
VBA Code:
Range("5:" & LastCell.Row)
 
Upvote 0
Does this help?

VBA Code:
Sub Test()
  Dim fnd As Range
  Set fnd = Range("5:5").Find("Service Provider", , , xlWhole, , , False, , False)
  If Not fnd Is Nothing Then
    Intersect(Range("5:" & Cells.Rows.Count), fnd.EntireColumn).Copy
    Range("O5").Insert Shift:=xlToRight
    Intersect(Range("5:" & Cells.Rows.Count), fnd.EntireColumn).Clear
    Range("B:B").Delete
  End If
End Sub
 
Upvote 0
Everything inside double-quotes is treated as literal text.
All variables must be OUTSIDE of the double-quotes.
And you want to pull the Row off of the cell it finds.

So this:
VBA Code:
Range("5:LastCell")
should be this:
VBA Code:
Range("5:" & LastCell.Row)
Oh...thanks for the schooling!
 
Upvote 0

Forum statistics

Threads
1,216,119
Messages
6,128,941
Members
449,480
Latest member
yesitisasport

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