VBA - Find Multiple Criteria

Knockoutpie

Board Regular
Joined
Sep 10, 2018
Messages
116
Office Version
  1. 365
Platform
  1. Windows
How do I modify this to search for numerous different phrases instead of just one?

Rows(4).Find("Part Number", LookIn:=xlValues, lookat:=xlWhole).Offset(-1) = "CPN"
Cells.Find(What:="CPN").Activate
ActiveCell.Offset(-1, 0).Select
Do something


Instead of searching for just "Part Number", I'd like to search for either, "CPN", "Part Number", "Part Num", "P/N", or "Mfg PN"
Only one of the above phrases will ever be found.

This code searched for "Part Number" in row 4, replaces it with "CPN" and moves the active cell up 1 row.
 
Last edited:

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Correction, it does not replace the value found, rather moves up 1 cell from the found value and puts "CPN" to the new active cell.
 
Upvote 0
Try:
VBA Code:
Dim x
Dim c As Range
For Each x In Split("CPN|Part Number|Part Num|P/N|Mfg PN", "|")
    Set c = ActiveSheet.Rows(4).Find(x, LookIn:=xlValues, lookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
    
    If Not c Is Nothing Then
        c.Offset(-1) = x
        c.Offset(-1).Select
        Exit For
    End If
Next
 
Upvote 0
Try:
VBA Code:
Dim x
Dim c As Range
For Each x In Split("CPN|Part Number|Part Num|P/N|Mfg PN", "|")
    Set c = ActiveSheet.Rows(4).Find(x, LookIn:=xlValues, lookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
 
    If Not c Is Nothing Then
        c.Offset(-1) = x
        c.Offset(-1).Select
        Exit For
    End If
Next
That's very nice, thank you. My solution was to look for everything. But I like yours more.

VBA Code:
' Find Part Number Column
On Error Resume Next
    Rows(4).Find("Part Number", LookIn:=xlValues, lookAt:=xlWhole).Offset(-1) = "CPN"
    Rows(4).Find("CPN", LookIn:=xlValues, lookAt:=xlWhole).Offset(-1) = "CPN"
    Rows(4).Find("Part Num", LookIn:=xlValues, lookAt:=xlWhole).Offset(-1) = "CPN"
    Rows(4).Find("P/N", LookIn:=xlValues, lookAt:=xlWhole).Offset(-1) = "CPN"
    Rows(4).Find("Mfg PN", LookIn:=xlValues, lookAt:=xlWhole).Offset(-1) = "CPN"
On Error GoTo 0
 
Upvote 0
Try:
VBA Code:
Dim x
Dim c As Range
For Each x In Split("CPN|Part Number|Part Num|P/N|Mfg PN", "|")
    Set c = ActiveSheet.Rows(4).Find(x, LookIn:=xlValues, lookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
   
    If Not c Is Nothing Then
        c.Offset(-1) = x
        c.Offset(-1).Select
        Exit For
    End If
Next
Is there any way to adjust this to search for wildcards? Example.. using "*" below.

For Each x In Split("CPN|Part Number|Part Num|*P/N*|*Mfg PN*", "|")
 
Upvote 0
Is there any way to adjust this to search for wildcards? Example.. using "*" below.

For Each x In Split("CPN|Part Number|Part Num|*P/N*|*Mfg PN*", "|")
Yes, that would work.

VBA Code:
Dim x
Dim c As Range
For Each x In Split("CPN|Part Number|Part Num|*P/N*|*Mfg PN*", "|")
    Set c = ActiveSheet.Rows(4).Find(x, LookIn:=xlValues, lookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
    
    If Not c Is Nothing Then
        c.Offset(-1) = "CPN"  'insert CPN
        c.Offset(-1).Select
        Exit For
    End If
Next
 
Upvote 0
Solution
Yes, that would work.

VBA Code:
Dim x
Dim c As Range
For Each x In Split("CPN|Part Number|Part Num|*P/N*|*Mfg PN*", "|")
    Set c = ActiveSheet.Rows(4).Find(x, LookIn:=xlValues, lookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
   
    If Not c Is Nothing Then
        c.Offset(-1) = "CPN"  'insert CPN
        c.Offset(-1).Select
        Exit For
    End If
Next
Thank you so much, this is very helpful and I can re-use this for multiple sections!!
 
Upvote 0
You're welcome, glad to help & thanks for the feedback.:)
 
Upvote 0

Forum statistics

Threads
1,214,907
Messages
6,122,181
Members
449,071
Latest member
cdnMech

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