你不需要VBA。嘗試這種陣列式:
=AND(EXACT(A14:C14,A15:C15))
式中鍵入後,使用Ctrl鍵 + 移 + 輸入將其設置爲數組公式。
如果範圍相等則返回TRUE,否則返回FALSE。
如果你需要做到這一點使用VBA,試試這個:
Sub CompareTwoRanges()
Dim compareRanges As Boolean
compareRanges = ActiveSheet.Evaluate("=AND(EXACT(A14:C14,A15:C15))")
End Sub
編輯: 我不喜歡ActiveSheet,我不喜歡硬編碼的範圍,所以這裏是一個更完整的片段:
Sub CompareTwoRanges()
Dim wb As Workbook
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim rng1 As Range
Dim rng2 As Range
Set wb = ThisWorkbook
Set ws1 = wb.Sheets("Sheet1")
Set ws2 = wb.Sheets("Sheet2")
Set rng1 = ws1.Range("A14:C14")
Set rng2 = ws2.Range("N3:P3")
If rangesAreEqual(rng1, rng2, ws1, ws2) Then
MsgBox "The ranges are equal."
Else
MsgBox "Sorry. The ranges are NOT equal."
End If
End Sub
Function rangesAreEqual(rng1 As Range, rng2 As Range, _
ws1 As Worksheet, ws2 As Worksheet) As Boolean
' booleans default to false
' verify ranges have same dimensions
If rng1.Columns.Count <> rng2.Columns.Count Then Exit Function
If rng1.Rows.Count <> rng2.Rows.Count Then Exit Function
' ranges are the same size. are their contents equal?
rangesAreEqual = ws1.Evaluate("=AND(EXACT(" & ws1.Name & "!" & _
rng1.Address & "," & ws2.Name & "!" & rng2.Address & "))")
End Function
我不相信這是真的。你可以比較範圍。 @alex,你是否在我的答案中嘗試過數組公式? –
這是一個公式。我回復他使用純粹的VBA。否則,您的解決方案只是另一種解決方案 – ApplePie
我還包括一個純粹的VBA選項作爲我的答案的一部分。 –