根据一系列单元格的隐藏/取消隐藏状态隐藏/取消隐藏行

时间:2016-01-13 18:34:25

标签: excel vba

如果隐藏了整个ENTIRE行,我想取消隐藏单行。如果即使是范围内的SINGLE行被取消隐藏,我也想隐藏这一行。这是什么语法?我目前的代码如下:

Public Sub MySub()

Application.ScreenUpdating = False

With Range("A1:A5")
.EntireRow.Hidden = False

 For Each cell In Range("A1:A5")
     Select Case cell.Value
         Case Is = "-"
         cell.EntireRow.Hidden = True
     End Select
 Next cell

End With

Application.ScreenUpdating = True

End Sub

2 个答案:

答案 0 :(得分:1)

我想我明白了。怎么回事:

Sub test()
Dim cel As Range, rng As Range
Dim hideRow&, numDashes&

Set rng = Range("A1:A5")
hideRow = rng.Count + 1

For Each cel In rng
    If cel.Value = "-" Then
        numDashes = numDashes + 1
        Rows(cel.Row).EntireRow.Hidden = True
    End If
Next cel
If numDashes = rng.Count Then
    ' If all cells in the range are '-'
    Rows(hideRow).EntireRow.Hidden = False
Else
    Rows(hideRow).EntireRow.Hidden = True
End If
End Sub

我有点假设你想要隐藏/取消隐藏第6行,因为它是你的范围最后一行之下的一个。因此,我创建了一个变量来保存它。这样,如果你想改变你的范围说A1:A100,你所要做的就是调整rng,它会隐藏/取消隐藏第101行。当然,如果你只是需要它6,然后只需hideRow = 6

编辑:为了好玩,我尝试减少计数变量numDashes的使用,并尝试检查所有-的范围的部分更简洁。以下内容也应该有效,但可能需要一两个调整:

Sub test2()
Dim cel As Range, rng As Range
Dim hideRow&

Set rng = Range("A1:A5")
hideRow = rng.Count + 1
'Check to see if your range is entirely made up of `-`
If WorksheetFunction.CountIf(rng, "-") = rng.Count Then
    Rows(hideRow).EntireRow.Hidden = False
    ' If you want to stop your macro if ALL range values are "-", then uncomment the next line:
    'Exit Sub
Else
    Rows(hideRow).EntireRow.Hidden = True
End If

For Each cel In rng
    If cel.Value = "-" Then
        Rows(cel.Row).EntireRow.Hidden = True
    End If
Next cel

End Sub

答案 1 :(得分:0)

您可以使用辅助列中的公式执行此操作。我使用这个用于财务报表来抑制多列都为零的行,以缩短报告。

 =IF(AND(SUM(A7:R7)<1,SUM(A7:R7)>-1),IF(OR(ISNUMBER(LEFT(H7,4)),ISBLANK(H7),ISERR(VALUE(LEFT(H7,4)))),"Show","Hide"),"Show"). 

然后按该列过滤行。