根据单元格范围隐藏行

Hide rows according to range of cells

你好,我想问你一个问题。

我有两个带数字的 coll,我需要比较第一个 coll(较长)和第二个 coll(较短),如果匹配,则隐藏匹配所在的行。

到目前为止我有这个:

Sub RowHide()
Dim cell As Range
Dim CompareCells As Range

Set CompareCells = Range("I2:I18")

For Each cell In Range("A2:A200")
    If cell.Value = CompareCells Then
        cell.EntireRow.Hidden = True
    End If
Next
End Sub

我的问题是我不知道如何设置 CompareCells 的值来开始比较。我会感激每一个建议。

您必须设置 2 个单独的范围并进行比较。如果您希望每个单元格与同一行的单元格(A1 与 B1、A2 与 B2 等)进行比较,请考虑使用:

for i = 1 to something

    set cell1 = range("A" & i)
    set cell2 = range("B" & i)
    if cell1.value = cell2.value then
        'Do this, and do that!
        cell1.entirerow.hidden = true
    end if

next i

试试这个:

Sub RowHide()
    Dim Longer As Range
    Dim i As Double
    i = 2 'Initial row
    For Each Longer In Range("A2:A200")
        If Longer.Value = Cells(i,2).Value Then
            Longer.EntireRow.Hidden = True
        End If
        i = i + 1
    Next
End Sub

PS: Cells(RowIndex, ColumnIndex).Value: returns 行和列的值。 ColumnIndex => A 列 = 1,B 列 = 2,依此类推...

我研究了你的两个想法并将它们转化为一个,我终于让它发挥作用了。

这是我的最终代码:

Sub RowHide()
Dim i As Integer
Dim j As Integer

For i = 2 To 197

Set FirstRange = Range("A" & i)

  For j = 2 To 18

    If FirstRange.Value = Cells(j, 8).Value Then
       FirstRange.EntireRow.Hidden = True
    End If
  Next j

Next i
End Sub

如果有人想使用它,唯一的修改是你必须根据列中的行数更改循环中的数字。

感谢两位的指点。