Excel vba隐藏行直到值

我想在Excel中将行7隐藏到X,其中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 

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

你的叙述说'上面的6行' ,你的代码说'3行之前' 。 我通常相信代码。 这是非常重要的,因为涉及的math,并确保你不想隐藏行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 

尽可能避免使用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 

希望这不是太复杂。 🙂

说我们开始:

在这里输入图像说明

我们想隐藏“幸福”第一次出现的第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 

会产生:

在这里输入图像说明