繁体   English   中英

Excel VBA隐藏行直到值

[英]Excel vba hide rows until value

我想在Excel中隐藏X到X的第7行,其中X是A列中值的第一个实例上方的第6行。

Google似乎提出了一些看起来对我而言太复杂的东西。

到目前为止,我的基本知识是:

'Select Rows 7 through 3 rows before the first instance of any text in column A, then hides them
Rows("7:62").Select
' have to update the 62 above to look for text and hide based on that
Selection.Rows.Group
Selection.EntireRow.Hidden = True

在我的Sub中。 我只需要弄清楚如何将62更改为通常在60左右的可变单元格,但并不总是62的确切值。

尽量避免使用“ Select

strValueToFind = "What ever the value you are trying to find in column A is"
With Sheets("Sheet1")
    'Find the row containing the value
    intFoundRow = .Range("A:A").Find(What:= strValueToFind, _
        LookIn:=xlValues, _
        LookAt:=xlWhole).Row
    'Now hide Rows 7 through 6 above the row we found
    .Rows("7:" & intFoundRow - 6).Hidden = True
End With

希望这并不太复杂。 :)

您的叙述说“上面有6行”,而代码说“之前3行” 我通常相信代码。 由于涉及数学运算,因此这很重要,并确保您不尝试隐藏第7行上方的行。

Dim rw As Variant
rw = Application.Match(Chr(42), Range("A8:A1048576"), 0)
If Not IsError(rw) Then
    rw = Application.Max(rw + 4, 7)
    Range("A7:A" & rw).EntireRow.Hidden = True
End If

说我们开始于:

在此处输入图片说明

并且我们希望将第一个出现的“幸福”上方的所有行都隐藏7至6行。 运行此:

Sub FindAndHide()
    Dim rng As Range

    Set rng = Range("A:A")
    Range(Cells(7, 1), rng.Find(What:="happiness", after:=rng(7)).Offset(-6, 0)).EntireRow.Hidden = True
End Sub

将产生:

在此处输入图片说明

暂无
暂无

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

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