VBA search script to ignore certain characters

dmj120

Active Member
Joined
Jan 5, 2010
Messages
308
Office Version
  1. 365
  2. 2019
  3. 2010
This is a follow up to my original post. The script DanteAmor provided works great, but I'm wondering if it can be tweaked a bit so special characters (spaces, dashes, periods, slashes, quotation marks) in the "mdl# search" can be ignored and still find the correct item - which are all standardized to replace slashes, dashes, periods, with a 'space' and a space is added between a letter and number.

Below is one example I screenshotted for a visual representation. Here's a couple other possible variations for

Mdl Search termResult (what needs to be 'found')
2.0CW2 0 CW
15M1815 M 18
1-A1/DE1 A 1 DE
S5-15S 5 15
7V 3-C7 V 3 C
TP750TP 750
3D Alpha 1.2V3 D Alpha 1 2 V


1624380013672.png


VBA Code:
Sub Search_criteria()
  Dim sh1 As Worksheet, sh2 As Worksheet
  Dim a As Variant, b As Variant
  Dim i As Long, j As Long
  Dim cad1 As String, cad2 As String, cad3 As String
  '
  Set sh1 = Sheets("MCT")
  Set sh2 = Sheets("search")
  '
  a = sh1.Range("A1:C" & sh1.Range("A:C").Find("*", , xlValues, , 1, 2).Row).Value2
  sh2.Range("A8:C" & Rows.Count).ClearContents
  ReDim b(1 To UBound(a, 1), 1 To 3)
  
  If sh2.[B2] <> "" Then cad1 = "*" & LCase(sh2.[B2].Value) & "*"
  If sh2.[B3] <> "" Then cad2 = "*" & LCase(sh2.[B3].Value) & "*"
  If sh2.[B4] <> "" Then cad3 = "*" & LCase(sh2.[B4].Value) & "*"
  
  For i = 1 To UBound(a, 1)
    If LCase(a(i, 1)) Like cad1 And LCase(a(i, 2)) Like cad2 And LCase(a(i, 3)) Like cad3 Then
      j = j + 1
      b(j, 1) = a(i, 1)
      b(j, 2) = a(i, 2)
      b(j, 3) = a(i, 3)
    End If
  Next i
  
  If j > 0 Then sh2.Range("A8").Resize(j, 3).Value = b
  
'moves to top of displayed list
  Range("A8").Select
  

 
End Sub
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.

Forum statistics

Threads
1,223,911
Messages
6,175,335
Members
452,636
Latest member
laura12345

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