VBA to delete rows with certain conditions

joelpe

New Member
Joined
Oct 18, 2018
Messages
15
Hi,

I dont know how to create VBA macros and use that languaje, so I will ask here.

I have a sheet with 2 columns of text, A and B.

There are some rows with A blank and B with text, and others with text in A and B blank.

I need a VBA to delete those rows that have text in A but B is blank, while maintaining the other type (A blank and B with text) intact.

Many thanks
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Do you need VBA?

Can do it manually with Data Filter
Filter on column B = Blank
Delete all visible rows
Remove the filter and the remaining rows are visible again
 
Upvote 0
Or you can create a macro with the macro recorder :
• Turn on the recorder
• Select column B
• Go to SpecialCells/Blanks
• Go to Edit/Delete/Rows
• Turn off the recorder
 
Upvote 0
or VBA solution

1 Test this in a COPY of your workbook
2 Paste some test data into sheet1 starting at cell A1
3 Add the code as detailed below
4 Run macro from sheet1 {ALT}{F8} lists available macros


Adding the code

Copy the code below \ {ALT}{F11} takes you to VBA \ {ALT} I M (Inserts a new Module) \ paste code there \ {ALT}{F11} takes you back to Excel

Code:
Sub FilterBlanksInColB()
    Dim rng1 As Range, rng2 As Range
    Set rng1 = ActiveSheet.Range("A1").CurrentRegion
    Set rng2 = rng1.Offset(1)
    On Error Resume Next
    rng1.Parent.ShowAllData
    rng1.AutoFilter Field:=2, Criteria1:="="
    rng2.SpecialCells(xlCellTypeVisible).EntireRow.Delete
    rng1.Parent.ShowAllData
End Sub
 
Last edited:
Upvote 0
Or :
Code:
Sub Delete_Blanks()
[B:B].SpecialCells(xlCellTypeBlanks).EntireRow.Delete
End Sub
 
Upvote 0
@joelpe Glad you solved your problem. Thanks for feedback (y)
@footoo Short and simple. perfect :)
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,867
Messages
6,122,002
Members
449,059
Latest member
mtsheetz

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