Delete ranges based on header contains colon and matched with cell value

abdo meghari

Well-known Member
Joined
Aug 3, 2021
Messages
612
Office Version
  1. 2019
Hello
I want delete range contains header and matched with cell I3 and also delete from SUMMARY range is matched by part of item with cell I3.
data could be 7000 rows as maximum and 10 ranges as maximum.
example :
KashfMabiatReport1.xls
ABCDEFGHI
1
2MOVEMENT : PURCHASING
3DATEINVOICE NOCODEBRANDQTYUNIT PRICETOTALMOVEMENT : SELLING
42024.01.021141284GC 1200R20 AZ0026 CHI80895.000242,560.000
51285GC 1200R20 AZ0183 CHI40925.000
61385GC 315/80R22.5 AT161 CHI20735.000
71287GC 315/80R22.5 AZ126 CHI20735.000
81294GC 315/80R22.5 AZ188 CHI20745.000
91241BS 1200R20 G580 JAP402,035.000
101227BS 215/70R15C R623 THI8425.000
111221BS 205/70R15C R623 THI12405.000
12DATEINVOICE NOCODEBRANDQTYUNIT PRICETOTAL
132024.01.091211306BS 750R16 R230 JAP80715.000114,840.000
141310BS 225/95R16C D618 JAP40515.000
151391BS 265/65R17 D840 JAP50535.000
161287GC 315/80R22.5 AZ126 CHI14735.000
17DATEINVOICE NOCODEBRANDQTYUNIT PRICETOTAL
182024.01.101251227BS 215/70R15C R623 THI2425.000850.000
19DATEINVOICE NOCODEBRANDQTYUNIT PRICETOTAL
202024.01.171381401BS 650R16 R230 JAP5570.0005,210.000
211402BS 245/70R16 D697 JAP4590.000
22DATEINVOICE NOCODEBRANDQTYUNIT PRICETOTAL
232024.01.201481292GC 1200R24 AZ166 CHI24935.00041,550.000
241287GC 315/80R22.5 AZ126 CHI26735.000
25DATEINVOICE NOCODEBRANDQTYUNIT PRICETOTAL
262024.01.201491221BS 205/70R15C R623 THI50405.000411,770.000
271227BS 215/70R15C R623 THI12425.000
281346BS 255/70R15C D84024505.000
291241BS 1200R20 G580 JAP1802,035.000
301244BS 1200R20 R187 JAP42,000.000
31DATEINVOICE NOCODEBRANDQTYUNIT PRICETOTAL
322024.01.212801190BS 285/50R20 DSPORT JAP1705.00011,205.000
331411BS 275/55R20 ALENZA1 JAP4725.000
341269BS 1200R24 G580 JAP41,900.000
35DATEINVOICE NOCODEBRANDQTYUNIT PRICETOTAL
362024.03.262151306BS 750R16 R230 JAP10940.0009,400.000
37DATEINVOICE NOCODEBRANDQTYUNIT PRICETOTAL
382024.03.302171285GC 1200R20 AZ0183 CHI101,225.00012,250.000
39DATEINVOICE NOCODEBRANDQTYUNIT PRICETOTAL
402024.04.062181306BS 750R16 R230 JAP50940.00047,000.000
41DATEINVOICE NOCODEBRANDQTYUNIT PRICETOTAL
422024.04.092261227BS 215/70R15C R623 THI4544.0002,176.000
43DATEINVOICE NOCODEBRANDQTYUNIT PRICETOTAL
442024.04.142301285GC 1200R20 AZ0183 CHI401,205.00082,320.000
451284GC 1200R20 AZ0026 CHI301,125.000
461486WL 205/55R16 Z-108 CHI2185.000
47MOVEMENT : SELLING
48DATEINVOICE NOCODEBRANDQTYUNIT PRICETOTAL
492024.01.245611306BS 750R16 R230 JAP10775.0007,750.000
50
51
52SUMMARY
53TOTAL PURCHASE980,131.000
54TOTAL SELLING7,750.000
55TOTAL PAID2,120,485.000
INVOICES


RESULT
KashfMabiatReport1.xls
ABCDEFGHI
1
2MOVEMENT : PURCHASING
3DATEINVOICE NOCODEBRANDQTYUNIT PRICETOTALMOVEMENT : SELLING
42024.01.021141284GC 1200R20 AZ0026 CHI80895.000242,560.000
51285GC 1200R20 AZ0183 CHI40925.000
61385GC 315/80R22.5 AT161 CHI20735.000
71287GC 315/80R22.5 AZ126 CHI20735.000
81294GC 315/80R22.5 AZ188 CHI20745.000
91241BS 1200R20 G580 JAP402,035.000
101227BS 215/70R15C R623 THI8425.000
111221BS 205/70R15C R623 THI12405.000
12DATEINVOICE NOCODEBRANDQTYUNIT PRICETOTAL
132024.01.091211306BS 750R16 R230 JAP80715.000114,840.000
141310BS 225/95R16C D618 JAP40515.000
151391BS 265/65R17 D840 JAP50535.000
161287GC 315/80R22.5 AZ126 CHI14735.000
17DATEINVOICE NOCODEBRANDQTYUNIT PRICETOTAL
182024.01.101251227BS 215/70R15C R623 THI2425.000850.000
19DATEINVOICE NOCODEBRANDQTYUNIT PRICETOTAL
202024.01.171381401BS 650R16 R230 JAP5570.0005,210.000
211402BS 245/70R16 D697 JAP4590.000
22DATEINVOICE NOCODEBRANDQTYUNIT PRICETOTAL
232024.01.201481292GC 1200R24 AZ166 CHI24935.00041,550.000
241287GC 315/80R22.5 AZ126 CHI26735.000
25DATEINVOICE NOCODEBRANDQTYUNIT PRICETOTAL
262024.01.201491221BS 205/70R15C R623 THI50405.000411,770.000
271227BS 215/70R15C R623 THI12425.000
281346BS 255/70R15C D84024505.000
291241BS 1200R20 G580 JAP1802,035.000
301244BS 1200R20 R187 JAP42,000.000
31DATEINVOICE NOCODEBRANDQTYUNIT PRICETOTAL
322024.01.212801190BS 285/50R20 DSPORT JAP1705.00011,205.000
331411BS 275/55R20 ALENZA1 JAP4725.000
341269BS 1200R24 G580 JAP41,900.000
35DATEINVOICE NOCODEBRANDQTYUNIT PRICETOTAL
362024.03.262151306BS 750R16 R230 JAP10940.0009,400.000
37DATEINVOICE NOCODEBRANDQTYUNIT PRICETOTAL
382024.03.302171285GC 1200R20 AZ0183 CHI101,225.00012,250.000
39DATEINVOICE NOCODEBRANDQTYUNIT PRICETOTAL
402024.04.062181306BS 750R16 R230 JAP50940.00047,000.000
41DATEINVOICE NOCODEBRANDQTYUNIT PRICETOTAL
422024.04.092261227BS 215/70R15C R623 THI4544.0002,176.000
43DATEINVOICE NOCODEBRANDQTYUNIT PRICETOTAL
442024.04.142301285GC 1200R20 AZ0183 CHI401,205.00082,320.000
451284GC 1200R20 AZ0026 CHI301,125.000
461486WL 205/55R16 Z-108 CHI2185.000
47
48
49SUMMARY
50TOTAL PURCHASE980,131.000
51TOTAL PAID2,120,485.000
INVOICES

thanks
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Try this:

VBA Code:
Sub deleterange()
  Dim ar As Range, f As Range, r As Range, rng As Range
  Dim i As Long, lr As Long
  Dim itm As String, cell As String
  
  lr = Range("A" & Rows.Count).End(3).Row
  Set rng = Range("A" & lr + 1).Resize(1, 7)
  
  itm = Trim(Split([I3].Value, ":")(1))
  Set r = Range("A:A")
  Set f = r.Find(itm, , xlValues, xlPart, , , False)
  If Not f Is Nothing Then
    cell = f.Address
    Do
      Set rng = Union(rng, f.Resize(1, 7))
      Set f = r.FindNext(f)
    Loop While f.Address <> cell
  End If
  
  Set r = Range("D:D")
  Set f = r.Find([I3], , xlValues, xlWhole, , , False)
  If Not f Is Nothing Then
    cell = f.Address
    Do
      For i = f.Row + 2 To lr
        Select Case Range("D" & i).Value
          Case "", "BRAND", [I3]
            i = i - 1
            Exit For
        End Select
      Next
      Set ar = Range("C" & f.Row + 1, Range("C" & i)).SpecialCells(xlCellTypeConstants)
      Set rng = Union(rng, ar.Offset(-1, -2).Resize(ar.Rows.Count + 1, 7))
      Set f = r.FindNext(f)
    Loop While f.Address <> cell
  End If
  
  Application.ScreenUpdating = False
  rng.Select
  rng.Delete Shift:=xlUp
  Application.ScreenUpdating = False
End Sub

🤗
 
Upvote 0
Hi Dante ,again .
I'm not sure why don't delete if I write MOVEMENT : PURCHASING , it just delete header without range also will delete rows52:55 for SUMMARY range !
and when write MOVEMENT : SELLING then it's ok except one thing will not delete row 54 as show in result .
row 54 will contain selling should match wit I3 MOVEMENT : SELLING then should delete from SUMMARY range.
 
Upvote 0
It is not clear where is the end of the range to delete.

Try this:
VBA Code:
Sub deleterange()
  Dim ar As Range, f As Range, r As Range, rng As Range
  Dim i As Long, lr As Long
  Dim itm As String, cell As String
 
  lr = Range("A" & Rows.Count).End(3).Row
  Set rng = Range("A" & lr + 1).Resize(1, 7)
 
  itm = WorksheetFunction.Trim(Split([I3].Value, ":")(1))
  Set r = Range("A:A")
  Set f = r.Find(itm, , xlValues, xlPart, , , False)
  If Not f Is Nothing Then
    cell = f.Address
    Do
      Set rng = Union(rng, f.Resize(1, 7))
      Set f = r.FindNext(f)
    Loop While f.Address <> cell
  End If
 
  Set r = Range("D:D")
  Set f = r.Find([I3], , xlValues, xlWhole, , , False)
  If Not f Is Nothing Then
    cell = f.Address
    Do
      For i = f.Row + 2 To lr
        If Range("D" & i).Value = "" Or InStr(1, Range("D" & i).Value, ":") > 0 Then
          i = i - 1
          Exit For
        End If
      Next
      Set ar = Range("C" & f.Row + 1, Range("C" & i)).SpecialCells(xlCellTypeConstants)
      Set rng = Union(rng, ar.Offset(-1, -2).Resize(ar.Rows.Count + 1, 7))
      Set f = r.FindNext(f)
    Loop While f.Address <> cell
  End If
 
  Application.ScreenUpdating = False
  'rng.Select
  rng.Delete Shift:=xlUp
  Application.ScreenUpdating = False
End Sub

I'm not sure why don't delete if I write MOVEMENT : PURCHASING , it just delete header without range also will delete rows52:55 for SUMMARY range !
and when write MOVEMENT : SELLING then it's ok except one thing will not delete row 54 as show in result .
row 54 will contain selling should match wit I3 MOVEMENT : SELLING then should delete from SUMMARY range.
Check that "Selling" is spelled correctly and also does not contain spaces after the word.

And another thing, in the case of "Purchasing", it is not the same as "Purchase"


Try again and tell me.
😇
 
Upvote 0
Solution
sorry about error in spelling!
ok this is better , but I need fixing problem deletion the whole summary range .
if I delete one of them based on I3 then should keep the others , but the code will delete all of cells for SUMMARY range.
example: when delete TOTAL SELLING then should keep TOTAL PURCHASE, TOTAL PAID. under SUMMARY word.
It is not clear where is the end of the range to delete.
finally I don't understand this statement until I answer you.

thanks again
 
Upvote 0
sorry about error in spelling!
First, on I3 you should have something like this: MOVEMENT : SELLING , just as you put it in your initial example.

Second, the range of cells to delete starts where the text: "MOVEMENT : SELLING" exists, but you are not explaining where the range of rows to delete ends. I'm assuming where it ends, but to make it work check the following image:

1725719731744.png


Let's test before deleting rows. In the macro, change this line:
VBA Code:
rng.Delete Shift:=xlUp

For this line:
VBA Code:
rng.Select

So based on the above, try the macro again and tell me what it selects.

If the selected data is not what should be deleted, then, put an image here so that I can see what is selected, and in another image, put what should be deleted.
It is important that the images shows what you have in I3.

🧙‍♂️
 
Upvote 0
based on your instruction in post#6 works greatly .
sorry buddy about confusion 🙏
every thing is ok now.
thank you so much.:)
 
Upvote 1

Forum statistics

Threads
1,223,888
Messages
6,175,212
Members
452,618
Latest member
Tam84

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