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
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Hello people,

I have written a code that remove last added row, its very simple it counts from down to up the problem is..

Row 1 and row 2 will not be able to delete I did add "A3" but its still possible to delete row 1 and 2


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
Change your ClearContents part to
Code:
If lRow > 2 Then
  Rows(lRow).ClearContents
End If
 
Upvote 0
doesn't work it doesnt delete any.

hmm
In that case your original code wouldn't have cleared anything (below row 2) either.
All I've added is to prevent anything being cleared from rows 1 and 2 - which is what I though you were asking.
 
Upvote 0
I did wrote your code in this way:

Code:
sub mytest()
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




If lRow > 2 Then
  Rows(lRow).ClearContents
End If


end sub

is this correct?
 
Upvote 0
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.

could someone help me ?

Thank you in advance

What about this?
Code:
Sub deleteLast()
LastRow = Cells(Rows.Count, 1).End(xlUp).Row


If LastRow > 2 Then
    Rows(LastRow).ClearContents
End If
End Sub
 
Upvote 0
When I answered before, I didn't actually test your code because you indicated (or at least that is what I thought) that it was working as expected other than it eventually deleted rows 2 and/or 1. I made a suggestion to stop it doing that.

Now that I look more closely at your code (with my addition) it will never delete anything unless rows 1 and 2 are already empty.
The reason is that you are starting at A3 and searching backwards (up). No doubt that search finds something in row 2 (or 1) and my code for prevention of deletion in those rows means that nothing happens.

Change the A3 back to A1 (& still search xlPrevious) & see how it goes.
 
Upvote 0
When I answered before, I didn't actually test your code because you indicated (or at least that is what I thought) that it was working as expected other than it eventually deleted rows 2 and/or 1. I made a suggestion to stop it doing that.

Now that I look more closely at your code (with my addition) it will never delete anything unless rows 1 and 2 are already empty.
The reason is that you are starting at A3 and searching backwards (up). No doubt that search finds something in row 2 (or 1) and my code for prevention of deletion in those rows means that nothing happens.

Change the A3 back to A1 (& still search xlPrevious) & see how it goes.



It seems to work now
Code:
ColumnA = Cells(Rows.Count, 1).End(xlUp).Row
    If ColumnA > 2 Then
        Rows(ColumnA).EntireRow.ClearContents
   End If

the issue is there can be content in any column not just column 1 I believe the column range 1-8

do you know how I can set a range of columns ?

thank you in advance
 
Upvote 0
Can we just search in all columns?

Rich (BB code):
Sub mytest2()
  Dim lRow As Long
      
  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 Sub
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,213
Members
452,618
Latest member
Tam84

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