Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Returning from functions byval byref

Status
Not open for further replies.

kalle82

Technical User
Apr 2, 2009
163
0
0
SE
Hi!

I have gotten so much help here! And I have come a long way programming VBA with all the help I get!

Now, in a thread from yesterday I learned a really nice way of using regex. I put that piece of code in a function in MODULE 1.

Code:
Public Function regexsolution(mystring As String)
'Dim mystring As String
Dim m As Variant


With CreateObject("VBScript.Regexp")

    .IgnoreCase = True
    .Global = True
    .MultiLine = True
    .Pattern = "(#datum)([1-9]+)(#)"
    For Each m In .Execute(mystring)
        nytext = Replace(mystring, m.Value, Format(Date + CInt(m.SubMatches(1)), "long Date"))
    Next m

End With

End Function

When i press a commandbutton I call that function along with several replace. But I cant get this function to return the string. It either makes the string "" and everything is lost, or nothing happens. How can I make the function return the modified value?

This code is located in MODULE 4

Code:
Sub forhandsgranska(pages)

Dim nytext As String
Select Case pages

Case Is = 1

'**** RÄTT TEXTBOX****
nytext = UserForm1.TextBox100.Text

'******Here's the call to the function
call regexsolution(nytext)

'***also tried this code but i wont work

nytext = regexsolution(nytext)
nytext = Replace(nytext, "#datum#", Format(Date, "long Date"))


I have used the userform for storing information, but I really want to learn how to use return values, and get functions to return the values I need. I read the tutorials on microsoft...

But I can't even end the function by writing

Code:
return nytext

It just ask me for a statement?

Hope anyone can help me make this big leap!
 
You need to assign a return value to the function, something like this:
Code:
Public Function regexsolution(mystring As String) [red]as String[/red]
'Dim mystring As String
Dim m As Variant


With CreateObject("VBScript.Regexp")

    .IgnoreCase = True
    .Global = True
    .MultiLine = True
    .Pattern = "(#datum)([1-9]+)(#)"
    For Each m In .Execute(mystring)
        [red]regexsolution[/red] = Replace(mystring, m.Value, Format(Date + CInt(m.SubMatches(1)), "long Date"))
    Next m

End With

End Function
And call it like this:
Code:
nytext = regexsolution(nytext)
That is a nice way of using regex, which wonder programmer wrote that? [tongue]

Hope this helps

Andy
---------------------------------
Zebracorn: 50% Zebra, 50% Unicorn = 100% Real.

 
It's a shame the answer I gave there wasn't as good as the regex...

What I posted won't work properly, try this instead:
Code:
Public Function regexsolution(mystring As String) as String
Dim tempString As String
Dim m As Variant

tempString = mystring

With CreateObject("VBScript.Regexp")

    .IgnoreCase = True
    .Global = True
    .MultiLine = True
    .Pattern = "(#datum)([1-9]+)(#)"
    For Each m In .Execute(tempString)
        tempString = Replace(tempString, m.Value, Format(Date + CInt(m.SubMatches(1)), "long Date"))
    Next m

End With

RegexReplace = tempString

End Function
Hope this helps

Andy
---------------------------------
Zebracorn: 50% Zebra, 50% Unicorn = 100% Real.

 
Hmm!

When I run it give me "" for the nytext vairable

here how i Call it

Code:
nytext = regexsolution(nytext)

and the function NOW looks like this

Code:
Public Function regexsolution(mystring As String) As String
'Dim mystring As String
Dim m As Variant
Dim nytest As String
tempString = mystring

With CreateObject("VBScript.Regexp")

    .IgnoreCase = True
    .Global = True
    .MultiLine = True
    .Pattern = "(#datum)([1-9]+)(#)"
    For Each m In .Execute(tempString)
        nytext = Replace(tempString, m.Value, Format(Date + CInt(m.SubMatches(1)), "long Date"))
    Next m

End With

nytext = tempString

End Function

The value it returns is to make the nytext variable be to hold "".

I have stepped through the code and the string i passed to the function with no prolblems, the regex code finds the correct things and changes them, the tempstring is saved into the other variable.

But it does not get returned :(
 
You aren't actually setting the return variable to be passed back from the function, that's why it's a blank string. You don't need nytext in there at all, it will be set by the return value. Try using the function as so (and declare variables):
Code:
Public Function regexsolution(mystring As String) As String
'Dim mystring As String
Dim m As Variant
[red]Dim tempString As String[/red]

tempString = mystring

With CreateObject("VBScript.Regexp")

    .IgnoreCase = True
    .Global = True
    .MultiLine = True
    .Pattern = "(#datum)([1-9]+)(#)"
    For Each m In .Execute(tempString)
        [red]tempString[/red] = Replace(tempString, m.Value, Format(Date + CInt(m.SubMatches(1)), "long Date"))
    Next m

End With

[red]regexsolution = tempString[/red] ' here is where the return value of the function is set.

End Function
This should work better for you.

Andy
---------------------------------
Zebracorn: 50% Zebra, 50% Unicorn = 100% Real.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top