Solved

Converting macro code to function code

Posted on 2012-03-09
4
207 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 42

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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Suggested Solutions

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
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…

740 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