Solved

Converting macro code to function code

Posted on 2012-03-09
4
200 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Wow, that was a long weekend! Thanks, you guys.

- John
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

What is a Form List Box? (skip if you know this) The forms List Box is the alternative to the ActiveX list box. If you are using excel 2007, you first make sure you have a developer tab (click the Orb)->"Excel Options"->Popular->"Show Developer tab…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now