Is it possible for a macro to search for a phrase and export if true?

ashleywanless

Board Regular
Joined
Aug 4, 2009
Messages
158
Hi,

I have a large sheet which tracks initiatives. I would like to write some code so that a user can click on a button which opens a search box, they can then enter a phrase. What the macro will do is search the data (Columns A-AA) and where it finds a match in text to the phrase it exports that whole row to a new sheet1. It also needs to not produce duplicates as it is possible for the phrase to appear twice in the same row. Column A has a unique identifier if this helps in the process.

If anyone has any ideas on if this is possible and how to progress i would be grateful

Ashley
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Assuming that Sheet1 contains the source data, and Row 1 contains the headers, the following will copy the filtered data to Sheet2...

Code:
Sub test()

Dim x As String
Dim WSO As Worksheet
Dim WSN As Worksheet
Dim LastColumn As Long

Application.ScreenUpdating = False

Do
    x = Application.InputBox("Please enter your search term or phrase...", "Search Term/Phrase")
    If TypeName(x) = "Boolean" Then Exit Sub
Loop Until x <> ""

Set WSO = ActiveWorkbook.Worksheets("Sheet1")
Set WSN = ActiveWorkbook.Worksheets("Sheet2")

WSO.Activate
WSN.Cells.Clear

With WSO.UsedRange
    LastColumn = .Columns.Count
    WSO.Cells(2, LastColumn + 2).FormulaR1C1 = "=MATCH(""*" & x & "*"",RC1:RC" & LastColumn & ",0)"
    .AdvancedFilter Action:=xlFilterInPlace, criteriarange:=Cells(1, LastColumn + 2).Resize(2, 1)
    .Copy Destination:=WSN.Cells(1, 1)
End With

WSO.Activate
WSO.ShowAllData
WSO.Columns(LastColumn + 2).Clear
WSN.Activate

Application.ScreenUpdating = True

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,436
Messages
6,124,869
Members
449,192
Latest member
MoonDancer

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