?
Solved

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

Posted on 2010-09-11
3
Medium Priority
?
674 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 93

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 93

Accepted Solution

by:
Patrick Matthews earned 2000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
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…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

764 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