Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Delete entire row if 2 column data is blank at the same time

0

Hello,

I have an excel workbook that has +100 worksheets with exact same structure. 

4 columns; 3rd and 4th column contains data. What I'm trying to do is;

deleting the entire row if BOTH 3rd and 4th column is blank at the same time.

If not (if any contains data) do nothing.

Also, I have merged cells (all 4 columns merged) in some rows and I would like them to be deleted too. Because at the end they don't contain data for 3rd and 4th column.

I tried my best coming up with this code but obviously it does not work;

Sub delete_empty_row()

Application.ScreenUpdating = False

Columns("D:D").Select

Selection.SpecialCells(xlCellTypeBlanks).Select

If Selection.Columns("C:C").SpecialCells(xlCellTypeBlanks).Select Then

Selection.EntireRow.Delete

End If

Application.ScreenUpdating = True

End Sub
Answer
Discuss

Answers

0
Selected Answer

Hello again furkanyildiz,

Here is some code to delete the unwanted rows:

Updated 04/19/25 @ 15:45

Updated 04/20/25 @ 08:40 (as per John_Ru's recommendation)

I was rushing when I originally posted the code earlier. I've had some time to do some better testing and found a mistake. The code below is the corrected code.

Sub Delete_Rows_with_Blanks()

' macro written by WillieD24 for teachexcel.com
' this code will delete rows if cells "C" AND "D" are blank/empty
' this code will also delete rows with merged cells
' the code loops though all sheets in the workbook

Dim WScount As Long   ' number of worksheets in the workbook
Dim Lrow As Long   ' last row of used range
Dim r As Long   ' row number to check
Dim ws As Worksheet

' how many worksheets are in the workbook
WScount = ThisWorkbook.Worksheets.Count
Application.ScreenUpdating = False

For Each ws In Worksheets
    ws.Activate
    Lrow = ws.UsedRange.Rows.Count

        For r = Lrow To 2 Step -1
            With ws
                If Cells(r, 3) = "" And Cells(r, 4) = "" Then   ' cells "C" and "D" are empty
                    Rows(r).EntireRow.Delete
                End If
            End With
        Next r
        Cells(1, 1).Select
Next ws
Application.ScreenUpdating = True

End Sub
'

If this is the answer you were looking for please mark my answer as Selected

Cheers   :-)

Discuss

Discussion

WARNING! 

The code under "Updated 04/19/25 @ 15:45" contains an error. The portion below:

         For r = Lrow To 2 Step -1
            If Cells(r, 3) = "" And Cells(r, 4) = "" Then   ' cells "C" and "D" are empty
                Rows(r).EntireRow.Delete
            End If
         Next r


will delete the rows in the active worksheet ONLY.

To do the same in all other sheets, the test and delete operation must refer to the worksheet within the For Each loop and so should read (changes in bold):

          For r = Lrow To 2 Step -1
             If ws.Cells(r, 3) = "" And ws.Cells(r, 4) = "" Then
             ' cells "C" and "D" are empty in the row in worksheet ws 
               ws. Rows(r).EntireRow.Delete
             End If
          Next r 
John_Ru (rep: 6722) Apr 20, '25 at 2:32 am
@John

I have updated my code as per your recommendation. When I tested my original code, it worked for me (Excel 2016); but none the less, your skills are superior to mine so I changed the code as per your reommendation.

Cheers   :-)
WillieD24 (rep: 687) Apr 20, '25 at 8:45 am
@Willie- thanks (I was goinmg from memory and away from my PC).

I suspect you checked your code on a single sheet (and it would work) but not with several sheets (unless you Activated each sheet).
John_Ru (rep: 6722) Apr 20, '25 at 8:57 am
@John,
After some more testing I found I had to wrap the deletion part in a "With" block to avoid an error, and had to activate each worksheet or the code would run only on the currently active sheet.
Cheers   :-)
WillieD24 (rep: 687) Apr 20, '25 at 9:38 am
@Willie - I see that you decided to Activate each sheet (like I commented). This may have a minor impact on execution time but should work fine.
John_Ru (rep: 6722) Apr 20, '25 at 9:45 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login