question

ScottChou-7692 avatar image
0 Votes"
ScottChou-7692 asked ScottChou-7692 answered

Need Help fixing VBA Type Mismatch Error passing a Double as argument

It seems so simple but I've tried many variations of this and I can't get it to work. It immediately returns a compile error on this line:

Sub SplitAdjustment(split As Double)
. . .
End Sub


at the calling end, I have:

Dim split As Double
Call SplitAdjustment(CDbl(split))

I started out with just Call SplitAdjustment(split) but failed
also tried using ByVal split at either end and at both ends but failed

office-vba-dev
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

ScottChou-7692 avatar image
0 Votes"
ScottChou-7692 answered

I found the problem and it appears to be a rookie mistake on my part. The yellow highlighted debugger stopping point at the subroutine header was a coincidence. The error was INside the subroutine. A very straightforward ByRef error

Sorry and thanks for your time!

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

TvanStiphout avatar image
0 Votes"
TvanStiphout answered

Sub SplitAdjustment(split As Double)

I would CERTAINLY use ByVal in this case, because presumably the procedure has no intention of returning a changed "split" to the caller.
But it does not explain the main issue :-(

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

TvanStiphout avatar image
0 Votes"
TvanStiphout answered

The CDbl wrapper is superfluous. Remove it.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

TvanStiphout avatar image
0 Votes"
TvanStiphout answered

This code looks OK to me. The problem may be somewhere else. Set breakpoints and carefully step through.

You do have Option Explicit in place per best practices, and your code does compile (Debug > Compile), correct?

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.