Delete 100000 rows based on matching the value for first row with last row

Maklil

Board Regular
Joined
Jun 23, 2022
Messages
146
Office Version
  1. 2019
Platform
  1. Windows
Hello
I have multiple ranges in Details sheet if the second row (under header for each range contains value is the same value where is in lastrow(TOTAL) in column E or the lastrow(TOTAL) contains zero in column E then should delete the ranges , otherwise should keep the rest
I have about 100000 rows and increasable .

Merging ranges_3.xlsb
ABCDE
1DATECUSTOMERINV NOITEMQTY
211/11/2000CSS-100INV-A123ITTT-100/AS-1200
31/7/2023CSS-100INV-A123ITTT-100/AS-1100
41/7/2023CSS-100INV-A123ITTT-100/AS-140
51/7/2023CSS-100INV-A123ITTT-100/AS-150
6TOTAL190
7
8
9DATECUSTOMERINV NOITEMQTY
1011/12/2000CSS-100INV-A123ITTT-100/AS-2300
111/7/2023CSS-100INV-A123ITTT-100/AS-2400
12TOTAL400
13
14
15DATECUSTOMERINV NOITEMQTY
1611/15/2000CSS-100INV-A124ITTT-100/AS-2600
171/7/2023CSS-100INV-A124ITTT-100/AS-2300
181/7/2023CSS-100INV-A124ITTT-100/AS-2100
191/7/2023CSS-100INV-A124ITTT-100/AS-2200
20TOTAL600
21
22
23DATECUSTOMERINV NOITEMQTY
2411/17/2000CSS-100INV-A129ITTT-100/AS-7800
251/7/2023CSS-100INV-A129ITTT-100/AS-7500
261/7/2023CSS-100INV-A129ITTT-100/AS-7200
271/7/2023CSS-100INV-A129ITTT-100/AS-7100
28TOTAL0
DETAILS

should be
Merging ranges_3.xlsb
ABCDE
1DATECUSTOMERINV NOITEMQTY
211/11/2000CSS-100INV-A123ITTT-100/AS-1200
31/7/2023CSS-100INV-A123ITTT-100/AS-1100
41/7/2023CSS-100INV-A123ITTT-100/AS-140
51/7/2023CSS-100INV-A123ITTT-100/AS-150
6TOTAL190
7
8
9DATECUSTOMERINV NOITEMQTY
1011/12/2000CSS-100INV-A123ITTT-100/AS-2300
111/7/2023CSS-100INV-A123ITTT-100/AS-2400
12TOTAL400
OUTPUT



thanks
 
When I run Peter's code on your sample data the items remaining are shown below and consists of 2 small ranges.
What this means is that although the size looks large at 40,445 rows 40,440 of the rows are contiguous that is they are a single block of rows (29 - 40,445) with nothing in between that needs to be kept.
Deleting a single block is not what makes deleting rows a slow process, deleting rows scattered throughout the data set is what is slow,
So unless this is actually the case in your live data, then using this to test performance is not going to give you a representative time.

Using your original Post #1 data and repeating it to fill 60k rows, it takes around 18 secs to run

Result after running the Macro on your sample data:

20230110 VBA Delete Non-Contiguous Formatted rows Maklil v05.xlsm
ABCDEFG
1DATECUSTOMERINV NOITEMQTYOriginal Row No
211/11/2000CSS-100INV-A123ITTT-100/AS-12002
37/01/2023CSS-100INV-A123ITTT-100/AS-13003
47/01/2023CSS-100INV-A123ITTT-100/AS-1404
57/01/2023CSS-100INV-A123ITTT-100/AS-1505
6TOTAL3906
77
88
9DATECUSTOMERINV NOITEMQTY23
1017/11/2000CSS-100INV-A129ITTT-100/AS-780024
117/01/2023CSS-100INV-A129ITTT-100/AS-790025
12TOTAL10026
1327
1428
15
16
17
OUTPUT
 
Upvote 0

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
If you do discover the performance issue I am referring, try the code below.
It reduces the 18 secs on a more realistic set of data (60k rows) down to 5 and if you don't have any formulas in your "DETAIL" sheet you can swap out the Paste:=xlPasteFormulas with Paste:=xlPasteValues, which will reduce it to under 3 secs.

This is based on @Peter_SSs's post 5 which uses the sort method and relies on his very accurate flagging of the rows in a helper column with 1 for to be deleted and importantly Blank for to be retained. The code copies the formatting from the to be retained rows to the post deleted result in the Sheet output.

It works as far as I can tell. Let me know how it goes at your end.

VBA Code:
Sub Del_Blocks_PeterPost5_withCopyBorders()
  Dim a As Variant, b As Variant
  Dim nc As Long, i As Long, j As Long, k As Long, fr As Long, rws As Long
  Dim v As Double, tot As Double
  Dim tm As Double
  tm = Timer
  
  With Sheets("DETAILS")
        nc = .Cells.Find(What:="*", LookIn:=xlFormulas, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column + 1
        a = .Range("E1", .Range("E" & Rows.Count).End(xlUp).Offset(2)).Value
        a(UBound(a) - 1, 1) = "QTY"
        ReDim b(1 To UBound(a), 1 To 1)
      For i = 1 To UBound(a)
        Select Case a(i, 1)
          Case "QTY"
            If rws > 0 Then
              If a(i - 3, 1) = v Or a(i - 3, 1) = 0 Then
                For j = fr To fr + rws - 1
                  k = k + 1
                  b(j, 1) = 1
                Next j
              End If
            End If
            fr = i
            v = a(i + 1, 1)
            rws = 1
          Case Else
            rws = rws + 1
        End Select
      Next i
        
      If k > 0 Then
        Application.ScreenUpdating = False
       
        Application.DisplayAlerts = False
        On Error Resume Next
        Sheets("OUTPUT").delete
        On Error Resume Next
        Application.DisplayAlerts = True
         
        With .Range("A1").Resize(UBound(a), nc)
          .Columns(nc).Value = b
          .Columns(nc).SpecialCells(xlBlanks).EntireRow.Copy
        End With
        
        Dim outSht As Worksheet
        Set outSht = Sheets.Add
        outSht.Name = "OUTPUT"
    
        With .Range("A1").Resize(UBound(a), nc)
            .Copy
            outSht.Range("A1").PasteSpecial Paste:=xlPasteFormulas
        End With
    
        
        With outSht.Range("A1").Resize(UBound(a), nc)
          .Range("A1").PasteSpecial Paste:=xlPasteFormats
          .Sort Key1:=.Columns(nc), Order1:=xlAscending, Header:=xlNo
          .Resize(k).EntireRow.delete
        End With
        
        With .Range("A1").Resize(UBound(a), nc)
            .Columns(nc).SpecialCells(xlBlanks).EntireRow.Copy
        End With
    
        outSht.Range("A1").PasteSpecial Paste:=xlPasteFormats
        .Columns(nc).ClearContents
        
        .Columns("A").Resize(, nc - 1).Copy
        Sheets("Output").Columns("A").PasteSpecial Paste:=xlPasteColumnWidths, Operation:=xlNone, _
            SkipBlanks:=False, Transpose:=False
        Application.ScreenUpdating = True
      End If
  End With
  MsgBox Format(Timer - tm, "0.00")
End Sub
 
Upvote 0
@bebo021999 thanks
it doesn't seem to work !
1) Below image is after code. Could you point the error out?

Capture.JPG




2) BTW, to dupplicate sheet without button, I use:
VBA Code:
Application.CopyObjectsWithCells = False
'copy
Application.CopyObjectsWithCells = True

And the final code should be:
VBA Code:
Option Explicit


Sub delRange()
Dim lr&, i&, j&, t&, seri, rng
Dim tm As Double
  tm = Timer
Application.CopyObjectsWithCells = False
Sheets("DETAILS").Copy after:=Sheets("DETAILS") ' dupplicate sheet DETAILS
If Evaluate("=ISREF(OUTPUT!A1)") Then Sheets("OUTPUT").Delete ' delete the old OUTPUT sheet
ActiveSheet.Name = "OUTPUT"
Application.CopyObjectsWithCells = True
lr = Cells(Rows.Count, "A").End(xlUp).Row
rng = Range("A1:F" & lr).Value
For i = 1 To UBound(rng) - 1
   If rng(i, 1) = "DATE" Then
        For j = i + 1 To UBound(rng)
            If rng(j, 1) = "TOTAL" Then
                If rng(j, 5) <> 0 And rng(j, 5) <> rng(i + 1, 5) Then
                    For t = i To IIf(j >= UBound(rng), UBound(rng), j + 2)
                        seri = seri + 1
                        rng(t, 6) = seri
                    Next
                    i = j + 1
                End If
                Exit For
            End If
        Next
    End If
Next
With Range("A1").Resize(UBound(rng), 6)
    .Value = rng
    .Sort Range("F1")
End With
lr = Cells(Rows.Count, "F").End(xlUp).Row
Range(Cells(lr + 1, 1), Cells(Rows.Count, 1)).EntireRow.Delete
Columns("F").ClearContents
MsgBox "Done!"
MsgBox Format(Timer - tm, "0.00")
End Sub
 
Upvote 0
@bebo021999
thanks nothing changes .
1) Below image is after code. Could you point the error out?
didy you see missed borders for row3,4 and the TOTAL row border becomes to bottom in row7 and add wrong border in row2 ? the right way as in range row9:12 as in picture . your code makes the TOTAL row border to bottom .
 
Upvote 0
1) Below image is after code. Could you point the error out?
Row 2 has a heavy border that should not be there.
Rows 3 and 4 do not have internal borders at all.
Row 7 has the heavy border that should be on row 6.
As Alex has pointed out the borders issue related to sorting.

Edit: Oops, missed @Maklil post above so ignore my repetition above.

@Alex Blakenburg
I think that something is wrong with your code. Haven't looked at the cause in detail but I suspect it could relate to the fact that the initial blocks are not evenly spaced - see before image in post #15

In any case, I have found this alternative code faster than others suggested and retains formatting. At this stage I have assumed that original data has at least two blank rows between each block.

VBA Code:
Sub Del_Blocks_v4()
  Dim a As Variant, b As Variant
  Dim nc As Long, i As Long, j As Long, k As Long, fr As Long, rws As Long
  Dim v1 As Double, v2 As Double
 
  Dim tm As Double
  tm = Timer
 
  Application.DisplayAlerts = False
  On Error Resume Next
  Sheets("OUTPUT").Delete
  On Error Resume Next
  Application.DisplayAlerts = True
  Sheets.Add(after:=Sheets("DETAILS")).Name = "OUTPUT"
  With Sheets("DETAILS")
    nc = .Cells.Find(What:="*", LookIn:=xlFormulas, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column + 1
    a = .Range("E1", .Range("E" & Rows.Count).End(xlUp).Offset(2)).Value
    a(UBound(a) - 1, 1) = "QTY"
    ReDim b(1 To UBound(a), 1 To 1)
    For i = 1 To UBound(a)
      Select Case a(i, 1)
        Case "QTY"
          If rws > 0 Then
            If v1 <> v2 And v2 <> 0 Then
              For j = fr To fr + rws - 1
                k = k + 1
                b(j, 1) = 1
              Next j
            End If
          End If
          fr = i
          v1 = a(i + 1, 1)
          rws = 1
        Case vbNullString
          If a(i - 2, 1) <> "" Then rws = rws + 1
        Case Else
          v2 = a(i, 1)
          rws = rws + 1
      End Select
    Next i
    If k > 0 Then
      Application.ScreenUpdating = False
      With .Cells(1, nc).Resize(UBound(a))
        .Value = b
        .SpecialCells(xlConstants).EntireRow.Copy Destination:=Sheets("OUTPUT").Range("A1")
        .ClearContents
      End With
      Sheets("OUTPUT").Cells(1, nc).Resize(k).ClearContents
      Sheets("OUTPUT").UsedRange.EntireColumn.AutoFit
      Application.ScreenUpdating = True
    End If
  End With
  MsgBox Format(Timer - tm, "0.00")
End Sub

I used this sample data copied down 6,000 rows.

Maklil.xlsm
ABCDE
1DATECUSTOMERINV NOITEMQTY
211/11/2000CSS-100INV-A123ITTT-100/AS-1200
31/07/2023CSS-100INV-A123ITTT-100/AS-1300
41/07/2023CSS-100INV-A123ITTT-100/AS-140
51/07/2023CSS-100INV-A123ITTT-100/AS-150
6TOTAL200
7
8
9DATECUSTOMERINV NOITEMQTY
1011/12/2000CSS-100INV-A123ITTT-100/AS-2300
111/07/2023CSS-100INV-A123ITTT-100/AS-240
121/07/2023CSS-100INV-A123ITTT-100/AS-250
13TOTAL0
14
15
16
17
18DATECUSTOMERINV NOITEMQTY
1911/15/2000CSS-100INV-A124ITTT-100/AS-2600
201/07/2023CSS-100INV-A124ITTT-100/AS-2200
211/07/2023CSS-100INV-A124ITTT-100/AS-2400
22TOTAL20
23
24
25DATECUSTOMERINV NOITEMQTY
2611/17/2000CSS-100INV-A129ITTT-100/AS-7800
271/07/2023CSS-100INV-A129ITTT-100/AS-7900
28TOTAL100
29
30
31
32
33DATECUSTOMERINV NOITEMQTY
345/07/2003CSS-1000INV-A1030ITTT-100/AS-9022493
351/07/2023CSS-1000INV-A1030ITTT-100/AS-9021000
361/07/2023CSS-1000INV-A1030ITTT-100/AS-902400
371/07/2023CSS-1000INV-A1030ITTT-100/AS-9021000
381/07/2023CSS-1000INV-A1030ITTT-100/AS-90293
39TOTAL2493
40
41
42DATECUSTOMERINV NOITEMQTY
435/08/2003CSS-1001INV-A1031ITTT-100/AS-9032494
44TOTAL2494
45
46
47DATECUSTOMERINV NOITEMQTY
485/09/2003CSS-1002INV-A1032ITTT-100/AS-9042495
49TOTAL2495
50
51
52
53
54
55
56DATECUSTOMERINV NOITEMQTY
575/10/2003CSS-1003INV-A1033ITTT-100/AS-9052496
58TOTAL200
59
60
DETAILS


Formatting of original sheet shown here

1673414834066.png


First part of my OUTPUT sheet shown here.

1673414886895.png
 
Last edited:
Upvote 0
Solution
@Alex Blakenburg

just question, did you test your modfying?
it doesn't show me anything!
if I said you strange thing , do you surprise from that?
in reality when test the data contains different count rows for each range whether they locates consecutively to the bottom ( following one after another without an interruption or not )the code becomes faster than before.
yesterday gives 0.530 sec
today gives 0.03 for two both codes post#26 & 36 from the first time, after run the macro more than one time the first will give 0.260 sec
second give 0.110 sec

I no know if it's relating for LAPTOP's memory .
 
Upvote 0
@Peter_SSs

again thanks for new version . I can depend on two codes
just I have problem about autofit the columns width , some data don't show clearly for the last code . I have to expanding columns width manually . can the code does it without interfering from me ?
date is not clear and data in column D

Merging ranges.xlsb
ABCDE
1DATECUSTOMERINV NOITEMQTY
2########CSS-100INV-A123ITTT-100/AS-1200
31/7/2023CSS-100INV-A123ITTT-100/AS-1300
41/7/2023CSS-100INV-A123ITTT-100/AS-140
51/7/2023CSS-100INV-A123ITTT-100/AS-150
6TOTAL390
7
8
9DATECUSTOMERINV NOITEMQTY
10########CSS-100INV-A129ITTT-100/AS-7800
111/7/2023CSS-100INV-A129ITTT-100/AS-7900
12TOTAL100
13
14
15DATECUSTOMERINV NOITEMQTY
16########CSS-1152INV-A1182ITTT-100/AS-10542645
17########CSS-1153INV-A1183ITTT-100/AS-10552646
18TOTAL2647
19
20
21DATECUSTOMERINV NOITEMQTY
22########CSS-1411INV-A1441ITTT-100/AS-13132904
23########CSS-1412INV-A1442ITTT-100/AS-13142905
24########CSS-1413INV-A1443ITTT-100/AS-13152906
25########CSS-1414INV-A1444ITTT-100/AS-13162907
26########CSS-1415INV-A1445ITTT-100/AS-13172908
27TOTAL2905
28
29
OUTPUT
 
Upvote 0
just I have problem about autofit the columns width ,
Just add this blue line of code where indicated near the end of the previous code.
Rich (BB code):
Sheets("OUTPUT").Cells(1, nc).Resize(k).ClearContents
Sheets("OUTPUT").UsedRange.EntireColumn.AutoFit
Application.ScreenUpdating = True
 
Upvote 0
@Peter_SSs I bet this tread took much more time to solve it especially yesterday .
thanks for your time and solution .:)
 
Upvote 0

Forum statistics

Threads
1,215,256
Messages
6,123,914
Members
449,132
Latest member
Rosie14

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