Help Deleting some blank rows based on a condition?

Coyotex3

Well-known Member
Joined
Dec 12, 2021
Messages
507
Office Version
  1. 365
Platform
  1. Windows
Good afternoon, I have some codes which help me delete certain rows based on a blank cell, having a hard time getting it to go from this

Book1
ABCDEFGH
1Date
2Time
3User
4Rights
5
6NumberCityCountryDescriptionOwedNotes
70000012-000School
8Change0
9
100000013-000Playground
11CentralLAUSPark United100Busiest Location
12WestLAUSPark West1502nd Busiest
13Change250
14
150000015-000Museum
16Change0
17
180000017-000Concert
19EuropeParisFranceVenue150100% Capacity
20North AmericaNYCUSAVenue210095% Capacity
21South AmericaSantiagoChileVenue315095% Capacity
22AfricaLagosNigeriaVenue420095% Capacity
23AsiaTokyoJapanVenue525095% Capacity
24AustraliaMelbourneAustrliaVenue630095% Capacity
25AntarticaN/AN/AVenue74500% Capacity
26Change1500
27
28
29
30
Sheet1


To this:

Book1
ABCDEFG
1Date
2Time
3User
4Rights
5
6NumberCityCountryDescriptionOwedNotes
70000013-000Playground
8CentralLAUSPark United100Busiest Location
9WestLAUSPark West1502nd Busiest
100000017-000Concert
11EuropeParisFranceVenue150100% Capacity
12North AmericaNYCUSAVenue210095% Capacity
13South AmericaSantiagoChileVenue315095% Capacity
14AfricaLagosNigeriaVenue420095% Capacity
15AsiaTokyoJapanVenue525095% Capacity
16AustraliaMelbourneAustrliaVenue630095% Capacity
17AntarticaN/AN/AVenue74500% Capacity
18
19
20
21
22
23
24
25
Sheet1


I'm going to have spreadsheets with variable ranges. I'm trying to get my code to look at Range("A7") and since there is nothing corresponding to this account (If the line below it is empty), delete all the rows up to the next non-blank cell which in this case would be until Range("A10"). Since there is data below Range("A10"), leave most of it alone, but row 13 should be deleted as there is no nothing important on that row. And repeat until the end of the spreadsheet.

Unsure what condition I can use to make this work. Any tips?
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Maybe like this but your explanation is a bit confusing
VBA Code:
Sub MM1()
Range("A7:A" & Cells(Rows.Count, "A").End(xlUp).Row).SpecialCells(4).EntireRow.Delete
End Sub
 
Upvote 0
Maybe like this but your explanation is a bit confusing
VBA Code:
Sub MM1()
Range("A7:A" & Cells(Rows.Count, "A").End(xlUp).Row).SpecialCells(4).EntireRow.Delete
End Sub
Michael, thank you for your help and sorry for the confusing statements.

Essentially, I would like for the code to look at Column A, If Column A does not have anything below it i.e Range("A7"), then Delete all rows until it gets to the next non-blank cell which in this scenario is Range("A10").

Since Range("A10") has values beneath Range("A11:A12"), leave those alone, but since Range("A13") is blank, we can delete the entire row as well.

Hopefully this makes a little more sense.
 
Upvote 0
Try this code:
VBA Code:
Option Explicit
Sub delRows()
Dim lr&, i&, k&, rng, arr(1 To 10000, 1 To 1)
lr = Cells(Rows.Count, "D").End(xlUp).Row
On Error Resume Next
Range("A7:A" & lr).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
lr = Cells(Rows.Count, "D").End(xlUp).Row
rng = Range("B7:B" & lr).Value
For i = 1 To UBound(rng) - 1
    If rng(i, 1) = "" And rng(i + 1, 1) = "" Then
            k = k + 1
            arr(k, 1) = i + 6 & ":" & i + 6
            GoTo z:
    End If
z:
Next
For i = 1 To k
    Range(arr(i, 1)).Delete
Next
End Sub
 
Upvote 0
Solution
Good afternoon, I have some codes which help me delete certain rows based on a blank cell, having a hard time getting it to go from this

Book1
ABCDEFGH
1Date
2Time
3User
4Rights
5
6NumberCityCountryDescriptionOwedNotes
70000012-000School
8Change0
9
100000013-000Playground
11CentralLAUSPark United100Busiest Location
12WestLAUSPark West1502nd Busiest
13Change250
14
150000015-000Museum
16Change0
17
180000017-000Concert
19EuropeParisFranceVenue150100% Capacity
20North AmericaNYCUSAVenue210095% Capacity
21South AmericaSantiagoChileVenue315095% Capacity
22AfricaLagosNigeriaVenue420095% Capacity
23AsiaTokyoJapanVenue525095% Capacity
24AustraliaMelbourneAustrliaVenue630095% Capacity
25AntarticaN/AN/AVenue74500% Capacity
26Change1500
27
28
29
30
Sheet1


To this:

Book1
ABCDEFG
1Date
2Time
3User
4Rights
5
6NumberCityCountryDescriptionOwedNotes
70000013-000Playground
8CentralLAUSPark United100Busiest Location
9WestLAUSPark West1502nd Busiest
100000017-000Concert
11EuropeParisFranceVenue150100% Capacity
12North AmericaNYCUSAVenue210095% Capacity
13South AmericaSantiagoChileVenue315095% Capacity
14AfricaLagosNigeriaVenue420095% Capacity
15AsiaTokyoJapanVenue525095% Capacity
16AustraliaMelbourneAustrliaVenue630095% Capacity
17AntarticaN/AN/AVenue74500% Capacity
18
19
20
21
22
23
24
25
Sheet1


I'm going to have spreadsheets with variable ranges. I'm trying to get my code to look at Range("A7") and since there is nothing corresponding to this account (If the line below it is empty), delete all the rows up to the next non-blank cell which in this case would be until Range("A10"). Since there is data below Range("A10"), leave most of it alone, but row 13 should be deleted as there is no nothing important on that row. And repeat until the end of the spreadsheet.

Unsure what condition I can use to make this work. Any tips?

Did you try the code ??
When I read ran the code, It kept a few extra rows I would like to get rid of. This code kept rows A7, A10, A15, and A18. Ideally I would like to delete Rows A7 and A15 as well as there is no relevant information belonging to that group(anything below it).
 
Upvote 0
Try this code:
VBA Code:
Option Explicit
Sub delRows()
Dim lr&, i&, k&, rng, arr(1 To 10000, 1 To 1)
lr = Cells(Rows.Count, "D").End(xlUp).Row
On Error Resume Next
Range("A7:A" & lr).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
lr = Cells(Rows.Count, "D").End(xlUp).Row
rng = Range("B7:B" & lr).Value
For i = 1 To UBound(rng) - 1
    If rng(i, 1) = "" And rng(i + 1, 1) = "" Then
            k = k + 1
            arr(k, 1) = i + 6 & ":" & i + 6
            GoTo z:
    End If
z:
Next
For i = 1 To k
    Range(arr(i, 1)).Delete
Next
End Sub
This one works. Thank you so much!
 
Upvote 0

Forum statistics

Threads
1,221,814
Messages
6,162,131
Members
451,743
Latest member
matt3388

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