Solved

vba excel compare user defined variables "Type mismatch" when compiling

Posted on 2010-09-11
3
661 Views
Last Modified: 2012-05-10
Hi,

I have a user defined type that has multiple elements. I want to be able to compare different variables of the same type but I get a "Type mismatch" error on the IF statement below when compiling.

Type MyType
    element1 As Variant
    element2 As Variant
End Type

Public Sub x()
Dim MyVar1 As MyType
Dim MyVar2 As MyType

If MyVar1 = MyVar2 Then
   MsgBox "Same"
End If

End Sub

It seems vba conditions can only be done on basic data types? Is there any way to do a comparison like this without comparing every element of the type?

Thanks

Bergsy
0
Comment
Question by:gbergsma
  • 2
3 Comments
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 33656384
Bergsy,It would appear that you have to compare down at the type-member level.  E.g.:If MyVar1.element1 = MyVar2.element1 And MyVar1.element2 = MyVar2.element2 Then   MsgBox "Same"Else   MsgBox "Oh no, not again!"End IfThe alternative: instead of using a UDT, create a class, and include a class Method for "Compare":Public Function Compare(ClassInstance1 As MyClass, ClassInstance2 As MyClass) As Boolean    Compare = (ClassInstance1.Property1 = ClassInstance2.Property1 And _        ClassInstance1.Property2 = ClassInstance2.Property2 And _        ClassInstance1.Property3 = ClassInstance2.Property3 And _        ClassInstance1.Property4 = ClassInstance2.Property4)End Function
0
 

Author Comment

by:gbergsma
ID: 33656477
Hi,

what's the difference? I can compare the UDT by data type, or the class by data type. I was hoping I could just compare the whole variable (like you can do with a C structure), but that does not seem to be possible

Thanks

Bergsy
0
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 500 total points
ID: 33656517
>>what's the difference?In the end, not much.Bottom line is, what you really want to do--compare the two variables' properties all in one go--is not possible in VBA.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question