Solved

Converting macro code to function code

Posted on 2012-03-09
4
204 Views
Last Modified: 2012-06-27
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
0
Comment
Question by:gabrielPennyback
  • 2
4 Comments
 
LVL 41

Accepted Solution

by:
dlmille earned 350 total points
ID: 37703984
Your logic in cumulating what's in the cells appears correct, and should be similar to the original sub.  Perhaps its just organizing your function, as follows?

Function myPattern(rTopRegEx As Range) As String
Dim i As Long, Ptrn As String
Dim wks As Worksheet

   Set wks = rTopRegEx.Worksheet 'lock in the right worksheet
   
   For i = 3 To rTopRegEx.End(xlDown).Row
       Ptrn = Ptrn & wks.Cells(i, rTopRegEx.Column)
   Next i
   myPattern = "\d{1,3}[ /-]?(?!" & Ptrn & ")[A-M]+"
End Function

Open in new window

Usage:
    .Pattern = myPattern([topRegEx])

Hope this helps!

Dave
0
 
LVL 85

Assisted Solution

by:Rory Archibald
Rory Archibald earned 150 total points
ID: 37708739
FYI, since you are not passing your ranges directly, you will need to make that volatile.
Also note that you cannot clear or in any way alter the [RegExPattern1] cell from your function if you are calling it from a worksheet cell.
0
 
LVL 1

Author Comment

by:gabrielPennyback
ID: 37712585
Hi guys, thanks. Long weekend and I've got a deadline on another project, hopefully I can get back to this tomorrow morning.
0
 
LVL 1

Author Closing Comment

by:gabrielPennyback
ID: 38080068
Wow, that was a long weekend! Thanks, you guys.

- John
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question