VBA Delete Row if ....

Pinaceous

Well-known Member
Joined
Jun 11, 2014
Messages
1,113
Office Version
  1. 365
Platform
  1. Windows
Hi All,

I'm working with a database where the first Column A's cell is not blank starting in row 11 of the worksheet.

I'm trying to create a Sub to delete the row if there are no values in the proceeding Column A.

For example, If Column A is not empty BUT its Columns B to AK are empty, the sub will delete that row.

Capture1.PNG


Above there are values in Column A but if there are no values in that row from Columns B to AK, the sub will delete that row, as can be seen below.

Capture2.PNG



So here the sub will delete the rows 15-16, 18-36 ...., because there were no values in that row from Columns B to AK, so the sub will delete that row.

Please let me know, if you can provide me with a sub to test out!

Thank you!
Pinaceous
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Try on a copy...

VBA Code:
Sub ClearRowContentsIfBIsEmpty()
    Dim lastRow As Long
    Dim ws As Worksheet
    Dim cell As Range
    Dim rowsToClear As Range
    Dim clearCount As Long
   
    Set ws = ThisWorkbook.Sheets("Sheet1") ' Change "Sheet1" to your sheet name

    lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
    For Each cell In ws.Range("A1:A" & lastRow)
        If IsEmpty(cell.Offset(0, 1)) Then

            If rowsToClear Is Nothing Then
                Set rowsToClear = cell.EntireRow
            Else
                Set rowsToClear = Union(rowsToClear, cell.EntireRow)
            End If
        End If
    Next cell

    If Not rowsToClear Is Nothing Then
        rowsToClear.ClearContents
    End If
End Sub
 
Upvote 0
Thank you Cubist!

I'll give it a try and appreciate that!
 
Upvote 0
Maybe this one as well, does the active sheet
VBA Code:
Sub ClearRowContentsIfBIsEmpty()
Dim, r As Long
For r =Cells(Rows.Count, "A").End(xlUp).Row To 11 Step -1
    If WorksheetFunction.CountA(Range("A" & r & ":AK" & r)) = 1 Then Rows(r).Delete
Next r
End Sub
 
Upvote 1
Try on a copy...

VBA Code:
Sub ClearRowContentsIfBIsEmpty()
    Dim lastRow As Long
    Dim ws As Worksheet
    Dim cell As Range
    Dim rowsToClear As Range
    Dim clearCount As Long
  
    Set ws = ThisWorkbook.Sheets("Sheet1") ' Change "Sheet1" to your sheet name

    lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
    For Each cell In ws.Range("A1:A" & lastRow)
        If IsEmpty(cell.Offset(0, 1)) Then

            If rowsToClear Is Nothing Then
                Set rowsToClear = cell.EntireRow
            Else
                Set rowsToClear = Union(rowsToClear, cell.EntireRow)
            End If
        End If
    Next cell

    If Not rowsToClear Is Nothing Then
        rowsToClear.ClearContents
    End If
End Sub
Hey Cubist!

I'm testing out the code and I have a question.

Do you know if you can delete the row specifically between A:AI, only and not the entire length of the row as I previously mentioned?

Please let me know and thank you!
pinaceous
 
Upvote 0
Maybe this

VBA Code:
Sub ClearRowContentsIfEmpty()
    Dim lastRow As Long
    Dim ws As Worksheet
    Dim r As Long
    Dim rowsToClear As Range
   
    Set ws = ThisWorkbook.Sheets("Sheet1")

    lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
    
    For r = 11 To lastRow
        If WorksheetFunction.CountA(ws.Range("A" & r & ":AI" & r)) = 1 Then
            If rowsToClear Is Nothing Then
                Set rowsToClear = ws.Rows(r)
            Else
                Set rowsToClear = Union(rowsToClear, ws.Rows(r))
            End If
        End If
    Next r

    If Not rowsToClear Is Nothing Then
        rowsToClear.Columns("A:AI").ClearContents
    End If
End Sub
 
Upvote 0
Maybe this

VBA Code:
Sub ClearRowContentsIfEmpty()
    Dim lastRow As Long
    Dim ws As Worksheet
    Dim r As Long
    Dim rowsToClear As Range
 
    Set ws = ThisWorkbook.Sheets("Sheet1")

    lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
 
    For r = 11 To lastRow
        If WorksheetFunction.CountA(ws.Range("A" & r & ":AI" & r)) = 1 Then
            If rowsToClear Is Nothing Then
                Set rowsToClear = ws.Rows(r)
            Else
                Set rowsToClear = Union(rowsToClear, ws.Rows(r))
            End If
        End If
    Next r

    If Not rowsToClear Is Nothing Then
        rowsToClear.Columns("A:AI").ClearContents
    End If
End Sub
Yes, this did the trick but I had to run this code several times for it to of worked upon the sheet.

Do you know, if you can modify this code to work once upon these values?

Thanks!
 
Upvote 0
Change the "Sheet1" to your sheet name

VBA Code:
Set ws = ThisWorkbook.Sheets("Sheet1")
 
Upvote 0
Hi Cubist,

I mean that I have to run your code several times before I achieve the final result.

For example, Starting with the original "data" we have:

Capture1.PNG



In running the code once it achieves:

Capture2.PNG



In running the code a 2nd time it achieves:

Capture3.PNG


In running the code a 3rd time it achieves:

Capture4.PNG


Now my question is, how do I combine all of this work into one upon your code to work once?

Please let me know, if you need any further explanation.

Thank you very much!

R/
pinaceous
 
Upvote 0
Hi Pin
Did you try the code I posted ?
With regard to the code @Cubist posted, it will always have issues because you are running the loop from top to bottom, so it will skip rows when one is deleted. You must always loop from bottom to top when deleting rows
 
Upvote 0

Forum statistics

Threads
1,215,071
Messages
6,122,963
Members
449,094
Latest member
Anshu121

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