根据单元格范围隐藏行

时间:2015-08-10 13:20:21

标签: excel-vba compare range row hide

美好的一天,我很乐意问你一个问题。

我有两个带数字的colls,我需要比较第一个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的值来开始比较。我会感激你的每一条建议。

3 个答案:

答案 0 :(得分:0)

您必须设置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

答案 1 :(得分:0)

试试这个:

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:返回行和列的值。 ColumnIndex => A列= 1,B列= 2,等等......

答案 2 :(得分:0)

我调查了你们两个人的想法并将它们转换成了一个,我终于让它发挥作用了。

这是我的最终代码:

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

只有在有人想要使用它时才进行修改是你必须根据列中的行数更改周期中的数字。

感谢你们两位的建议。

相关问题