How to delete last used row?

elmnas

Board Regular
Joined
Feb 20, 2015
Messages
206
Hello people,

I have written a code that remove last added row, its very simple it counts from down to up my issue is I want the code not be able to delete row 1 and 2.

I did wrote "A3"
but its still possible.


Code:
Dim lRow As Long
Dim lCol As Long
    
    lRow = Cells.Find(What:="*", _
                    After:=Range("A3"), _
                    LookAt:=xlPart, _
                    LookIn:=xlFormulas, _
                    SearchOrder:=xlByRows, _
                    SearchDirection:=xlPrevious, _
                    MatchCase:=False).Row
    
    Rows(lRow).ClearContents


could someone help me ?

Thank you in advance
 
Thank you Peter_SSs


this code work as a charm!

a last tweak...


I trying to remove the last added row for all the sheets.
But I don't get it work

Could you help me out?

I made this code
Code:
Sub DelSheeSeG()


 Dim ws As Worksheet


    For Each ws In ThisWorkbook.Sheets


ActiveWorkbook.Sheets("All_Segments").Activate
 Dim lRow As Long
      
  lRow = Cells.Find(What:="*", After:=Cells(1, 1), LookIn:=xlFormulas, SearchOrder:=xlByRows, _
    SearchDirection:=xlPrevious, SearchFormat:=False).Row
  
  If lRow > 1 Then
    Rows(lRow).ClearContents
  End If
Next ws


End Sub

what is wrong?..
 
Upvote 0

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
I didn't test this but try

Rich (BB code):
Sub mytest3()
  Dim lRow As Long
  Dim ws As Worksheet
  
  For Each ws In Worksheets
    With ws
      lRow = .Cells.Find(What:="*", After:=.Cells(1, 1), LookIn:=xlFormulas, SearchOrder:=xlByRows, _
        SearchDirection:=xlPrevious, SearchFormat:=False).Row
      
      If lRow > 2 Then
        .Rows(lRow).ClearContents
      End If
    End With
  Next ws
End Sub
 
Upvote 0
This would be safer in case you have a worksheet with nothing on it.

Rich (BB code):
Sub mytest3()
  Dim lRow As Long
  Dim ws As Worksheet
  
  For Each ws In Worksheets
    With ws
      lRow = 0
      On Error Resume Next
      lRow = .Cells.Find(What:="*", After:=.Cells(1, 1), LookIn:=xlFormulas, SearchOrder:=xlByRows, _
        SearchDirection:=xlPrevious, SearchFormat:=False).Row
      On Error GoTo 0
      
      If lRow > 2 Then
        .Rows(lRow).ClearContents
      End If
    End With
  Next ws
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,897
Messages
6,175,269
Members
452,628
Latest member
dd2

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