如何检查两个范围值是否相等

How to check if two ranges value is equal

如果整行有相同的值,我想合并列中的单元格。
例如。如果 A1:G1 范围与 A2:G2 相同,我想合并 A1:A2 个单元格,B1:B2 到 G1:G2。
使用下面的代码,我得到 运行 时间错误 13:类型不匹配。我假设,问题在于检查两个范围是否相等。

Dim i As Long, j As Long, row as Long
row = Cells(Rows.Count, 6).End(xlUp).row
For i = row To 7 Step -1
        If Range(Cells(i, 7), Cells(i, 24)).Value = Range(Cells(i - 1, 7), Cells(i - 1, 24)).Value Then
        For j = 7 To 24 Step 1
            Range(Cells(i, j), Cells(i - 1, j)).Merge
        Next j
        End If
Next i

问题是,如何检查两个范围值是否相等?

评论后编辑: 使用下面的代码它确实有效

Dim i As Long, j As Long, row As Long
row = Cells(Rows.Count, 6).End(xlUp).row
For i = row To 7 Step -1
        If Join(Application.Transpose(Application.Transpose(Range(Cells(i, 7), Cells(i, 24)))), Chr(0)) = Join(Application.Transpose(Application.Transpose(Range(Cells(i - 1, 7), Cells(i - 1, 24)))), Chr(0)) Then
        For j = 7 To 24 Step 1
            Range(Cells(i, j), Cells(i - 1, j)).Merge
            Application.DisplayAlerts = False
        Next j
        End If
Next i

但是,我想知道,为什么你(@Pᴇʜ) 将第一行和最后一行的函数分开。

此外,使用我的代码,在不合并单元格的情况下,我有一个用于更改单元格颜色的循环:

Dim row As Long
row = Cells(Rows.Count, 6).End(xlUp).ro
Do Until IsEmpty(Cells(row, 3))
     If row Mod 2 <> 0 Then
       Range(Cells(row, 3), Cells(row, 24)).Interior.Color = RGB(217, 225, 242)
     Else
       Range(Cells(row, 3), Cells(row, 24)).Interior.Color = xlNone
     End If
     row = row + 1
Loop

单元格合并后如何处理?

问题是……

Range(Cells(i, 7), Cells(i, 24)).Value

returns 一个值数组,但不能将一个值数组与 = 进行比较。因此,您需要遍历所有这些值并将每个值与

中的相应值进行比较
Range(Cells(i - 1, 7), Cells(i - 1, 24)).Value

因为您已经有了这个循环,只需将您的 If 语句移动到循环中:

Dim iRow As Long, iCol As Long, LastRow as Long
LastRow = Cells(Rows.Count, 6).End(xlUp).row
For iRow = LastRow To 7 Step -1
    For iCol = 7 To 24 Step 1
        If Cells(iRow, iCol).Value = Cells(iRow - 1, iCol).Value Then
            Range(Cells(iRow, iCol), Cells(iRow - 1, iCol)).Merge
        End If
    Next iCol 
Next iRow 

请注意,我将变量命名更改为更有意义的名称。这也避免了使用 Row 作为 Excel 本身已经使用的变量名。


根据评论编辑

Option Explicit

Sub Test()
    Dim RangeToMerge As Range
    Set RangeToMerge = Range("C5:F14")

    Dim FirstMergeRow As Long
    FirstMergeRow = 1

    Dim iRow As Long, iCol As Long
    For iRow = 1 To RangeToMerge.Rows.Count - 1
        If Join(Application.WorksheetFunction.Transpose(Application.WorksheetFunction.Transpose(RangeToMerge.Rows(FirstMergeRow).Value)), "|") <> _
           Join(Application.WorksheetFunction.Transpose(Application.WorksheetFunction.Transpose(RangeToMerge.Rows(iRow + 1).Value)), "|") Then
            If iRow <> FirstMergeRow Then
                For iCol = 1 To RangeToMerge.Columns.Count
                    Application.DisplayAlerts = False
                    RangeToMerge.Cells(FirstMergeRow, iCol).Resize(rowsize:=iRow - FirstMergeRow + 1).Merge
                    Application.DisplayAlerts = True
                Next iCol
            End If
            FirstMergeRow = iRow + 1
        End If
    Next iRow

    'merge last ones
    If iRow <> FirstMergeRow Then
        For iCol = 1 To RangeToMerge.Columns.Count
            Application.DisplayAlerts = False
            RangeToMerge.Cells(FirstMergeRow, iCol).Resize(rowsize:=iRow - FirstMergeRow + 1).Merge
            Application.DisplayAlerts = True
        Next iCol
    End If
End Sub

会转以下

进入

范围的 value 属性 returns 如果范围有多个单元格,则为数组。您可以在循环中比较每个元素的值,也可以使用 join() 将数组转换为字符串然后进行比较(参见 )。