excel - Select some adjacent and non-adjacent column ranges and save to pdf as if they were all adjacent - Stack Overflow

admin2025-04-24  4

I have a problem I need some help with. I have an excel sheet I need to export a report from to pdf. The first 2 adjacent columns should be in each report. I need to print 4 versions of the report containing 85 rows from Col A & B side by side together with one non-adjacent column to these (lets say Col F in one report version). I want col F to align side by side with cols A & B in the output pdf.

I've been playing about with range selection as in the following code. Sadly this gives me cols A & B side by side followed beneath by col F. If anyone has any pointers, I'd be grateful.

    Sub Export()
      Dim Rng As Range
      With ActiveSheet
        Set Rng = .Range("A1:B85, F1:F85")
        Rng.ExportAsFixedFormat Type:=xlTypePDF, fileName:="test.pdf", Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
      End With
    End Sub

I have a problem I need some help with. I have an excel sheet I need to export a report from to pdf. The first 2 adjacent columns should be in each report. I need to print 4 versions of the report containing 85 rows from Col A & B side by side together with one non-adjacent column to these (lets say Col F in one report version). I want col F to align side by side with cols A & B in the output pdf.

I've been playing about with range selection as in the following code. Sadly this gives me cols A & B side by side followed beneath by col F. If anyone has any pointers, I'd be grateful.

    Sub Export()
      Dim Rng As Range
      With ActiveSheet
        Set Rng = .Range("A1:B85, F1:F85")
        Rng.ExportAsFixedFormat Type:=xlTypePDF, fileName:="test.pdf", Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
      End With
    End Sub
Share Improve this question edited Jan 16 at 23:52 Ken White 126k15 gold badges236 silver badges466 bronze badges asked Jan 16 at 22:02 Joe McCauleyJoe McCauley 451 silver badge5 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Please try to hide columns C:E before exporting.

Sub Export()
    Dim Rng As Range
    With ActiveSheet
        Set Rng = .Range("A1:F85")
        With .Columns("C:E")
            .Hidden = True
            Rng.ExportAsFixedFormat Type:=xlTypePDF, Filename:="test.pdf", _
                Quality:=xlQualityStandard, IncludeDocProperties:=True, _
                IgnorePrintAreas:=False, OpenAfterPublish:=True
            .Hidden = False
        End With
    End With
End Sub

转载请注明原文地址:http://anycun.com/QandA/1745501651a90787.html