Errors From Excel: Not Acting Like Strings?

  • Thread starter Thread starter (PeteCresswell)
  • Start date Start date
P

(PeteCresswell)

I'm calling a routine that does something like:
------------------------------------------------------------
Dim myResult As Variant

myResult = gExcelApp.Run("MDURATION",
mySettlementDate,
myMaturityDate,
theCoupon,
theYieldPercent,
thePaymentFrequencyID_Excel,
theBasisID_Excel
)
------------------------------------------------------------

Works fine most of the time and the "Duration" (a number like
5.7586) gets passed back to the calling routine.

But on some passed arguments, MDURATION chokes and returns an
error that *looks* like a string. e.g. "Error 2036"

And, indeed, I'm able to pass that back up the chain through
the function's Variant return value.

After the func has been called, and I look at the returned value
in the Immediate window, it still looks like a
string: "Error 2036".

The fun comes when I try to do a Debug.Print.

Just printing the variable containing the error works.
viz:
-----------------------------
debug.Print curDuration_Excel
Error 2036
-----------------------------

But when I try to concatenate a string in front of the
variable, it throws an error;
viz:
-------------------------------------------------------
debug.Print "xxx" & curDuration_Excel
(throws Runtime Error 13: Type Mismatch)
-------------------------------------------------------

Same thing if I do a:
?"XX" & curDuration_Excel


Seems like my use of a variant is covering for me most of the
time, but somehow I'm getting caught when I try to concat it with
a string.

Can anybody explain what's going on here?
 
You were trying to concatenate a String to a Variant.
When you want to do something like that, you need to cast the Variant to a
String.

Similar thing can happen with numeric calculations and Variants. In that
case you would cast the Variant with CInt() or similar numeric conversion
function. You should always cast the variant to the data type required by
your expression.

Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top