VBA显示行如果值

时间:2018-08-11 20:57:19

标签: excel vba

我只想选择值未选中的行,但是由于类型不匹配而出现错误。请告知

Sub Hide_Rows()

    If Range("D3:F100").Value = "Checked" Then
        Rows("3:100").EntireRow.Hidden = True
    ElseIf Range("D3:F100").Value = "Unchecked" Then
        Rows("3:").EntireRow.Hidden = False
    End If

End Sub

1 个答案:

答案 0 :(得分:1)

您不能将值数组测试为一个值:

  

If Range("D3:F100").Value = "Checked" Then

将始终出错。您需要循环检查每个值:

如果D,E,F列中的任何值为Unchecked,则将取消隐藏该行。

Sub Hide_Rows()


ActiveSheet.Range("3:100").EntireRow.Hidden = True
Dim i As Long
For i = 3 To 100
    Dim j As Long
    For j = 4 To 6
        If ActiveSheet.Cells(i, j) = "Unchecked" Then
            Dim UnRng As Range
            If UnRng Is Nothing Then
                Set UnRng = ActiveSheet.Rows(i)
            Else
                Set UnRng = Union(UnRng, ActiveSheet.Rows(i))
            End If
            Exit For
        End If
    Next j
Next i

UnRng.EntireRow.Hidden = False

End Sub