ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   ByRef argument type mismatch (repost) (https://www.excelbanter.com/excel-programming/450752-byref-argument-type-mismatch-repost.html)

Robert Crandal[_3_]

ByRef argument type mismatch (repost)
 
I get a compile error, ByRef argument type mismatch,
with the following (streamlined) code. How can I fix this?

BTW, Gary, I have tried all variations of the TruncateString()
function that you posted. It still produces the same error.
Hopefully this simplified or stripped down code and help
narrow down the problem.

'--------------------------------------------------------------
Function TruncateString$(sText$, sFind$, Optional lStart& = 1)
TruncateString = Mid$(sText, lStart, InStr(sText, sFind) - lStart)
End Function
'--------------------------------------------------------------
Sub basicTest()
Dim s As String
Dim vData ' variant

s = "Hello world. Be the world. Goodnight world."
vData = Split(s, ".")

MsgBox TruncateString(vData(2), "world")

End Sub





Claus Busch

ByRef argument type mismatch (repost)
 
Hi Robert,

Am Thu, 2 Apr 2015 01:18:39 -0700 schrieb Robert Crandal:

Function TruncateString$(sText$, sFind$, Optional lStart& = 1)


Function TruncateString$(sText, sFind$, Optional lStart& = 1)

your sText is typ variant and not string


Regards
Claus B.
--
Vista Ultimate / Windows7
Office 2007 Ultimate / 2010 Professional

Robert Crandal[_3_]

ByRef argument type mismatch (repost)
 
"Claus Busch" :

Function TruncateString$(sText$, sFind$, Optional lStart& = 1)


Function TruncateString$(sText, sFind$, Optional lStart& = 1)

your sText is typ variant and not string


Ughh. I swear I tried both variations and it was making errors
both ways.

Now that I removed the "$" from the sText paramater it seems to
be working. Ugh! I swear that I had tried both variations, and it
seemed to be making errors both ways, but now it works.

Thanks Claus (and Gary) for your help on this. Pardon my
newbie ways! 8P




GS[_2_]

ByRef argument type mismatch (repost)
 
I get a compile error, ByRef argument type mismatch,
with the following (streamlined) code. How can I fix this?

BTW, Gary, I have tried all variations of the TruncateString()
function that you posted. It still produces the same error.
Hopefully this simplified or stripped down code and help
narrow down the problem.

'--------------------------------------------------------------
Function TruncateString$(sText$, sFind$, Optional lStart& = 1)
TruncateString = Mid$(sText, lStart, InStr(sText, sFind) - lStart)
End Function
'--------------------------------------------------------------
Sub basicTest()
Dim s As String
Dim vData ' variant

s = "Hello world. Be the world. Goodnight world."
vData = Split(s, ".")

MsgBox TruncateString(vData(2), "world")

End Sub


CStr(vData(2)) is what the function is expecting...

MsgBox TruncateString(CStr(vData(2)), "world")


While Claus' suggestion handles that you passed a variant where a
string is required is correct. However, I would leave the arg type as
string because the function manipulates strings by design AND name, to
return a string. Mixing data types is going to cause confusion at some
point, and so should be avoided (IMO)!

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion



Claus Busch

ByRef argument type mismatch (repost)
 
Hi Garry,

Am Thu, 02 Apr 2015 10:32:28 -0400 schrieb GS:

However, I would leave the arg type as
string because the function manipulates strings by design AND name, to
return a string. Mixing data types is going to cause confusion at some
point, and so should be avoided (IMO)!


then he could put in vData by Value instead of reference:

Function TruncateString$(ByVal sText$, sFind$, Optional lStart& = 1)


Regards
Claus B.
--
Vista Ultimate / Windows7
Office 2007 Ultimate / 2010 Professional

GS[_2_]

ByRef argument type mismatch (repost)
 
Robert,
Please use the latest version of the TruncateString$() function that
has the If..Then construct.

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion



GS[_2_]

ByRef argument type mismatch (repost)
 
Hi Garry,

Am Thu, 02 Apr 2015 10:32:28 -0400 schrieb GS:

However, I would leave the arg type as
string because the function manipulates strings by design AND name,
to return a string. Mixing data types is going to cause confusion
at some point, and so should be avoided (IMO)!


then he could put in vData by Value instead of reference:

Function TruncateString$(ByVal sText$, sFind$, Optional lStart& = 1)


Regards
Claus B.


Correct! My bad for not properly converting this from VBS to VB...

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion



GS[_2_]

ByRef argument type mismatch (repost)
 
Properly converted...

Function TruncateString$(ByVal sText$, ByVal sFind$, Optional lStart& =
1)
If InStr(sText, sFind) 0 Then
TruncateString = Mid$(sText, lStart, InStr(sText, sFind) - lStart)
Else
TruncateString = sText
End If
End Function

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion




All times are GMT +1. The time now is 01:54 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com