Substitute with Wildcards

Alex0013

Board Regular
Joined
Jul 23, 2014
Messages
158
Office Version
  1. 365
Platform
  1. Windows
Hey All,

I'm wondering if someone would be able to help me with a UDF. What I'm trying to do is basically have an Excel Substitute formula that takes wildcards.

Example:

A1: example#00ff00ColorTextHere

=SubstituteWild(A1,"#??????","#??????>")

In my example, I essentially want to insert a ">" symbol after each hex color code in my string. "A1" might have several hex codes in the 1 cell, so I can't just use mid functions, I want to use substitute so it evaluates the whole string.

I'm guessing the UDF would have to find "#", and then look at the next 6 digits, store them, then replace them again with the new "format".

Ideally, making it dynamic so it can be used elsewhere would be awesome (aka, make it treat "?" as a true wildcard), but hard coding everything to always assume hex codes and 6 characters would at least get me by for this project.

Any ideas?

Thanks!
Alex
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Hey Jonmo1,

Wow, that was super quick! This will do the trick! Super easy way to solve my issue at least, so it works for me!

Enjoy the rest of your day!
Alex
 
Upvote 0
Actually...Sorry I spoke to soon. It only works on the first instance of #. It didn't work if it happens multiple times
 
Upvote 0
Ahh, ok. Not sure how to do that.

Substitute doesn't work with wildcards (as you've discovered).
 
Upvote 0
Yeah, that's why I'm hoping a UDF pro might be able to help me. I'm trying to learn VBA a bit myself, but this one is stumping me...
 
Upvote 0
Hi Alex

Try:

Code:
Function AddPlus(s As String, sAdd As String)

With CreateObject("VBScript.RegExp")
    .Pattern = "#[0-9a-fA-F]{6}"
    .Global = True
    AddPlus = .Replace(s, "$&" & sAdd)
End With
End Function

Use like:

=AddPlus(A2,">")
 
Upvote 0
That's much better than what I was working on.
I haven't gotten a grip on RegExp yet.
 
Upvote 0
Hey PGC,

That works great! Would you be willing to explain a little of it for me? Just so I understand exactly what it's doing?

What is the [0-9a-fA-F]? Also what does .Global=True do? And lastly, the "$&"?

Sorry for the questions, just trying to understand VBA better!

Thanks,
Alex
 
Upvote 0
Not that it matters anymore, but here's what I did.
Maybe at least it will be easier to understand.

Code:
Public Function MyFind(c As Variant)
Dim MyStr As String, x As Long
MyStr = c
Do
    x = InStr(1 + x, MyStr, "#")
    If x = 0 Then Exit Do
    MyStr = Application.Replace(MyStr, x + 7, 0, ">")
Loop
MyFind = MyStr
End Function

Then use in a formula as
=MyFind(A1)
 
Upvote 0

Forum statistics

Threads
1,221,481
Messages
6,160,080
Members
451,616
Latest member
swgrinder

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