Macro to extract specific desciptions

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,563
Office Version
  1. 2021
Platform
  1. Windows
I have decriptions in Col B on sheet1. I would like a macro to do the following

1) Extract descriptions that contain the following text to sheet2 (including Cols A:B)

sale, sales, fact assist , discount, DIC & rebates, F & I (for eg sale pine desk, fact assist pine desk, discount retail desk, F & I sales)

2) Extract descriptions that contain the following text to sheet3 (including Cols A:B)

COS, O/allowance (for eg COS pine desk, O/allowance,

Your assistance in this regard is most appreciated
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Hi..

This should do it..
Code:
Option Compare Text
Private Sub CommandButton1_Click()
Dim r As Range
    With CreateObject("VBScript.RegExp")
        .IgnoreCase = True
        .Pattern = "\b(sale|sales|fact assist|discount|DIC|rebates|F & I)|COS|O/allowance\b"
        For Each r In Range(Cells(1, 2), Cells(Rows.Count, 2).End(xlUp))
        
        If .test(r.Value) = True And InStr(1, r.Value, "COS", 0) Or .test(r.Value) = True And InStr(1, r.Value, "O/Allowance", 0) Then
        r.Offset(, -1).Resize(1, 2).Copy Destination:=Sheets("Sheet3").Cells(Rows.Count, 1).End(xlUp).Offset(1)
        Else
        If .test(r.Value) Then r.Offset(, -1).Resize(1, 2).Copy Destination:=Sheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Offset(1)
        End If
        
        Next r
    End With
End Sub
 
Upvote 0
Hi APO

Thanks for help, much appreciated. If I need to add addtional items to be extracted to sheet 3 do I add to these lines of code? If so where do I add for eg H/Back

.Pattern = "\b(sale|sales|fact assist|discount|DIC|rebates|F & I)|COS|O/allowance\b"
For Each r In Range(Cells(1, 2), Cells(Rows.Count, 2).End(xlUp))

If .test(r.Value) = True And InStr(1, r.Value, "COS", 0) Or .test(r.Value) = True And InStr(1, r.Value, "O/Allowance", 0) Then


Regards

Howard
 
Upvote 0
Hi..

Code:
[COLOR=#333333].Pattern = "\b(sale|sales|fact assist|discount|DIC|rebates|F & I|COS|O/allowance|[/COLOR][COLOR=#574123]H/Back)[/COLOR][COLOR=#333333]\b"[/COLOR]
[COLOR=#333333]For Each r In Range(Cells(1, 2), Cells(Rows.Count, 2).End(xlUp))[/COLOR]

[COLOR=#333333]If .test(r.Value) = True And InStr(1, r.Value, "COS", 0) Or .test(r.Value) = True And InStr(1, r.Value, "O/Allowance", 0) [/COLOR][COLOR=#333333]Or .test(r.Value) = True And InStr(1, r.Value, "[/COLOR][COLOR=#574123]H/Back[/COLOR][COLOR=#333333]", 0) [/COLOR][COLOR=#333333]Then[/COLOR]
 
Upvote 0
Hi APO

Thanks for the help, much appreciated

Howard
 
Upvote 0

Forum statistics

Threads
1,214,985
Messages
6,122,605
Members
449,089
Latest member
Motoracer88

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