Hello Excel Gurus!
I have a project that is giving me a fit in that I am using the same code for two different tabs to save only the cells that have data in them as a PDF and one works fine, while the other does not. The problem child breaks up the saved cells into two pages, where the other one doesn't.
I've included a screenshot of the cells that are being saved and the PDF that I had to save as two separate image files
. Here is my VBA code, which was my fourth different iteration of it, each time trying a different way, but ending with the same results:
Sub SaveMPFChronLogToPDF()
Dim ws As Worksheet
Dim rng As Range
Dim fileName As String
' Set the worksheet
Set ws = ThisWorkbook.Sheets("Chron Log")
' Set the reange to save (columns A and B that contain data)
Set rng = ws.Range("A:B").SpecialCells(xlCellTypeConstants)
' Check if there is data in the range
If WorksheetFunction.CountA(rng) = 0 Then
MsgBox "No data to save!", vbExclamation
Exit Sub
End If
' Create the filename with date format "ddmmmyyyy"
fileName = "C:\Trackers\Logs\Chron Log\MPF Chron Logs\ " & "MPF Chron Log " & Format(Date, "ddmmmyyyy") & ".pdf"
' Set the print area to fit the data
With ws.PageSetup
.PaperSize = xlPaperLetter
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = 1
.LeftMargin = Application.InchesToPoints(0.5)
.RightMargin = Application.InchesToPoints(0.5)
.TopMargin = Application.InchesToPoints(0.5)
.BottomMargin = Application.InchesToPoints(0.5)
End With
' Save the range as PDF
rng.ExportAsFixedFormat Type:=xlTypePDF, fileName:=fileName, Quality:=xlQualityStandard
' Clear the print area
ws.PageSetup.PrintArea = ""
MsgBox "PDF saved successfully!", vbInformation
End Sub
Any assistance/advice is appreciated.
Thank you!
I have a project that is giving me a fit in that I am using the same code for two different tabs to save only the cells that have data in them as a PDF and one works fine, while the other does not. The problem child breaks up the saved cells into two pages, where the other one doesn't.
I've included a screenshot of the cells that are being saved and the PDF that I had to save as two separate image files
Sub SaveMPFChronLogToPDF()
Dim ws As Worksheet
Dim rng As Range
Dim fileName As String
' Set the worksheet
Set ws = ThisWorkbook.Sheets("Chron Log")
' Set the reange to save (columns A and B that contain data)
Set rng = ws.Range("A:B").SpecialCells(xlCellTypeConstants)
' Check if there is data in the range
If WorksheetFunction.CountA(rng) = 0 Then
MsgBox "No data to save!", vbExclamation
Exit Sub
End If
' Create the filename with date format "ddmmmyyyy"
fileName = "C:\Trackers\Logs\Chron Log\MPF Chron Logs\ " & "MPF Chron Log " & Format(Date, "ddmmmyyyy") & ".pdf"
' Set the print area to fit the data
With ws.PageSetup
.PaperSize = xlPaperLetter
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = 1
.LeftMargin = Application.InchesToPoints(0.5)
.RightMargin = Application.InchesToPoints(0.5)
.TopMargin = Application.InchesToPoints(0.5)
.BottomMargin = Application.InchesToPoints(0.5)
End With
' Save the range as PDF
rng.ExportAsFixedFormat Type:=xlTypePDF, fileName:=fileName, Quality:=xlQualityStandard
' Clear the print area
ws.PageSetup.PrintArea = ""
MsgBox "PDF saved successfully!", vbInformation
End Sub
Any assistance/advice is appreciated.
Thank you!