VB and VBA Users Source Code: Determine if any two variables are different
[
Home
|
Contents
|
Search
|
Reply
| Previous | Next ]
VB/VBA Source Code
Determine if any two variables are different
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Wednesday, March 21, 2001
Hits:
561
Category:
Visual Basic General
Article:
The following routines can be used to determine if two variables are identical. 'Purpose : Compares any two variants, including arrays, objects etc. 'Inputs : vItem1 The first item to test. ' vItem2 The second item to test. 'Outputs : Returns True if the variables are identical. 'Author : Andrew Baker 'Date : 20/Mar/2001 21:40 'Notes : 'Assumptions : Function IsSame(vItem1 As Variant, vItem2 As Variant) As Boolean Dim lThisRow As Long, lThisCol As Long, lNumCols As Long On Error GoTo ErrFailed If IsArray(vItem1) Then If Not IsArray(vItem2) Then Exit Function End If If LBound(vItem1) <> LBound(vItem2) Then Exit Function End If If UBound(vItem1) <> UBound(vItem2) Then Exit Function End If If ArrayNumDimensions(vItem1) <> ArrayNumDimensions(vItem2) Then Exit Function End If If ArrayNumDimensions(vItem1) = 1 Then For lThisRow = LBound(vItem1) To UBound(vItem1) If Not IsSame(vItem1(lThisRow), vItem2(lThisRow)) Then Exit Function End If Next Else lNumCols = UBound(vItem1, 2) For lThisRow = LBound(vItem1) To UBound(vItem1) For lThisCol = LBound(vItem1, 2) To lNumCols If Not IsSame(vItem1(lThisRow, lThisCol), vItem2(lThisRow, lThisCol)) Then Exit Function End If Next Next End If 'Items are identical IsSame = True ElseIf IsObject(vItem1) Then IsSame = (vItem1 Is vItem2) Else If Not IsArray(vItem2) Then If IsNull(vItem1) Then IsSame = IsNull(vItem2) Else IsSame = (vItem1 = vItem2) End If End If End If ErrFailed: On Error GoTo 0 End Function 'Purpose : Calculates the number of dimensions in an array 'Inputs : avInArray. The array to evaluate. 'Outputs : The number of dimensions the array has. 'Author : Andrew Baker 'Date : 25/03/2000 'Notes : Function ArrayNumDimensions(avInArray As Variant) As Long Dim lNumDims As Long If IsArray(avInArray) Then On Error GoTo ExitSub Do lNumDims = UBound(avInArray, ArrayNumDimensions + 1) ArrayNumDimensions = ArrayNumDimensions + 1 Loop End If ExitSub: On Error GoTo 0 End Function
Terms and Conditions
Support this site
Download a trial version of the Excel Workbook Rebuilder