VBA: how to search for multiple text strings in a data field and if matched return one word

techgrl

New Member
Joined
May 7, 2014
Messages
10
Hi!

So I have been pulling my hair our trying to figure out how to build a VBA for a process that is so time consuming and repetitive and deadly.

background: I have hundreds of records of customer info and I am trying to categorize the job titles so that I can conduct some analysis with a pivot table. My problem is that there is about 20 different ways and variations of "manager" so Instead of autofiltering for "mrg." "Manager" "marketing manager" ect. and then replacing the text manually and using the enter and fill process (to create some standardization to compare "manager" to "director" to "c-level" to "consultant", etc.) and repeating this process over and over again...

I would like to create a VBA that would search the column "job title" for multiple text strings at once and if the text string was true in the cell then the cell would be replaced by a new text string "Manager". I was thinking a series of if functions within one vba but I am not sure if this is possible. I need help with developing the actually code and direction as if this is even possible.

ANY HELP would be so appreciated!!!!!!!!!!!!!!!!!!
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
This example code searches Sheet2 column C for various forms of "Manager" (change the RE.Pattern to suit) and replaces the cell contents with "Manager".

Code:
[COLOR=darkblue]Sub[/COLOR] Search_Col_C_For_Managers()
    
    [COLOR=darkblue]Dim[/COLOR] v [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Variant[/COLOR], i [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR], j [COLOR=darkblue]As[/COLOR] Long
    [COLOR=darkblue]Dim[/COLOR] RE [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Object[/COLOR]
    
    [COLOR=darkblue]Set[/COLOR] RE = CreateObject("VBScript.RegExp")
    RE.IgnoreCase = [COLOR=darkblue]True[/COLOR]
    RE.Global = [COLOR=darkblue]True[/COLOR]
    RE.Pattern = [COLOR=#ff0000]"Manager|Mngr|Mgr|Mrg"[/COLOR]
    
    [COLOR=darkblue]With[/COLOR] Sheets("Sheet2")
        [COLOR=darkblue]With[/COLOR] .Range("C2", .Range("C" & Rows.Count).End(xlUp))
            v = .Value
            [COLOR=darkblue]For[/COLOR] i = [COLOR=darkblue]LBound[/COLOR](v) [COLOR=darkblue]To[/COLOR] [COLOR=darkblue]UBound[/COLOR](v)
                [COLOR=darkblue]If[/COLOR] RE.Test(CStr(v(i, 1))) [COLOR=darkblue]Then[/COLOR]
                    v(i, 1) = "Manager"
                    j = j + 1
                [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
            [COLOR=darkblue]Next[/COLOR] i
            .Value = v
        [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
    [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
    
    MsgBox j & " 'Managers' found. ", , "Manager Search"
    
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]
 
Last edited:
Upvote 0
Thank you! I really wished it would work because that solution looks promising but I tried to enter this and run it and I received the message" visual basic editor: license information for this component not found. you do not have the functionality license in this design environment"

any suggestions? also If this were to work would I be able to create a vba that did this for several searches at once?

thanks again!
 
Upvote 0
Thank you! I really wished it would work because that solution looks promising but I tried to enter this and run it and I received the message" visual basic editor: license information for this component not found. you do not have the functionality license in this design environment"

any suggestions? also If this were to work would I be able to create a vba that did this for several searches at once?

thanks again!

What version of both Windows and Excel do you have? If you have a Mac, this method may not work.


If this were to work would I be able to create a vba that did this for several searches at once?
It searches the entire column. Otherwise, elaborate.
 
Upvote 0
I am working with a mac and it is excel 2007. I know excel is best suited for a PC - I have one at home but this a a work project so I have to figure out on my macbook

Any suggestions?
thanks again!
 
Upvote 0
Does VBA work the same between MAC and Windows?

would this code work:

Code:
Sub SearchIDs()

Dim iCell       As Range
Dim strFind()   As String
Dim i           As Integer
Dim strReplace  As String
Dim strWords    As String

strWords = "Manager|Mgr|Mngr|Mrg|Manger"

strFind = Split(strWords, "|")

strReplace = "Manager"

For Each iCell In Selection
    For i = LBound(strFind) To UBound(strFind)
        If InStr(UCase(iCell.Value), UCase(strFind(i))) > 0 Then
            iCell.Value = strReplace
            GoTo NextOne
        End If
    Next i
NextOne:
Next iCell

End Sub
 
Last edited:
Upvote 0
For future reference, if you have a question for Excel-Mac, include that version info. As you can see, I matters.

It looks like Anthony's code should work though I'm not a Mac expert.

You could also use the Advanced Filters feature.
 
Upvote 0
Upvote 0

Forum statistics

Threads
1,214,991
Messages
6,122,628
Members
449,095
Latest member
bsb1122

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