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

Maklil

Board Regular
Joined
Jun 23, 2022
Messages
174
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
 
Oops, you are right there. I said doesn't when I should have said does.
 
Upvote 0

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
@Peter_SSs
your code is too fast , awesome !
but I need fixing two thing
when delete some ranges , it doesn't delete correctly the data doesn't become arranged !
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/19/2000CSS-108INV-A131ITTT-100*12GF1000
111/7/2023CSS-108INV-A131ITTT-100*12GF2
121/7/2023CSS-108INV-A131ITTT-100*12GF2
13TOTAL4
14
15
16DATECUSTOMERINV NOITEMQTY
1711/20/2000CSS-109INV-A132ITTT-100*12GF1100
181/7/2023CSS-109INV-A132ITTT-100*12GF200
191/7/2023CSS-109INV-A132ITTT-100*12GF20
20TOTAL220
21
OUTPUT

second the data should show OUTPUT SHEET
to see closly problem formatting after delete it see the picture .
1.JPG
 
Last edited:
Upvote 0
data should show OUTPUT SHEET
OK, I will fix that. Does OUTPUT already exist in the workbook?
If so, can I delete it with the code and make a new one?


when delete some ranges , it doesn't delete correctly the data doesn't become arranged !
I don't understand that part. With your sample data from post #1 my code seemed to me to produce exactly what you showed as the output (apart from not being on a different sheet)
Can you give a small set of sample data where my code does not do what you want and also post the results that you do want for that sample data?
 
Upvote 0
Does OUTPUT already exist in the workbook?
yes
If so, can I delete it with the code and make a new one?
Do you mean making new code from scratch ?
it's up to you buddy.
my code seemed to me to produce exactly what you showed as the output (apart from not being on a different sheet)
yes surley without any doubt .
just the problem is relating with formatting for each range when delete some ranges and move some ranges to up then the formatting becomes untidy
see the picrure before
1.JPG

after run the macro
2.JPG


I hope this help you.
 
Upvote 0
then the formatting becomes untidy
From your 'before' picture it looks like the formatting already is untidy in that the blocks of data are not uniformly spaced with 2 blank rows between every block like there was in your post #1 sample.
Is that the problem you are referring to?

My code simply deleted whole rows and leaves the remaining rows formatted as they were. If the issue is something other than the number of blank rows then I would think that the formatting problems must already be there.

In any case I can not do any testing with a picture so if there are other issues can you give a small sample with XL2BB that demonstrate those other issues?
 
Upvote 0
just when I posted the pictures instead of using XL2BB to see clearly how the formatting was for each range .
for me it doesn't seem to show as in picture when using XL2BB .
ok I make sure there are spaced with 2 blank rows between every block like there was in your post #1 sample. even if that the problem still continues.
here is orginal data before

Merging ranges_3.xlsb
ABCDE
1DATECUSTOMERINV NOITEMQTY
211/11/2000CSS-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
1011/12/2000CSS-100INV-A123ITTT-100/AS-2300
111/7/2023CSS-100INV-A123ITTT-100/AS-240
121/7/2023CSS-100INV-A123ITTT-100/AS-250
13TOTAL300
14
15
16DATECUSTOMERINV NOITEMQTY
1711/15/2000CSS-100INV-A124ITTT-100/AS-2600
181/7/2023CSS-100INV-A124ITTT-100/AS-2200
191/7/2023CSS-100INV-A124ITTT-100/AS-2400
20TOTAL0
21
22
23DATECUSTOMERINV NOITEMQTY
2411/17/2000CSS-100INV-A129ITTT-100/AS-7800
251/7/2023CSS-100INV-A129ITTT-100/AS-7900
26TOTAL100
27
28
29DATECUSTOMERINV NOITEMQTY
305/7/2003CSS-1000INV-A1030ITTT-100/AS-9022493
311/7/2023CSS-1000INV-A1030ITTT-100/AS-9021000
321/7/2023CSS-1000INV-A1030ITTT-100/AS-902400
331/7/2023CSS-1000INV-A1030ITTT-100/AS-9021000
341/7/2023CSS-1000INV-A1030ITTT-100/AS-90293
35TOTAL2493
36
37
38DATECUSTOMERINV NOITEMQTY
395/8/2003CSS-1001INV-A1031ITTT-100/AS-9032494
40TOTAL2494
41
42
43DATECUSTOMERINV NOITEMQTY
445/9/2003CSS-1002INV-A1032ITTT-100/AS-9042495
45TOTAL2495
46
47
48DATECUSTOMERINV NOITEMQTY
495/10/2003CSS-1003INV-A1033ITTT-100/AS-9052496
50TOTAL2496
DETAILS


this is what I got after run the macro
Merging ranges_3.xlsb
ABCDE
1DATECUSTOMERINV NOITEMQTY
211/11/2000CSS-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
1011/17/2000CSS-100INV-A129ITTT-100/AS-7800
111/7/2023CSS-100INV-A129ITTT-100/AS-7900
12TOTAL100
OUTPUT


should be
Merging ranges_3.xlsb
ABCDE
1DATECUSTOMERINV NOITEMQTY
211/11/2000CSS-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
1011/17/2000CSS-100INV-A129ITTT-100/AS-7800
111/7/2023CSS-100INV-A129ITTT-100/AS-7900
12TOTAL100
OUTPUT

1.JPG
 
Upvote 0
Again:
VBA Code:
Option Explicit
Sub delRange()
Dim lr&, i&, j&, t&, seri, rng
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"
lr = Cells(Rows.count, "A").End(xlUp).Row
rng = Range("A1:F" & lr).Value
For i = 1 To UBound(rng) - 1
    rng(i, 6) = ""
    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!"
End Sub
 
Upvote 0
I believe the issue is that the Borders don't move with the sort. The XL2BB does not include the borders so they are not in our test data.
 
Upvote 0
@bebo021999
thanks again !
your first code does every thing , the only problem is running speed .
the second code is very fast . the only problem is formatting
see this picture and compare with picture for post#17 how 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
8DATECUSTOMERINV NOITEMQTY
911/12/2000CSS-100INV-A123ITTT-100/AS-2300
101/7/2023CSS-100INV-A123ITTT-100/AS-2400
11TOTAL400
12
OUTPUT


1.JPG
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,250
Members
452,623
Latest member
Techenthusiast

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