Solved

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

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
Viewers will learn the basics of slicers and timelines for both PivotTables and standard Excel tables in Excel 2013.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…

747 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

13 Experts available now in Live!

Get 1:1 Help Now