Excel VBA-基于多列中的值隐藏行

Excel VBA-基于多列中的值隐藏行,excel,vba,Excel,Vba,我正在尝试我的第一段代码和VBA,并且已经研究了一段时间,但我无法让它工作。我试图隐藏多个列的值为0(具有公式/数值的列)或保留为空(文本)的行。在我的Excel中,第9-1000行的O、AB、AN列包含数值以及AJ和AK 9-1000文本值。所以我想隐藏所有O,AB和AN为零,AJ和AK为空的地方。我可以根据第一个条件编写隐藏行的基本代码: Sub HideAccounts() Dim TotExp As Range Dim Cell As Range Set TotExp = Range

我正在尝试我的第一段代码和VBA,并且已经研究了一段时间,但我无法让它工作。我试图隐藏多个列的值为0(具有公式/数值的列)或保留为空(文本)的行。在我的Excel中,第9-1000行的O、AB、AN列包含数值以及AJ和AK 9-1000文本值。所以我想隐藏所有O,AB和AN为零,AJ和AK为空的地方。我可以根据第一个条件编写隐藏行的基本代码:

Sub HideAccounts()

Dim TotExp As Range
Dim Cell As Range

Set TotExp = Range("O9:O1000")

Application.ScreenUpdating = False

For Each Cell In TotExp.Rows
If Cell.Value = 0 Then
Cell.EntireRow.Hidden = True
Else
Cell.EntireRow.Hidden = False

End If

Application.ScreenUpdating = True

Next Cell

End Sub
Option Explicit

Public Sub HideAccounts()
    Dim i As Long, hide As Boolean

    Application.ScreenUpdating = False
    With ThisWorkbook.Sheets(1)         '<-- set this to the relevant sheet
        For i = 9 To 1000
            hide = (.Range("O" & i).Value2 = 0)
            hide = hide And (.Range("AB" & i).Value2 = 0)
            hide = hide And (.Range("AN" & i).Value2 = 0)
            hide = hide And (Len(.Range("AJ" & i).Value2) = 0)
            hide = hide And (Len(.Range("AK" & i).Value2) = 0)
            .Rows(i).EntireRow.Hidden = hide
        Next
    End With
    Application.ScreenUpdating = True
End Sub
所以现在我想添加其他条件,但我正在与多个条件作斗争,当我尝试插入GoTo语句时,它也不起作用

非常感谢您的帮助

谢谢,
Daniela

以下是一种包含多个标准的方法:

Sub HideAccounts()

Dim TotExp As Range
Dim Cell As Range

Set TotExp = Range("O9:O1000")

Application.ScreenUpdating = False

For Each Cell In TotExp.Rows
If Cell.Value = 0 Then
Cell.EntireRow.Hidden = True
Else
Cell.EntireRow.Hidden = False

End If

Application.ScreenUpdating = True

Next Cell

End Sub
Option Explicit

Public Sub HideAccounts()
    Dim i As Long, hide As Boolean

    Application.ScreenUpdating = False
    With ThisWorkbook.Sheets(1)         '<-- set this to the relevant sheet
        For i = 9 To 1000
            hide = (.Range("O" & i).Value2 = 0)
            hide = hide And (.Range("AB" & i).Value2 = 0)
            hide = hide And (.Range("AN" & i).Value2 = 0)
            hide = hide And (Len(.Range("AJ" & i).Value2) = 0)
            hide = hide And (Len(.Range("AK" & i).Value2) = 0)
            .Rows(i).EntireRow.Hidden = hide
        Next
    End With
    Application.ScreenUpdating = True
End Sub
选项显式
公共子HideAccounts()
模糊的i长,隐藏的布尔值
Application.ScreenUpdating=False

使用ThisWorkbook.Sheets(1)“有一种方法可以包含多个条件:

Sub HideAccounts()

Dim TotExp As Range
Dim Cell As Range

Set TotExp = Range("O9:O1000")

Application.ScreenUpdating = False

For Each Cell In TotExp.Rows
If Cell.Value = 0 Then
Cell.EntireRow.Hidden = True
Else
Cell.EntireRow.Hidden = False

End If

Application.ScreenUpdating = True

Next Cell

End Sub
Option Explicit

Public Sub HideAccounts()
    Dim i As Long, hide As Boolean

    Application.ScreenUpdating = False
    With ThisWorkbook.Sheets(1)         '<-- set this to the relevant sheet
        For i = 9 To 1000
            hide = (.Range("O" & i).Value2 = 0)
            hide = hide And (.Range("AB" & i).Value2 = 0)
            hide = hide And (.Range("AN" & i).Value2 = 0)
            hide = hide And (Len(.Range("AJ" & i).Value2) = 0)
            hide = hide And (Len(.Range("AK" & i).Value2) = 0)
            .Rows(i).EntireRow.Hidden = hide
        Next
    End With
    Application.ScreenUpdating = True
End Sub
选项显式
公共子HideAccounts()
模糊的i长,隐藏的布尔值
Application.ScreenUpdating=False

使用此工作簿。工作表(1)“回顾您的答案。这不是OP想要的。回顾你的答案。这不是OP想要的。