Excel VBA - 隐藏打印作业的行 - 适用于某些但不是所有变量

时间:2014-12-16 01:56:15

标签: excel vba excel-vba rows hidden

这是我第一次在这个网站上发帖,而且我对VBA还不熟悉。我试图在网上找到答案,但找不到合适的解决方案。

我试图隐藏某些行和列只是为了打印,然后我希望vba代码取消隐藏相同的行/列。列总是一样的,所以认为这很容易,但我正在努力排队。我想出了如何在A列中隐藏带有空白单元格的行,它会解决我想隐藏的行的一半而不是所有行。我还想隐藏A列中的单元格等于的任何行:

  • “BEER”
  • “葡萄酒”
  • “酒”
  • “N / A BEV”
  • “插入此行以下的新产品”
  • “插入此行以上的新产品”
  • “TOTAL C.O.G.(AVERAGE)”

我为BEER,WINE,LIQUOR和N / A BEV工作的代码,但它不适用于较长的句子。请注意,包含这些值的行将随着时间的推移而变化,因此我不能像我为列一样列出要隐藏的行。

有人能指出我正确的方向让这些行也隐藏起来吗?这是代码:

Sub WorkbookBeforePrint_Called()

  Dim rw As Long
  Dim rng As Range
  Dim cell As Range

    Application.ScreenUpdating = False

    Set rng = Range("A1", Range("A" & Rows.Count).End(xlUp))

    With rng.Columns(1)
         For Each cell In rng
             If .Cells(cell.Row, 1).Value = "INSERT NEW PRODUCTS BELOW THIS ROW" Then _
             Parent.Rows(cell.Row).Hidden = True
             If .Cells(cell.Row, 1).Value = "INSERT NEW PRODUCTS ABOVE THIS ROW" Then _
             .Parent.Rows(cell.Row).Hidden = True
             If .Cells(cell.Row, 1).Value = "TOTAL C.O.G.   (AVERAGE)" Then _
             .Parent.Rows(cell.Row).Hidden = True
             If .Cells(cell.Row, 1).Value = "BEER" Then _
             .Parent.Rows(cell.Row).Hidden = True
             If .Cells(cell.Row, 1).Value = "WINE" Then _
             .Parent.Rows(cell.Row).Hidden = True
             If .Cells(cell.Row, 1).Value = "LIQUOR" Then _
             .Parent.Rows(cell.Row).Hidden = True
             If .Cells(cell.Row, 1).Value = "N/A BEV" Then _
             .Parent.Rows(cell.Row).Hidden = True
         Next cell

With ActiveSheet

    ActiveSheet.Outline.ShowLevels ColumnLevels:=3
    ActiveSheet.Outline.ShowLevels RowLevels:=3
          .Range("C1,D1,E1,F1,G1,H1,I1,J1,K1,L1,M1,N1,O1,P1,Q1,R1,V1,W1,X1,Y1,Z1,AA1,AE1,AF1,AG1,AH1,AI1,AJ1,AN1,AO1,AP1,AQ1,AR1,AS1,AW1,AX1,AY1").EntireColumn.Hidden = True

  On Error Resume Next
      .Columns("A").SpecialCells(xlCellTypeBlanks).EntireRow.Hidden = True

      .PrintOut

      .Columns("A").SpecialCells(xlCellTypeBlanks).EntireRow.Hidden = False
  On Error GoTo 0
          .Range("C1,D1,E1,F1,G1,H1,I1,J1,K1,L1,M1,N1,O1,P1,Q1,R1,V1,W1,X1,Y1,Z1,AA1,AE1,AF1,AG1,AH1,AI1,AJ1,AN1,AO1,AP1,AQ1,AR1,AS1,AW1,AX1,AY1").EntireColumn.Hidden = False

    ActiveSheet.Outline.ShowLevels RowLevels:=3
    ActiveSheet.Outline.ShowLevels ColumnLevels:=3

End With

    .EntireRow.Hidden = False

End With

    Application.ScreenUpdating = True

End Sub

3 个答案:

答案 0 :(得分:0)

如果我遇到这样的麻烦,我会尝试使用文本函数消除转置错误。尝试添加LEFT函数,如下例所示。

If Left(.Cells(cell.Row, 1).Value, 25) = "INSERT NEW PRODUCTS BELOW" Then _
            Rows(cell.Row).Hidden = True

如果您愿意,还可以使用相同的IF语句捕获这两个长字符串。

If Left(.Cells(cell.Row, 1).Value, 19) = "INSERT NEW PRODUCTS" Then _
            Rows(cell.Row).Hidden = True

它为我提供了一个示例数据集,但请告诉我这是否可以帮助您处理实际数据。

答案 1 :(得分:0)

这是一种略有不同的方法。它只是隐藏部分。

使用Select Case代替IF

Sub WorkbookBeforePrint2()

Dim lastRow As Long
Dim sheet As String
Dim tempVal As String

    sheet = "Sheet1"    'Insert your sheet name here

    lastRow = Sheets(sheet).Range("A" & Rows.Count).End(xlUp).row

    For lRow = 2 To lastRow
        tempVal = Sheets(sheet).Cells(lRow, "A").Text
        Select Case tempVal
            Case Is = ""
                Rows(lRow).Hidden = True
            Case Is = "INSERT NEW PRODUCTS BELOW THIS ROW"
                Rows(lRow).Hidden = True
            Case Is = "INSERT NEW PRODUCTS ABOVE THIS ROW"
                Rows(lRow).Hidden = True
            Case Is = "TOTAL C.O.G.   (AVERAGE)"
                Rows(lRow).Hidden = True
            Case Is = "BEER"
                Rows(lRow).Hidden = True
            Case Is = "WINE"
                Rows(lRow).Hidden = True
            Case Is = "LIQUOR"
                Rows(lRow).Hidden = True
            Case Is = "N/A BEV"
                Rows(lRow).Hidden = True
        End Select
    Next lRow
End Sub

BEFORE AFTER

答案 2 :(得分:0)

感谢所有人的帮助!我有更长的文本变量包含在groups / oulines中但不包含更短的文本变量。我只需要重新安排代码:

Sub WorkbookBeforePrint(control As IRibbonControl)

  Dim lastRow As Long
  Dim tempVal As String

  Application.ScreenUpdating = False

    With ActiveSheet

      ActiveSheet.Outline.ShowLevels ColumnLevels:=3
      ActiveSheet.Outline.ShowLevels RowLevels:=3

        .Range("C1,D1,E1,F1,G1,H1,I1,J1,K1,L1,M1,N1,O1,P1,Q1,R1,V1,W1,X1,Y1,Z1,AA1,AE1,AF1,AG1,AH1,AI1,AJ1,AN1,AO1,AP1,AQ1,AR1,AS1,AW1,AX1,AY1").EntireColumn.Hidden = True

        lastRow = .Range("A" & Rows.Count).End(xlUp).Row

          For lRow = 2 To lastRow
            tempVal = .Cells(lRow, "A").Text
              Select Case tempVal
                Case Is = ""
                  Rows(lRow).Hidden = True
                Case Is = "INSERT NEW PRODUCTS BELOW THIS ROW"
                  Rows(lRow).Hidden = True
                Case Is = "INSERT NEW PRODUCTS ABOVE THIS ROW"
                  Rows(lRow).Hidden = True
                Case Is = "TOTAL C.O.G.   (AVERAGE)"
                  Rows(lRow).Hidden = True
                Case Is = "BEER"
                  Rows(lRow).Hidden = True
                Case Is = "WINE"
                  Rows(lRow).Hidden = True
                Case Is = "LIQUOR"
                  Rows(lRow).Hidden = True
                Case Is = "N/A BEV"
                  Rows(lRow).Hidden = True
              End Select
          Next lRow

      .PrintOut

        Rows(lRow).Hidden = False

        .Range("C1,D1,E1,F1,G1,H1,I1,J1,K1,L1,M1,N1,O1,P1,Q1,R1,V1,W1,X1,Y1,Z1,AA1,AE1,AF1,AG1,AH1,AI1,AJ1,AN1,AO1,AP1,AQ1,AR1,AS1,AW1,AX1,AY1").EntireColumn.Hidden = False

      ActiveSheet.Outline.ShowLevels RowLevels:=3
      ActiveSheet.Outline.ShowLevels ColumnLevels:=3

    End With

  Application.ScreenUpdating = True

End Sub