Help with macro that will delete a set series of rows from column "A".

IrishMist1748

Board Regular
Joined
Sep 27, 2010
Messages
131
Hello!

I need a macro that will delete a repeating series of rows? What I am faced with is a set series of rows (see the series below) in column "A" that is repeated on occasion. What I need to happen is for all the rows in that series to be deleted each time it is found WITHOUT even one set of the series remaining in column "A".

Thank you!

Type
Weight
Sizes
Gallery
Shape
Weight
Grade
Count
Shape
Weight
Grade
Type
Weight
Shape
Grade
Count
Center
Accent

<colgroup><col style="width:48pt" width="64"> </colgroup><tbody>
</tbody>
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
IrishMist1748,


If this is what your raw data looks like:


Excel Workbook
A
1Type
2Weight
3Sizes
4Gallery
5Shape
6Weight
7Grade
8Count
9Shape
10Weight
11Grade
12Type
13Weight
14Shape
15Grade
16Count
17Center
18Accent
19
Sheet1





What makes up a group?

What should the result look like?

And, explain your logic.
 
Last edited:
Upvote 0
OK, I am sorry if I am confusing you all! What I meant to say is I need to create a macro in Excel 2003 that will search every row of a sheet for the words:

"Type", "Weight", "Sizes", "Gallery", "Shape", "Weight", "Grade", "Count", "Shape", "Weight", "Grade", "Type", "Weight", "Shape", "Grade", "Count", "Center", "Accent"

in that precise order and if it finds that sequence it will delete those rows (the entire sequence of 18 words and their rows). There is a total of about 70,000 rows with about 550 sets of these specific word sequences that need to be removed from my data. Thank you so much for your help! See below for examples of what I need done.

Below is simple sample of initial data:

Sheet1

*A
1aa
2aa
3aa
4aa
5aa
6aa
7aa
8aa
9aa
10Type
11Weight
12Sizes
13Gallery
14Shape
15Weight
16Grade
17Count
18Shape
19Weight
20Grade
21Type
22Weight
23Shape
24Grade
25Count
26Center
27Accent
28bb
29bb
30Type
31Weight
32Sizes
33Gallery
34Shape
35Weight
36Grade
37Count
38Shape
39Weight
40Grade
41Type
42Weight
43Shape
44Grade
45Count
46Center
47Accent
48cc
49cc
50cc
51cc
52cc
53Type
54Weight
55Sizes
56Gallery
57Shape
58Weight
59Grade
60Count
61Shape
62Weight
63Grade
64Type
65Weight
66Shape
67Grade
68Count
69Center
70Accent
71dd
72dd
73dd

<tbody>
</tbody>


Excel tables to the web >> Excel Jeanie HTML 4

Below is how coloumne "A" should look after the macro runs:

Sheet1

*A
1aa
2aa
3aa
4aa
5aa
6aa
7aa
8aa
9aa
10bb
11bb
12cc
13cc
14cc
15cc
16cc
17dd
18dd
19dd

<tbody>
</tbody>


Excel tables to the web >> Excel Jeanie HTML 4

And again, below is pictured the 18 words listed in the order they should be in and only that order before those rows are delted:

Sheet1

*A
1Type
2Weight
3Sizes
4Gallery
5Shape
6Weight
7Grade
8Count
9Shape
10Weight
11Grade
12Type
13Weight
14Shape
15Grade
16Count
17Center
18Accent

<colgroup><col style="font-weight:bold; width:30px; "><col style="width:64px;"></colgroup><tbody>
</tbody>


Excel tables to the web >> Excel Jeanie HTML 4

Forget the data below I could not figure out how to get it out of the document!

Sheet1

A
1aa
2aa
3aa
4aa
5aa
6aa
7aa
aa
9aa
10bb
11bb

<tbody>
</tbody>
 
Last edited:
Upvote 0
IrishMist1748,


Sample raw data:


Excel Workbook
A
1aa
2aa
3aa
4aa
5aa
6aa
7aa
8aa
9aa
10Type
11Weight
12Sizes
13Gallery
14Shape
15Weight
16Grade
17Count
18Shape
19Weight
20Grade
21Type
22Weight
23Shape
24Grade
25Count
26Center
27Accent
28bb
29bb
30Type
31Weight
32Sizes
33Gallery
34Shape
35Weight
36Grade
37Count
38Shape
39Weight
40Grade
41Type
42Weight
43Shape
44Grade
45Count
46Center
47Accent
48cc
49cc
50cc
51cc
52cc
53Type
54Weight
55Sizes
56Gallery
57Shape
58Weight
59Grade
60Count
61Shape
62Weight
63Grade
64Type
65Weight
66Shape
67Grade
68Count
69Center
70Accent
71dd
72dd
73dd
74Type
75Weight
76Sizes
77Gallery
78Shape
79Weight
80Grade
81Count
82Shape
83Weight
84Grade
85zz
86zz
87zz
88zz
89
Sheet1





After the macro:


Excel Workbook
A
1aa
2aa
3aa
4aa
5aa
6aa
7aa
8aa
9aa
10bb
11bb
12cc
13cc
14cc
15cc
16cc
17dd
18dd
19dd
20Type
21Weight
22Sizes
23Gallery
24Shape
25Weight
26Grade
27Count
28Shape
29Weight
30Grade
31zz
32zz
33zz
34zz
35
Sheet1





Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

1. Copy the below code, by highlighting the code and pressing the keys CTRL + C
2. Open your workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Where the cursor is flashing, paste the code by pressing the keys CTRL + V
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel, open the workbook, and press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.


Code:
Option Explicit
Option Base 1
Sub Delete18()
' hiker95, 12/20/2012
' http://www.mrexcel.com/forum/excel-questions/676754-help-macro-will-delete-set-series-rows-column.html
Dim r As Long, rr As Long, fr As Long, n As Long
Dim s, i As Long, ii As Long, nn As Long
Application.ScreenUpdating = False
s = Array("Type", "Weight", "Sizes", "Gallery", "Shape", "Weight", "Grade", "Count", "Shape", "Weight", "Grade", "Type", "Weight", "Shape", "Grade", "Count", "Center", "Accent")
n = Application.CountIf(Columns(1), "Type")
For i = 1 To n Step 1
  fr = 0
  On Error Resume Next
  fr = Application.Match(s(1), Columns(1), 0)
  On Error GoTo 0
  If fr = 0 Then
    Application.ScreenUpdating = True
    Exit Sub
  Else
    nn = 0: ii = 0
    For r = fr To fr + 17 Step 1
      nn = nn + 1
      If Cells(r, 1) = s(nn) Then
        ii = ii + 1
      End If
    Next r
    If ii = 18 Then
      Rows(fr).Resize(18).Delete
    End If
  End If
Next i
Application.ScreenUpdating = True
End Sub


Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm


Then run the Delete18 macro.
 
Upvote 0
This works EXCEPT it seems to only take out the first 229 sets of the sequence. I need for it to function on the entire "A" column which is some 54,000 rows. I followed your directions to the letter! Is there something more I need to do?

Thank you!
 
Upvote 0
IrishMist1748,

The macro did work on the posted raw data.

Can I see your workbook with just column A's actual data?

You can upload your workbook to Box Net,
mark the workbook for sharing
and provide us with a link to your workbook.
 
Upvote 0
IrishMist1748,

Thanks for the workbook.

The latest macro running against the 53,468 rows of raw data took 12.6 seconds to run.

541 groups of 18 rows per group = 9,738 rows deleted.



Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).


Code:
Option Explicit
Option Base 1
Sub Delete18V2()
' hiker95, 12/31/2012
' http://www.mrexcel.com/forum/excel-questions/676754-help-macro-will-delete-set-series-rows-column.html
Dim r As Long, lr As Long, rr As Long
Dim s, i As Long, n As Long, nd As Long
Application.ScreenUpdating = False
s = Array("Type", "Weight", "Sizes", "Gallery", "Shape", "Weight", "Grade", "Count", "Shape", "Weight", "Grade", "Type", "Weight", "Shape", "Grade", "Count", "Center", "Accent")
lr = Cells(Rows.Count, 1).End(xlUp).Row
nd = 0
For r = 1 To lr Step 1
  If Cells(r, 1) = "Type" Then
    n = 0: i = 0
    For rr = r To r + 17 Step 1
      n = n + 1
      If Cells(rr, 1) = s(n) Then
        i = i + 1
      End If
    Next rr
    If i = 18 Then
      Rows(r).Resize(18).Delete
      nd = nd + 1
    End If
    lr = Cells(Rows.Count, 1).End(xlUp).Row
  End If
Next r
Application.ScreenUpdating = True
MsgBox "There were " & nd & " groups deleted!"
End Sub


Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm


Then run the Delete18V2 macro.
 
Upvote 0
.. I need to create a macro in Excel 2003 that will search every row of a sheet ...

There is a total of about 70,000 rows ...
Excel 2003 doesn't have 70,000 rows. :confused:


I won't be downloading the sample file but you may wish to try this code, it should be somewhat faster.

I have assumed that there is only data in column A. If that assumption is incorrect, please advise about how many columns contain data.

This code is also flexible in that you could add/delete items from the series list & the code should automatically adjust.

<font face=Courier New><br><SPAN style="color:#00007F">Sub</SPAN> Delete_Groups()<br>  <SPAN style="color:#00007F">Dim</SPAN> aGroupList, a, b<br>  <SPAN style="color:#00007F">Dim</SPAN> i <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, j <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, k <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, m <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, rws <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, GroupSize <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>  <SPAN style="color:#00007F">Dim</SPAN> GroupStarted <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Boolean</SPAN><br><br>  <SPAN style="color:#00007F">Const</SPAN> sGroupList <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN> = "Type, Weight, Sizes, Gallery, Shape, Weight, " _<br>                              & "Grade, Count, Shape, Weight, Grade, Type, " _<br>                              & "Weight, Shape, Grade, Count, Center, Accent"<br>  <br>  aGroupList = Split(sGroupList, ", ")<br>  GroupSize = <SPAN style="color:#00007F">UBound</SPAN>(aGroupList) + 1<br>  <SPAN style="color:#00007F">With</SPAN> Range("A1", Range("A" & Rows.Count).End(xlUp))<br>    a = .Value<br>    rws = <SPAN style="color:#00007F">UBound</SPAN>(a, 1)<br>    <SPAN style="color:#00007F">ReDim</SPAN> b(1 <SPAN style="color:#00007F">To</SPAN> rws, 1 <SPAN style="color:#00007F">To</SPAN> 1)<br>    <SPAN style="color:#00007F">For</SPAN> i = 1 <SPAN style="color:#00007F">To</SPAN> rws<br>      <SPAN style="color:#00007F">If</SPAN> GroupStarted <SPAN style="color:#00007F">Then</SPAN><br>        <SPAN style="color:#00007F">If</SPAN> a(i, 1) = aGroupList(j) <SPAN style="color:#00007F">Then</SPAN><br>          j = j + 1<br>          <SPAN style="color:#00007F">If</SPAN> j = GroupSize <SPAN style="color:#00007F">Then</SPAN><br>            GroupStarted = <SPAN style="color:#00007F">False</SPAN><br>          <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>        <SPAN style="color:#00007F">Else</SPAN><br>          <SPAN style="color:#00007F">For</SPAN> m = i - j <SPAN style="color:#00007F">To</SPAN> i - 1<br>            k = k + 1<br>            b(k, 1) = a(m, 1)<br>          <SPAN style="color:#00007F">Next</SPAN> m<br>          i = i - 1<br>          GroupStarted = <SPAN style="color:#00007F">False</SPAN><br>        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>      <SPAN style="color:#00007F">Else</SPAN><br>        <SPAN style="color:#00007F">If</SPAN> a(i, 1) = aGroupList(0) <SPAN style="color:#00007F">Then</SPAN><br>          GroupStarted = <SPAN style="color:#00007F">True</SPAN><br>          j = 1<br>        <SPAN style="color:#00007F">Else</SPAN><br>          k = k + 1<br>          b(k, 1) = a(i, 1)<br>        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>      <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>    <SPAN style="color:#00007F">Next</SPAN> i<br>    .Value = b<br>  <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br></FONT>
 
Upvote 0
Peter_SSs,

WOW - very, very fast = 0.484 seconds.

One for my archives.

Thanks, and Happy New Year.
 
Upvote 0

Forum statistics

Threads
1,221,443
Messages
6,159,907
Members
451,601
Latest member
terrynelson55

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