Please test this code to check if a variable number of columns are all equal

JenniferMurphy

Well-known Member
Joined
Jul 23, 2011
Messages
2,687
Office Version
  1. 365
Platform
  1. Windows
I have some tables that might have two or more columns with data that should be equal. Here is a simple example with small integers. Rows 6 & 9 have all equal values. In Row 7, Value3 is not equal. In Row 8, Value 2 is not equal. I will explain the "null parameter" comment shortly. In Row 10, Value4 is not equal. In Row 11, Value2 is flagged, but it is Value1 that is not equal. This is because the code that does this compares the first value t all of the rest.

image.png


The next 3 mini-sheets demonstrate the code in the table above and two others with different types of data.
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
For some reason, I cannot unhide columns G-N. When I select those columns, the Unhide icon is greyed it. What could be causing that?
 
Upvote 0
Ok, my bad. I was trying to unhide columns using the workbook hide icon. 🤔🤨😯🙃

Ok, here's that first table with examples of several ways to call my UDF (EqualAll). It can be called with a variable number of columns and/or ranges of columns.

Equal Tests.xlsx
CDEFGHIJKLMNO
5Value1Value2Value3Value4ListRangeMixedRepeat CellsRepeat Range()(,V1:V4)(V1:V3,,V4)Comments
61111EqualEqualEqualEqualEqual#VALUE!#VALUE!#VALUE!All equal
72232$E$7$E$7$E$7$E$7$E$7#VALUE!#VALUE!$E$7Value3 is not equal
83433$D$8$D$8$D$8$D$8$D$8#VALUE!#VALUE!$D$8Value2 is not equal before null parameter
94444EqualEqualEqualEqualEqual#VALUE!#VALUE!#VALUE!All equal
105556$F$10$F$10$F$10$F$10$F$10#VALUE!#VALUE!#VALUE!Value4 is not equal
117666$D$11$D$11$D$11$D$11$D$11#VALUE!#VALUE!$D$11Value2 is flagged before null parameter
Integers
Cell Formulas
RangeFormula
G6:G11G6=equalall([@Value1],[@Value2],[@Value3],[@Value4])
H6:H11H6=equalall(Table2[@[Value1]:[Value4]])
I6:I11I6=equalall([@Value1],Table2[@[Value2]:[Value3]],[@Value4])
J6:J11J6=equalall([@Value1],[@Value2],[@Value3],[@Value4],[@Value1],[@Value2],[@Value3],[@Value4],[@Value1],[@Value2],[@Value3],[@Value4])
K6:K11K6=equalall(Table2[@[Value1]:[Value2]],Table2[@[Value3]:[Value4]],Table2[@[Value1]:[Value4]],Table2[@[Value2]:[Value4]],Table2[@[Value1]:[Value3]])
L6:L11L6=equalall()
M6:M11M6=equalall(,Table2[@[Value1]:[Value4]])
N6:N11N6=equalall(Table2[@[Value1]:[Value3]],,[@Value4])


Here is another example with real numbers in different formats. The two Err columns are used to cause one cell in that row to be off a little.

Equal Tests.xlsx
CDEFGHIJ
5ExpressionV2 ErrV3 ErrValue1Value2Value3ListRange
6=3/70.0000000.0000000.4285714.29E-010.43EqualEqual
7=3^2.340.0000010.00000013.0756641.31E+0113.08$G$7$G$7
8=PI()^20.000000-0.0000019.8696049.87E+009.87$H$8$H$8
9=LOG(123^123)0.0000000.000000257.0583292.57E+02257.06EqualEqual
Real Numbers
Cell Formulas
RangeFormula
F6:F9F6=EvalText([@Expression])
G6:G9G6=EvalText([@Expression])+[@[V2 Err]]
H6:H9H6=EvalText([@Expression])+[@[V3 Err]]
I6:I9I6=equalall([@Value1],[@Value2],[@Value3])
J6:J9J6=equalall(Table25[@[Value1]:[Value3]])


This is the final example using dates in different formats. The date formats are abve the columns.

Equal Tests.xlsx
CDEFGHIJ
4mm/dd/yymmm d,yyyydd/mm/yymmmm dd, yy
5Date2 ErrDate3 errDate1Date2Date3Date4ListRange
60.0000000.00000008/24/24Aug 24, 202424/08/24August 24, 24EqualEqual
70.5000000.00000007/04/24Jul 4, 202404/07/24July 04, 24$F$7$F$7
80.0000000.00000001/01/24Jan 1, 202401/01/24January 01, 24EqualEqual
90.0000000.00010002/03/20Feb 3, 202003/02/20February 03, 20$G$9$G$9
100.0000000.00000003/14/24Mar 14, 202414/03/24March 14, 24EqualEqual
Dates
Cell Formulas
RangeFormula
F6:F10F6=[@Date1]+[@[Date2 Err]]
G6:G10G6=[@Date1]+[@[Date3 err]]
H6:H10H6=[@Date1]
I6:I10I6=equalall([@Date1],[@Date2],[@Date3],[@Date4])
J6:J10J6=equalall(Table368[@[Date1]:[Date4]])


And here is the code

VBA Code:
'================================================================================================
'                 Test if Multiple Cells are All Equal

'  Compare a variable number of cells to see if they are all equal.

'  Parameters
'     pCells   Variable number of cells to compare.

'  Parameters can be single cells, ranges, or a mix.
'  Returns "Equal" if all cells are equal.
'  Returns the cell address of the first unequal cell.
'  Returns a #VALUE! error if no parameters passed (EqualAll()).
'  Returns "Equal" if passed a single cell.
'  Returns a #VALUE! error if it encounters a null parameter before a unequal value.

'  08/23/24 Created to test ERA ratification dates.
'================================================================================================
Function EqualAll(ParamArray pCells() As Variant) As Variant

Dim value1 As Variant   'First value, all others compared to it
Dim cell As Range       'Next value in a range parameter
Dim i As Long           'Loop index
    
' Return #VALUE! error if no parameters are passed
If UBound(pCells) < 0 Then
   EqualAll = CVErr(xlErrValue)
   Exit Function
End If

' Load value1 with the first value
If TypeName(pCells(0)) = "Range" Then      'If it's a range parameter,
    value1 = pCells(0).cells(1, 1).Value      'Get the first value
Else                                      'If it's a simple cell,
    value1 = pCells(0)                        'Get that value
End If

' Loop through all the parameters comparing them to the first one (value1)
' It will compare value1 to itself on the first iteration
' This is to avoid the range test on each loop
For i = LBound(pCells) To UBound(pCells)
   If TypeName(pCells(i)) = "Range" Then  'If this parameter is a range,
      For Each cell In pCells(i)             'Loop through the range
         If cell.Value <> value1 Then           'If it's not a match,
            EqualAll = cell.Address                'Return the cell address
            Exit Function
         End If
      Next cell
   Else                                   'If it's a simple cell
      If pCells(i) <> value1 Then            'If it's not a match,
         EqualAll = Application.Caller.Address  'Return the cell address
         Exit Function
      End If
   End If
Next i
    
' Return "Equal" if all values match
EqualAll = "Equal"

End Function

I would appreciate any comments, especially bugs or improvements.
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,207
Members
452,618
Latest member
Tam84

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top