Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why isn't this a type mismatch?

Tags:

excel

vba

I answered this question, apparently to OP's satisfaction, but still find their question puzzling. Their question involved an expression in which a workbook object was being concatenated with a string, triggering Run-time Error '438': Object doesn't support this property or method. You can reproduce this sort of error by simply typing

?"Hello, " & ThisWorkbook

In the Immediate Window.

My question is -- why does this raise that error, instead of a error 13 -- type mismatch? A reasonable guess is that VBA tries to find a default property for a workbook object and that a default property doesn't exist. But, if so, I would expect it to be the following error from Microsoft's list of Visual Basic 6.0 error codes: Automation object doesn't have a default value (Error 443).

It is mostly of academic interest, but if the result of concatenating an object without a default property with a string is always Error 438, and that is the only way of triggering Error 438 rather than possibly another error when concatenating a string with an object, then the following code might be of use:

Function HasDefault(O As Variant) As Boolean
    Dim i As Long
    If Not IsObject(O) Then Exit Function
    On Error Resume Next
    i = Len("Hello, " & O)
    If Err.Number = 438 Then
        HasDefault = False
    Else
        HasDefault = True
    End If
End Function

I've tested this on a variety of objects, and for those I've tested it on it has returned False exactly when _Default doesn't show up as a (hidden) member of the object when viewed in the Object Browser. Nevertheless, I don't quite trust this function and am still puzzled by what is going on here.

like image 590
John Coleman Avatar asked Jun 21 '16 11:06

John Coleman


People also ask

How do I fix type mismatch error?

How to Fix Type Mismatch (Error 13) The best way to deal with this error is to use to go to the statement to run a specific line of code or show a message box to the user when the error occurs. But you can also check the court step by step before executing it.

What is type mismatch?

Sometimes, while writing code, programmers may ask that a value be stored in a variable that is not typed correctly, such as putting a number with fractions into a variable that is expecting only an integer. In such circumstances, the result is a type-mismatch error.

How do I fix type mismatch error in VBA?

Step 1: Write the subprocedure for VBA Type Mismatch. Step 2: Again assign a new variable, let's say “A” as Byte data type. Let's understand the Byte Data type here. Byte can only store the numerical value from 0 to 255.

What is type mismatch in Excel?

VBA Type Mismatch Explained A VBA Type Mismatch Error occurs when you try to assign a value between two different variable types. The error appears as “run-time error 13 – Type mismatch”. For example, if you try to place text in a Long integer variable or you try to place text in a Date variable.


1 Answers

VBA will try and convert the expressions on each side of the & operator to a data value. The language spec states that:

  • If the value type of the expression’s target variable is a class:
  • If the declared type of the target is Variant, runtime error 9
    (Subscript out of range) is raised.
  • If the declared type of the target is not Variant, and the target has a public default Property Get or function, the data value’s value is the result of invoking this default member for that target with this argument list. This consumes the argument list.
  • Otherwise, runtime error 438 (Object doesn’t support this property or method) is raised.

As regards your function, I'd just use:

callbyname(O, "_Default", VbGet)

which will raise a 438 error as appropriate.

like image 154
Rory Avatar answered Oct 07 '22 09:10

Rory