简体   繁体   English

隐藏名称范围内的5行中的3行?

[英]Hide 3 of the 5 rows in a name range?

I have a name range that has 5 rows and 6 columns. 我的名称范围包含5行和6列。 I want to hide 3 rows out of the 5 rows. 我想在5行中隐藏3行。 I'm only able to get one of the rows. 我只能得到其中之一。 How would I specify the other rows i need to be hidden? 如何指定需要隐藏的其他行?

Private Sub Worksheet_Change(ByVal Target As Range)
Dim rowCounter As Long
Dim theTargetRow, initialStartRow As String
initialStartRow = "113"
theTargetRow = initialStartRow + Target.Value
If Target.Value = "---" Then
    Range("myNameRange").Rows.Hidden = True
Else
    'Range("myNameRange").Rows.Hidden = True
    For rowCounter = theTargetRow To initialStartRow + 1 Step -1
        Rows(rowCounter).EntireRow.Hidden = False
    Next           
End If
End Sub

EDIT: I got it work by removing my namerange from the rows i needed to be hidden. 编辑:我通过从需要隐藏的行中删除我的名称范围来使其工作。

With you providing limited information, I can only give limited information back to you (more specifically, the below code is searching every single cell in your range because I am not sure what column N/A is located in). 当您提供有限的信息时,我只能将有限的信息反馈给您(更具体地说,以下代码正在搜索您范围内的每个单元格,因为我不确定N/A列位于哪一列)。

Option Explicit

Sub Test()

    Dim Target As Range, Cell As Range
    Set Target = Range("myNameRange")

    For Each Cell In Target
        If Cell.Value = "N/A" Then
            Cell.EntireRow.Hidden = True
        Else
            Cell.EntireRow.Hidden = False
        End If
    Next Cell

End Sub

要隐藏命名范围的第i行...

  Sheets("Sheet1").Range("myNameRange").Rows(i).EntireRow.Hidden = True 

This will hide the first three rows in a named range, although by the solution you found that worked for you it seems you want to hide just rows - not necessarily within a named range. 这将隐藏命名范围内的前三行,尽管您发现对自己有用的解决方案似乎只希望隐藏行-不一定要隐藏在命名范围内。

Public Sub Test()

    Dim rCell As Range
    Dim i As Long

    For Each rCell In Range("myNameRange").Rows
        If i < 3 Then
            rCell.EntireRow.Hidden = True
        Else
            Exit For
        End If
        i = i + 1
    Next rCell

End Sub

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

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