RegEx Helper / Returning Values From Function

IREALLYambatman

Board Regular
Joined
Aug 31, 2016
Messages
63
Hey guys.. so this is the first real VBA stript that I'm trying to put together and I keep getting the error "Object Required" When I try and use set in order to return a value to the main Sub? Help?

The First Sub calls the RegExHelper function that I'm trying to write like this..
Code:
StringToUseAfter = RegExHelper(StringToUseAfter, "J-FLAG", "")  'StringToWorkOn,PatternToSearchForWhatToReplaceItWith)StringToUseAfter = RegExHelper(StringToUseAfter, "MSD", "")  'StringToWorkOn,PatternToSearchForWhatToReplaceItWith)
StringToUseAfter = RegExHelper(StringToUseAfter, "MS", "")  'StringToWorkOn,PatternToSearchForWhatToReplaceItWith)
StringToUseAfter = RegExHelper(StringToUseAfter, "(?i).*\-[0-9]*.", "")  'StringToWorkOn,PatternToSearchForWhatToReplaceItWith)
StringToUseAfter = RegExHelper(StringToUseAfter, "(?i)ccv", "")  'StringToWorkOn,PatternToSearchForWhatToReplaceItWith)
StringToUseAfter = RegExHelper(StringToUseAfter, "(?i)ccb", "")  'StringToWorkOn,PatternToSearchForWhatToReplaceItWith)

The RegExHelper Function:
Code:
Public Function RegExHelper(StringToWorkOn As String, PatternToSearchFor As String, WhatToReplaceItWith As String) As String
Debug.Print StringToWorkOn
Debug.Print PatternToSearchFor
Debug.Print WhatToReplaceItWith




   Dim strPattern As String: strPattern = PatternToSearchFor
    Dim strReplace As String: strReplace = WhatToReplaceItWith
    Dim myreplace As Long
    Dim strInput As String
    Dim Myrange As Range


    Set regex = CreateObject("VBScript.RegExp")


            With regex
                .Global = True
                .MultiLine = True
                .IgnoreCase = False
                .Pattern = strPattern
            End With


            If regex.Test(StringToWorkOn) Then
                StringToWorkOn = regex.Replace(StringToWorkOn, strReplace)
                Debug.Print StringToWorkOn
                
            End If
            
    Set regex = Nothing
    Set RegExHelper = StringToWorkOn
End Function
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Figured it out.. Ooops... Set RegExHelper is not needed, the Set is for Objects (still don't know what that means) but apparently a string is not an object. working now.
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,632
Latest member
jladair

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top