VBA Code to keep only certain rows

clau818

New Member
Joined
Oct 31, 2016
Messages
32
Hello, novice in the VBA world and trying to put together some code that allows me to keep only certain rows in my data that meet certain criteria.

So I am pulling invoices from a program and the raw data gives me a bunch of unnecessary lines of information.

The rows I want to keep are the following;
If the cell in column A contains the text string "BIG"
I also want to keep the row following the row with the text string "BIG"
If the cell in column A contains the text string "IT1"
If the cell in column A contains the text string "N1" and the adjacent cell in column B contains the text string "BY"

All other rows can be deleted.

I need to be able to select all of the data in 2 or 3 invoices at once and run the code on my selected data only.

Thanks in advance for your help!!
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Here is an example of what my data looks like for one invoice:

ABCDEFGHIJKLMNOPQ
ISA0 0 128.46E+0917942915 U401 0P>
GSIN84553895037942915201612191054X004010VICS
ST810
BIG20161219110175835281
TC#568
TC&Consolidated Invoice
REFDP294
REFIA5104603
N1ST92569
N1BY92569
ITD13430
DTM1120161206
IT1474EAUP889967090084VA25882
TC#NONE
PIDFadf
IT1877EAUP889967090312VA27079
TC#NONE
PIDFghadg
IT12386EAUP889967090541VA30698
TC#NONE
PIDFadtre
IT13059EAUP889967086414VA30587
TC#NONE
PIDFasdfa
IT13111EAUP889967090343VA30587
TC#NONE
PIDFereasd
IT13219EAUP889967090350VA30587
TC#NONE
PIDFhfhfs
IT13449EAUP889967090190VA30721
TC#NONE
PIDFdfadf
IT13559EAUP889967090039VA30721
TC#NONE
PIDFzvvzxcv
IT13639EAUP889967090046VA30721
TC#NONE
PIDFzvzcv
IT13866EAUP889967090206VA30739
TC#GOLDNONE
PIDFzcvzcfe
TDS00
CADMGBEABM92450630
ISS0CA0LB
CTT10
SE45
GE1
IEA1

<colgroup><col><col><col><col><col><col><col><col><col span="9"></colgroup><tbody>
</tbody>
 
Upvote 0
Try this:
Code:
Sub MyDeleteRows()

    Dim lastRow As Long
    Dim myRow As Long
    Dim deleteRow As Boolean
    
    Application.ScreenUpdating = False
    
'   Find last row in column A with data
    lastRow = Cells(Rows.Count, "A").End(xlUp).Row
    
'   Loop through all rows backwardS, up to row 2
    For myRow = lastRow To 2 Step -1
        deleteRow = True
'       Check for "BIG" on current row
        If InStr(Cells(myRow, "A").Value, "BIG") Then
            deleteRow = False
        Else
'           Check for "BIG" on previous row
            If InStr(Cells(myRow - 1, "A").Value, "BIG") Then
                deleteRow = False
            Else
'               Check for "IT1" on current row
                If InStr(Cells(myRow, "A").Value, "IT1") Then
                    deleteRow = False
                Else
'                   Check for "N1" and "BY" on current row
                    If InStr(Cells(myRow, "A").Value, "N1") And _
                        InStr(Cells(myRow, "B").Value, "BY") Then
                        deleteRow = False
                    End If
                End If
            End If
        End If
'       Delete row if any condition met
        If deleteRow Then Rows(myRow).Delete
    Next myRow
    
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
wow, you are brilliant! thanks so much! I was so far off from this, no chance I would've figured it out. Thanks for the comments as well! slowly but surely figuring this stuff out.

One thing, it's running the code for my entire sheet. Is it possible to just have it run on the data that I select? Or will that require completely different code? If not, it's fine I can just import the raw data to a new sheet every time and copy it to my master sheet where I have all my previous invoices on.
 
Upvote 0
One other thing, I would like the invoices to be separated by a space. After the code runs I would like to insert 2 rows right above the row "BIG" row. Having some trouble putting this together as well.

Again, thanks again for everything you sent me already!
 
Upvote 0
A few modifications to have it run against the selected range:
Code:
Sub MyDeleteRows()

    Dim firstRow As Long
    Dim prevRow As Long
    Dim lastRow As Long
    Dim myRow As Long
    Dim deleteRow As Boolean
    
    Application.ScreenUpdating = False
    
'   Find find and last row of selected range
    firstRow = Selection.Rows(1).Row
    lastRow = Selection.Rows.Count + firstRow - 1
    
'   Loop through all rows backwardS, up to row 2
    For myRow = lastRow To firstRow Step -1
        deleteRow = True
'       Find previous row (default to 1 if top row)
        prevRow = myRow - 1
        If prevRow = 0 Then prevRow = 1
'       Check for "BIG" on current row
        If InStr(Cells(myRow, "A").Value, "BIG") Then
            deleteRow = False
        Else
'           Check for "BIG" on previous row
            If InStr(Cells(prevRow, "A").Value, "BIG") Then
                deleteRow = False
            Else
'               Check for "IT1" on current row
                If InStr(Cells(myRow, "A").Value, "IT1") Then
                    deleteRow = False
                Else
'                   Check for "N1" and "BY" on current row
                    If InStr(Cells(myRow, "A").Value, "N1") And _
                        InStr(Cells(myRow, "B").Value, "BY") Then
                        deleteRow = False
                    End If
                End If
            End If
        End If
'       Delete row if any condition met
        If deleteRow Then Rows(myRow).Delete
    Next myRow
    
    Application.ScreenUpdating = True
    
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,539
Messages
6,120,100
Members
448,944
Latest member
SarahSomethingExcel100

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