Errors From Excel: Not Acting Like Strings?

  • Follow


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?
-- 
PeteCresswell
0
Reply PeteCresswell 9/20/2007 12:02:03 AM

Try: debug.Print "xxx" & CStr(curDuration_Excel)

Steve

"(PeteCresswell)" wrote:

> 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?
> -- 
> PeteCresswell
> 
0
Reply Utf 9/20/2007 12:22:01 AM


Per SteveM:
>Try: debug.Print "xxx" & CStr(curDuration_Excel)

Bingo!   Worked like a charm.

Thanks.


But why?   Some kind of special data type "Error"?  
-- 
PeteCresswell
0
Reply PeteCresswell 9/20/2007 12:29:41 AM

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

"(PeteCresswell)" wrote:

> Per SteveM:
> >Try: debug.Print "xxx" & CStr(curDuration_Excel)
> 
> Bingo!   Worked like a charm.
> 
> Thanks.
> 
> 
> But why?   Some kind of special data type "Error"?  
> -- 
> PeteCresswell
> 
0
Reply Utf 9/20/2007 12:56:01 AM

3 Replies
217 Views

(page loaded in 0.102 seconds)


Reply: