如果没有文本导出到PDF,则隐藏行

时间:2018-11-07 14:38:46

标签: excel vba

我有一个用于将范围导出到Excel的简单脚本:

`Private Sub CommandButton2_Click()

 With Sheets("Summary").Range("B2:H83")
    .ExportAsFixedFormat _
        Type:=xlTypePDF, _
        Filename:="F:\Export.pdf", _
        Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, _
        OpenAfterPublish:=True
End With

End Sub

` 问题是某些行可能基于也可能不基于单独的工作表中的输入进行填充。因此,例如可以填充行34-42,但是不仅填充行34,而且更有可能。这样会在导出的PDF中留出很多空白。

我不知道如何构造代码来检查行中是否有信息,如果不行,则隐藏行从而消除空白

3 个答案:

答案 0 :(得分:2)

我已调整您的脚本以隐藏空白行。它只会检查您上面提到的范围,以防您告知我动态范围。

Private Sub CommandButton2_Click()

Dim rowCounter As Integer
Dim columnCounter As Integer
Dim blankFlag As Boolean

With sheets("Summary")
    'hide blank rows in 34-42 and 44-56 interval
    For rowCounter = 34 To 56
        blankFlag = True

        For columnCounter = 2 To 8
            If .Cells(rowCounter, columnCounter) <> "" Then
                blankFlag = False
                Exit For
            End If
        Next columnCounter

        If blankFlag = True Then rows(rowCounter).EntireRow.Hidden = True
        If rowCounter = 42 then rowCounter = rowCounter + 1
    Next rowCounter

End With

With sheets("Summary").Range("B2:H83")
    'export to PDF
    .ExportAsFixedFormat _
        Type:=xlTypePDF, _
        filename:="F:\Export.pdf", _
        Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, _
        OpenAfterPublish:=True
End With

End Sub

答案 1 :(得分:1)

最好一次隐藏所有空行,而不是在循环的每次迭代中分别隐藏它们。

Private Sub CommandButton2_Click()

    Dim mainRng As Range
    Set mainRng = sheets("Summary").Range("B2:H83")

    Dim unionRng As Range

    Dim roww As Range
    For Each roww In mainRng.rows
        If WorksheetFunction.CountA(roww) = 0 Then
            If Not unionRng Is Nothing Then
                Set unionRng = Union(unionRng, roww)
            Else
                Set unionRng = roww
            End If
        End If
    Next

    If Not unionRng Is Nothing Then unionRng.EntireRow.Hidden = True

    mainRng.ExportAsFixedFormat _
        Type:=xlTypePDF, _
        filename:="F:\Export.pdf", _
        Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, _
        OpenAfterPublish:=True

    If Not unionRng Is Nothing Then unionRng.EntireRow.Hidden = False

End Sub

答案 2 :(得分:0)

我认为您可以尝试以下代码:

    Private Sub CommandButton2_Click()

        Dim ShtSummary As Worksheet
        Dim i As Integer

        Set ShtSummary = Sheets("Summary")

        For i = 3 To 83
            If Application.WorksheetFunction.CountA(Rows(i)) = 0 Then
                ShtSummary.Rows(i).EntireRow.Hidden = True
            End If

        Next i

     With Sheets("Summary").Range("B2:H83")
        .ExportAsFixedFormat _
            Type:=xlTypePDF, _
            Filename:="F:\Export.pdf", _
            Quality:=xlQualityStandard, _
            IncludeDocProperties:=True, _
            IgnorePrintAreas:=False, _
            OpenAfterPublish:=True
    End With

End Sub

祝你好运。