Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can compare two ranges without looping by making use of an Excel array-formula. The following code stores a value temporarily in cell A20, then produces a value of True or False to confirm if the ranges contain the same values. They are written in Excel <strong>VBA</strong>, but it should be possible to convert them to use Interop. <code>varCalc</code> (in the 2nd example) would be an Object rather than Variant.</p> <pre><code>Sub TestCompare() '{=SUM(IF($A$1:$A$4=$B$1:$B$4,1,0))=ROWS($A$1:$A$4)} Dim rngA As Range Dim rngB As Range Dim blnTheSame As Boolean Set rngA = Range("$A$1:$A$4") Set rngB = Range("$B$1:$B$4") Range("A20").FormulaArray = "=SUM(IF(" &amp; rngA.Address &amp; "=" _ &amp; rngB.Address &amp; ",1,0))=ROWS(" &amp; rngA.Address &amp; ")" blnTheSame = Range("A20").Value Range("A20").Clear MsgBox "Are they the same? " &amp; blnTheSame End Sub </code></pre> <p>The following version does the same, but works entirely in code, not requiring the use of a cell to store a temporary result:</p> <pre><code>Sub TestCompare2() '{=SUM(IF($A$1:$A$4=$B$1:$B$4,1,0))=ROWS($A$1:$A$4)} Dim rngA As Range Dim rngB As Range Dim varCalc As Variant Dim blnTheSame As Boolean Set rngA = Range("$A$1:$A$4") Set rngB = Range("$B$1:$B$4") varCalc = "=SUM(IF(" &amp; rngA.Address &amp; "=" _ &amp; rngB.Address &amp; ",1,0))=ROWS(" &amp; rngA.Address &amp; ")" blnTheSame = Evaluate(Array(varCalc)) MsgBox "Are they the same? " &amp; blnTheSame End Sub </code></pre> <p>Neither make use of <code>Value2</code>, but I can't see that this is necessary for the comparison(s).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload