Solved

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

Posted on 2010-09-11
3
652 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
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 …
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

896 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now