Solved

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

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
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…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

730 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