Find Last Row in Sheet

Gregfox

Board Regular
Joined
Apr 12, 2011
Messages
120
Hi, I’m using Excel 2007, and wish to find the last row in data sheet “Main”, and put text “END” in column A of that last cell. The complication is not all columns always have data.
I’ve tried;
Sub LastRowTest ()
LastRowColA = Range("A65536").End(xlUp).Row<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
ActiveCell.FormulaR1C1 = "text"<o:p></o:p>
End sub<o:p></o:p>
======================================================<o:p></o:p>
And I also tried;<o:p></o:p>
Sub LastRowTest ()
Dim ws As Worksheet<o:p></o:p>
Set ws = Worksheets("Main")<o:p></o:p>
<o:p></o:p>
iRow = ws.Cells(Rows.Count, 1) _<o:p></o:p>
.End(xlUp).Offset(1, 0).Row<o:p></o:p>
'ActiveSheet.Paste 'ActiveSheet.PasteActiveCell.FormulaR1C1 = "text"<o:p></o:p>
End sub<o:p></o:p>
<o:p></o:p>
NO LUCK… Anyone have an answer?
Thanks!
:)
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Try

Code:
Sub Test2()
Dim LR As Long
With Sheets("Main")
    LR = .Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
    .Range("A" & LR).Value = "END"
End With
End Sub
 
Upvote 0
How about...

Code:
Sub EnterEndLR()
    Dim LR As Long: LR = Range("A" & Rows.Count).End(xlUp).Row + 1
    Dim ws As Worksheet: Set ws = Worksheets("Main")
    ws.Range("A" & LR).Value = "End"
End Sub
 
Upvote 0
Thank you for the help, but I’m afraid I need a bit more. I thought I could work this out, but am having trouble.
I want to Cut a Row [A4] from a datasheet [MAIN], find the last Empty Row, and Paste it there. Then go back to the top of the file and Cut Row [A5], find the last Empty Row, and INSERT it there {in front of the line that was just put there}, and so on until the macro finds the word END in column A.
Many Thanks.
:confused:
 
Upvote 0
Maybe like this

Code:
Sub Test3()
Dim Found As Range
With Sheets("Main")
    Set Found = .Columns("A").Find(What:="END", LookIn:=xlValues, lookat:=xlWhole)
    If Not Found Is Nothing Then
        .Rows("4:" & Found.Row - 1).Cut Destination:=Found.Offset(1)
    End If
End With
End Sub
 
Upvote 0
This code ran really FAST, but instead of pasting the line in front, it pasted the line after.
What I wanted to happen was as below…
Line A4
Line A5
Line A6
Become
Line A6
Line A5
Line A4
Thank you again!
 
Upvote 0
Then perhaps

Code:
Sub Test3()
Dim Found As Range, i As Integer
With Sheets("Main")
    Set Found = .Columns("A").Find(What:="END", LookIn:=xlValues, lookat:=xlWhole)
    If Not Found Is Nothing Then
        For i = Found.Row - 1 To 4 Step -1
            .Rows(i).Cut Destination:=Found.Offset(1)
        Next i
    End If
End With
End Sub
 
Upvote 0
Almost there! This code while much slower than the last, does the right thing except, instead of adding the line at the end, it over writes the last line as below…
Should be …
Line A6
Line A5
Line A4<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
But is<o:p></o:p>
Line A6<o:p></o:p>
Than<o:p></o:p>
Line A5<o:p></o:p>
In other words it is doing the right thing but it is replacing the line, instead of adding.<o:p></o:p>
I tried changing the offset, but failed.<o:p></o:p>
THANKS AGAIN!
 
Upvote 0
Try

Code:
Sub Test3()
Dim Found As Range, i As Integer
Application.ScreenUpdating = False
With Sheets("Main")
    Set Found = .Columns("A").Find(What:="END", LookIn:=xlValues, lookat:=xlWhole)
    If Not Found Is Nothing Then
        For i = Found.Row - 1 To 4 Step -1
            .Rows(i).Cut Destination:=.Range("A" & Rows.Count).End(xlUp).Offset(1)
        Next i
    End If
End With
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,602
Messages
6,179,844
Members
452,948
Latest member
UsmanAli786

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