Macro needed to delete any empty row in table from top

bsnow

New Member
Joined
Mar 29, 2022
Messages
10
Office Version
  1. 365
Platform
  1. Windows
Hi

I'm trying to come up with a macro for a button to be able to delete rows from a table that are empty, starting from the top (most recent).

I have a macro that will add new rows at the top (at Row 2), but I need one to delete rows from the top (Row 2), too.

The macro I have currently is:

Sub DelLR()

Dim x As Long

With Sheets("Sheet1")
'Assumes last row of data found in column A (1)
x = .Cells(.Rows.Count, 1).End(xlUp).Row
.Cells(x, 1).EntireRow.Delete
End With

End Sub


I'd also like to tweak the macro so that it won't allow the user to delete any rows that are already filled in (so only deletes empty rows) from the top downwards.

Thanks guys!
 

Attachments

  • Excel.JPG
    Excel.JPG
    134 KB · Views: 6

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
there is no other data at the LHS or at the RHS of your table ?
from A2 to the first filled A-cell minus 1
VBA Code:
Sub DelLR()
     Dim c     As Range

     With Sheets("Sheet1")
          If .Cells(2, "a") = "" Then                           'A2 is empty
               Set c = .Range(.Range("A2"), .Range("A2").End(xlDown).Offset(-1))     'range from A2 to the 1st not empty A-cell and return 1 row
               MsgBox c.Address                                 'just tell the range
               c.EntireRow.Delete                               'delete
          End If
     End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,603
Messages
6,125,786
Members
449,259
Latest member
rehanahmadawan

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