Function: String as input

mtheriault2000

Well-known Member
Joined
Oct 23, 2008
Messages
826
Hello again

I want to create a function to manipulate strings
Code:
Public Function NextBartime(ByRef t1 As String, ByRef t2 As String) As String

' Part of the code by  Rick Rothstein

Dim Realt1, realt2 As Date
Dim Newtime As String

    Realt1 = CDate(Format(t1, "@@@@-@@-@@ @@@@@@@@"))
    realt2 = CDate(Format(t2, "@@@@-@@-@@ @@@@@@@@"))
    NexBartime = Format((realt2 - Realt1) + realt2, "yyyymmdd hh:mm:ss")
End Function

Sub testnextbar()
Dim t1, t2, rt As String

t1 = "20110922 18:57:00"
t2 = "20110922 19:00:00"

'Call the function 
rt = NextBartime(t1, t2)

End Sub

My Sub give an error on T1 as an incompatible argument

Any idea where I miss the point???

Martin
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Hello again

I want to create a function to manipulate strings
Code:
Public Function NextBartime(ByRef t1 As String, ByRef t2 As String) As String

' Part of the code by  Rick Rothstein

Dim Realt1, realt2 As Date
Dim Newtime As String

    Realt1 = CDate(Format(t1, "@@@@-@@-@@ @@@@@@@@"))
    realt2 = CDate(Format(t2, "@@@@-@@-@@ @@@@@@@@"))
    NexBartime = Format((realt2 - Realt1) + realt2, "yyyymmdd hh:mm:ss")
End Function

Sub testnextbar()
Dim t1, t2, rt As String

t1 = "20110922 18:57:00"
t2 = "20110922 19:00:00"

'Call the function 
rt = NextBartime(t1, t2)

End Sub

My Sub give an error on T1 as an incompatible argument

Any idea where I miss the point???

Martin

Resolve by modifying Byref to ByVal in the function definition

Martin
 
Upvote 0
This statement
Code:
Dim t1, t2, rt As String
is declaring t1 as Variant, t2 as Variant and rt As String.

Try
Code:
Dim t1 As String, t2 As String, rt As String

BTW, in the last line of the function NexBartime is missing a "t".
 
Upvote 0
I just saw that you fixed it with ByVal.
That's clever, by making a new variable address, you forced Excel to evaluate the funtion arguments, triggering the automatic type conversion.

Slick solution, I'll have to remember that one.
 
Upvote 0
Hello Mike

Did learned two thing

Code:
Dim T1, T,T2 ,RT as string
Create variable with a different status. I did the test with
T1 = 2 Return: 2
RT = 3 Return : "3"

by making a new variable address, you forced Excel to evaluate the funtion arguments, triggering the automatic type conversion.
That was not intentional..... But you putting emphases on that made me understand that point

Have a great day

Martiin :)
 
Upvote 0

Forum statistics

Threads
1,224,606
Messages
6,179,865
Members
452,948
Latest member
UsmanAli786

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