Formatting substring text in cells, array input not working

KyleC123

New Member
Joined
Jun 7, 2016
Messages
8
Hi all, I pinched the below bit of code from somewhere, I can't remember where. It formats designated substrings within the cells you select manually, and works awesome. However, it requires me to manually type in all the substrings in VBA.

I tried to change this to accept values from a range of cells, but it gives me the error: "Invalid procedure call or argument" at the line colored in red (I think). I have colored the original, working code in green, and my attempted substitute in pink. I obviously don't run the old and new code at the same time, it just seemed easiest to display this way.

It seems a pretty straightforward substitution, I have no idea why it throws this error. Anyone have any thoughts and/or solutions? Thanks very much!



Sub FindAndFormatOnlySpecificTextInCell()
Dim r As Range
Dim match As Variant
Dim keywords As Variant

keywords = Range("a1:a20").Value
keywords = Array("Joe", "John", "Jane")

On Error GoTo error_and_exit
Application.EnableEvents = False

With CreateObject("VBScript.RegExp")
.Pattern = "(.*?)(" & Join(keywords, "|") & ")"
.Global = True
.IgnoreCase = True

For Each r In Selection.Cells
If Not r.HasFormula Then
For Each match In .Execute(r)
With match
If Trim(Left(r, .FirstIndex + Len(.Submatches(0)))) <> "" Or _
Trim(Mid(r, .FirstIndex + .Length + 1)) <> "" Then
With r.Characters(.FirstIndex + Len(.Submatches(0)) + 1, Len(.Submatches(1)))
.Font.Size = 12
.Font.Color = RGB(255, 0, 0)
.Font.Bold = True
End With
End If
End With
Next
End If
Next
End With

error_and_exit:
If Err Then MsgBox Err.Number & ": " & Err.Description
Application.EnableEvents = True
End Sub
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
This line of code...

keywords = Range("a1:a20").Value

makes the keywords variable a two-dimensional array (20 rows, 1 column). The Join function only works on one-dimensional arrays. This is untested (because I do not do Regular Expressions) so I don't know if the overall code will work with it or not, but changing the above line of code to this...

keywords = Application.Transpose(Range("A1:A20").Value)

will make the keywords variable a proper one-dimensional array which Join can work with.
 
Upvote 0
Welp...I don't fully understand the distinction between a one column array and a one dimensional array, so I never, ever would have caught that!

It works a treat though. Thanks tons for the quick reply and awesome solution!
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,750
Members
448,989
Latest member
mariah3

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