简体   繁体   English

Excel VBA函数隐藏/取消隐藏行

[英]Excel VBA Function to hide/unhide Rows

I am new to VBA/Macro.我是 VBA/宏的新手。 I need some help figuring how to hide/unhide rows given my scenario.根据我的情况,我需要一些帮助来确定如何隐藏/取消隐藏行。 This is my table:这是我的桌子:

See Table: enter image description here见表:在此处输入图像描述

What I would like to do is:我想做的是:

1) Create a function (or sub) with three arguments: 1) 创建一个具有三个参数的函数(或子函数):

  • FlagRange: flagRange or C1:C9标志范围:标志范围或 C1:C9
  • hideAction: True/False隐藏动作:真/假
  • Flag: This is one of the flag value标志:这是标志值之一

Example: Hide_Unhide(flagRange, True, "P")示例:Hide_Unhide(flagRange, True, "P")

2) This function will hide all the row(s) based on the Flag that is passed into the argument. 2) 此函数将根据传递到参数中的标志隐藏所有行。

Any suggestions and samples are appreciated.任何建议和样品表示赞赏。

Here are two options.这里有两个选项。 The second sub is going to be faster as it will hide all the rows at once rather many possible iterations of hiding rows.第二个 sub 会更快,因为它将一次隐藏所有行,而不是隐藏行的许多可能迭代。 The larger the # of rows to be hidden, the more you will notice the time saved by using the Union approach要隐藏的行数越大,您会注意到使用Union方法节省的时间越多

Option Explicit

Sub HideMe(FlagRange As Range, Flag As String)

FlagRange.EntireRow.Hidden = False
Dim FlagCell As Range

Application.ScreenUpdating = False
    For Each FlagCell In FlagRange
        FlagCell.EntireRow.Hidden = FlagCell = Flag 'Rows get hidden here
    Next FlagCell
Application.ScreenUpdating = True

End Sub

Option Explicit

Sub HideMeUnion(FlagRange As Range, Flag As String)

FlagRange.EntireRow.Hidden = False
Dim FlagCell As Range, HideMe As Range

For Each FlagCell In FlagRange
    If FlagCell = Flag Then
        If HideMe Is Nothing Then
            Set HideMe = FlagCell
        Else
            Set HideMe = Union(HideMe, FlagCell)
        End If
    End If
Next FlagCell

'rows gettin hidden here all at once
If Not HideMe Is Nothing Then HideMe.EntireRow.Hidden = True

End Sub

Try this:试试这个:

Private sub Hide_Unhide(FlagRange as Range, Hide as Boolean, FlagValue as String)
Dim c as Variant
    For Each c in FlagRange
           c.EntireRow.Hidden = False
    Next c
    For Each c in FlagRange
        If c.Value2 = FlagValue Then
           c.EntireRow.Hidden = Hide
        End IF
    Next c
End Sub

You can use the following solution to to hide/unhide rows:您可以使用以下解决方案来隐藏/取消隐藏行:

Function Hide_Unhide(flagRange As Range, hideAction As Boolean, Flag As String)
        For Each Row In flagRange.Rows
            If Row.Cells(3) = Flag Then
                Row.EntireRow.Hidden = hideAction
            End If
        Next Row
End Function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM