VBA to delete range between 2 used ranges.

ngochien251088

New Member
Joined
Jun 27, 2018
Messages
10
Office Version
  1. 2019
Platform
  1. Windows
Hi Everybody,

I would like to seek your help deleting blank rows between 2 used ranges.
I expect to remove the blank rows between the last rows of the used range 1 ( row 56) and the first row of the used range 2 ( row 73).
Another thing is I don't want to remove the blank row between the used rows of range 1, due to it helps to separate each items.
Because there are many sheets and the used range rows are not fixed so I can not write to code by myself.
I am a newbie in VBA.
Thank you very much for your help. Any ideas are appreciated.

Book1
ABCDEFGHIJ
1MULTI LABEL
2
3ITEM #:137PACKS
4VPC #:505-S212-0103
5DESCRIPTION:Paper Adhsv Address Labels 1 x 2-5/8, 1000 SHT
6TOTAL:37CARTONS
7
8ITEM #:120PACKS
9VPC #:505-S212-0104
10DESCRIPTION:Paper Adhsv Address Labels 1/2 x 1-3/4, 100 SHT
11TOTAL:2CARTONS
12
13ITEM #:21,170PACKS
14VPC #:505-S212-0105
15DESCRIPTION:Paper Adhsv Address Labels 1 x 2-5/8, 100 SHT
16TOTAL:117CARTONS
17
18ITEM #:3320PACKS
19VPC #:505-S212-0106
20DESCRIPTION:Paper Adhsv Address Labels 1 x 4, 100 SHT
21TOTAL:32CARTONS
22
23ITEM #:ST18060150PACKS
24VPC #:505-S212-0108
25DESCRIPTION:Shipping Paper Adhsv Labels 2 x 4, 100 SHT
26TOTAL:15CARTONS
27
28ITEM #:ST1806180PACKS
29VPC #:505-S212-0109
30DESCRIPTION:Shipping Paper Adhsv Labels 3-1/3 x 4, 100 SHT
31TOTAL:8CARTONS
32
33ITEM #:ST18062500PACKS
34VPC #:505-S212-0110
35DESCRIPTION:Shipping Paper Adhsv Labels 8-1/2 x 11, 100 SHT
36TOTAL:50CARTONS
37
38ITEM #:ST18064315PACKS
39VPC #:505-S212-0112
40DESCRIPTION:Paper Adhsv Address Labels 1 x 4, 250 SHT
41TOTAL:63CARTONS
42
43ITEM #:ST1806660PACKS
44VPC #:505-S212-0114
45DESCRIPTION:Shipping Paper Adhsv Labels 2 x 4, 250 SHT
46TOTAL:12CARTONS
47
48ITEM #:ST18067270PACKS
49VPC #:505-S212-0115
50DESCRIPTION:Shipping Paper Adhsv Labels 3-1/3 x 4, 250 SHT
51TOTAL:54CARTONS
52
53ITEM #:ST1807450PACKS
54VPC #:505-S212-0116
55DESCRIPTION:Shipping Paper Adhsv Labels 3-1/2 x 5, 100 SHT
56TOTAL:5CARTONS
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73* FREIGHT COLLECT
74* NO WOOD PACKAGING MATERIALS WERE USED IN THIS SHIPMENT
75* CONSIGNEE:STAPLES BRANDS GROUP
76A DIVISION OF STAPLES, THE OFFICE SUPERSTORE, LLC
77500 STAPLES DRIVE
78adafsa
79
80
Sheet2
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
I suggest that you update your Account details (or click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)

Try this with a copy of your worksheet. It leaves one blank row between those two sections.

VBA Code:
Sub Del_Rows()
  Dim rB As Range
 
  For Each rB In Range("C1", Range("C" & Rows.Count).End(xlUp)).SpecialCells(xlBlanks).Areas
    If rB.Rows.Count > 1 Then rB.Resize(rB.Rows.Count - 1).EntireRow.Delete
  Next rB
End Sub
 
Upvote 0
Try on a copy of file.
VBA Code:
Sub DeleteRows()
Dim Lr&, T&, S$
Dim M

Lr = Range("C" & Rows.Count).End(xlUp).Row
M = Filter(Evaluate("Transpose(If(LEN(C2:C" & Lr & "&D2:D" & Lr & "&E2:E" & Lr & "&F2:F" & Lr & "&G2:G" & Lr & ")=0,""C""&Row(C2:C" & Lr & "),false))"), False, False)
For T = UBound(M) To 0 Step -1
S = S & "," & M(T)
If Len(S) > 240 Or (T = 0 And Len(S) > 0) Then Range(Mid(S, 2)).EntireRow.Delete
Next T
End Sub
 
Upvote 0
I would like to seek your help deleting blank rows between 2 used ranges.
I expect to remove the blank rows between the last rows of the used range 1 ( row 56) and the first row of the used range 2 ( row 73).
try
Code:
Sub test()
    Dim LR&, x&, y&
    LR = Range("c" & Rows.Count).End(xlUp).Row
    x = Evaluate("max(if(c1:c" & LR & "=""TOTAL:"",row(1:" & LR & ")))")
    If x = 0 Then Exit Sub
    y = Evaluate("min(if(c" & x + 1 & ":c" & LR & "<>"""",row(" & x + 1 & ":" & LR & ")))")
    If (y <> 0) * (y - x > 1) Then Rows(x + 1 & ":" & y - 1).Delete
End Sub
 
Upvote 1
Solution
This code will delete rows between ranges and does not delete single blank rows in the data.
VBA Code:
Sub DeleteRows()
Dim Lr&, T&, S$
Dim M

Lr = Range("C" & Rows.Count).End(xlUp).Row
M = Filter(Evaluate("Transpose(If(LEN(C2:C" & Lr & "&D2:D" & Lr & "&E2:E" & Lr & "&F2:F" & Lr & "&G2:G" & Lr & ")=0,Row(C2:C" & Lr & "),false))"), False, False)
For T = UBound(M) To 1 Step -1
If Val(M(T - 1)) = Val(M(T)) - 1 Then S = S & ",C" & M(T)
If Len(S) > 240 Or (T <= 1 And Len(S) > 0) Then Range(Mid(S, 2)).EntireRow.Delete: S = ""
Next T
End Sub
 
Upvote 1
I suggest that you update your Account details (or click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)

Try this with a copy of your worksheet. It leaves one blank row between those two sections.

VBA Code:
Sub Del_Rows()
  Dim rB As Range
 
  For Each rB In Range("C1", Range("C" & Rows.Count).End(xlUp)).SpecialCells(xlBlanks).Areas
    If rB.Rows.Count > 1 Then rB.Resize(rB.Rows.Count - 1).EntireRow.Delete
  Next rB
End Sub
Thank you so much for your quick code. It runs well. I appreciate your time.
 
Upvote 0
This code will delete rows between ranges and does not delete single blank rows in the data.
VBA Code:
Sub DeleteRows()
Dim Lr&, T&, S$
Dim M

Lr = Range("C" & Rows.Count).End(xlUp).Row
M = Filter(Evaluate("Transpose(If(LEN(C2:C" & Lr & "&D2:D" & Lr & "&E2:E" & Lr & "&F2:F" & Lr & "&G2:G" & Lr & ")=0,Row(C2:C" & Lr & "),false))"), False, False)
For T = UBound(M) To 1 Step -1
If Val(M(T - 1)) = Val(M(T)) - 1 Then S = S & ",C" & M(T)
If Len(S) > 240 Or (T <= 1 And Len(S) > 0) Then Range(Mid(S, 2)).EntireRow.Delete: S = ""
Next T
End Sub
Thank you so much for your coding. It also goes well with my sheet! I appreciate it.
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,176
Members
451,543
Latest member
cesymcox

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