Avatar of John Carney
John Carney
Flag for United States of America asked on

Converting macro code to function code

I have a macro that generates the string I'm using for a RegEx pattern and puts it in a cell named [RegExPattern1].

   Sub StringStrings()
   Dim i As Long, targ As Range, Ptrn As String
   Set targ = [RegExPattern1]
   targ.ClearContents
   For i = 3 To [topRegEx].End(xlDown).Row
       targ = targ & cells(i, [topRegEx].Column)
   Next i
   targ = "\d{1,3}[ /-]?(?!" & targ & ")[A-M]+"
   End Sub

After running the macro I can replace the long and periodically growing pattern in my function with this:
      .Pattern = [RegExPattern1]
However I would like to accomplish it within the function itself. Something like this:

   Dim i As Long, Ptrn As String, MyPattern As String
   For i = 3 To [topRegEx].End(xlDown).Row
       Ptrn = Ptrn & cells(i, [topRegEx].Column)
   Next i
   MyPattern = "\d{1,3}[ /-]?(?!" & Ptrn & ")[A-M]+"

Not surprisingly, it doesn't work. How would I emulate what the middle 3 lines are doing in proper function code?

Thanks,
John
Microsoft Excel

Avatar of undefined
Last Comment
John Carney

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
dlmille

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
Rory Archibald

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
John Carney

ASKER
Hi guys, thanks. Long weekend and I've got a deadline on another project, hopefully I can get back to this tomorrow morning.
John Carney

ASKER
Wow, that was a long weekend! Thanks, you guys.

- John
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes