VBA,循环遍历所有选项卡以根据单元格值隐藏行" HIDE"

时间:2015-11-20 17:29:47

标签: vba excel-vba excel

我尝试创建一个代码来隐藏基于" HIDE"的行。列中的值" AC"在所有标签上。我无法通过所有工作表来获取此代码。我知道ActiveSheet可能是问题的一部分,但它是我可以让它隐藏任何工作表上的行的唯一方法。

Sub HideRows()

    Dim WS_Count As Integer
    Dim I As Integer
    Dim cell As Range

    WS_Count = ActiveWorkbook.Worksheets.Count
    For I = 1 To WS_Count

    For Each cell In ActiveSheet.Range("AC5:AC14")
        With cell
            .EntireRow.Hidden = .Value = "HIDE"
        End With
    Next
    Next I

End Sub

1 个答案:

答案 0 :(得分:0)

此代码应该有效。

Sub HideRows()

    Dim ws As Worksheet
    Dim lastRow As Integer
    Dim actCell As Range


    For Each ws In Worksheets
        lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row 'Get the last row of the table

        For Each actCell In ws.Range("AC5:AC" & lastRow) 'Iterate from row 5 until the last row of the table, if you have an fixed range just change it to Range("AC5:AC14") for example.
            With actCell
                If ws.Range("AC" & .Row) = "HIDE" Then
                    .EntireRow.Hidden = True
                End If
            End With
        Next
    Next
End Sub