?
Solved

Converting macro code to function code

Posted on 2012-03-09
4
Medium Priority
?
215 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 1400 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 600 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

649 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