I have a simple script for exporting a range to 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
`
The issue is that certain rows may or may not be populated based on inputs in separate sheets. So for example Rows 34-42 could be populated, but more likely than not only Row 34 is populated. This leaves a lot of white space in the exported PDF.
I have no idea how to structure my code to check if there is info in the Row, and if not then to hide the row thereby eliminating white space
I have adjusted your script to hide blank rows. It checks only the range you mention above, in case it's a dynamic range you can let me know.
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
Better to hide all of the empty rows at once instead of hiding them individually on each iteration of the loop.
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
I think you can try this code:
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
Good Luck.
Related
On the basis of a few posts I was able to make below script which prints several selected ranges to a pdf file. However, all ranges are printed on a seperate sheet.
Currently NewRng.Address="A1:G9,A13:G14,A18:G37". I think it might need to be "A1:G9;A13:G14;A18:G37" (seperated by ; instead of ,)(?)
Can someone explain how can I print the selected ranges on one sheet?
Thank you so much!
Script:
Sub CreatePDF_Selection1()
Dim rng1 As Range, rng2 As Range, rng3 As Range
Dim NewRng As Range
With ThisWorkbook.Sheets("Sheet1")
Set rng1 = .Range("A1:G9")
Set rng2 = .Range("A13:G14")
Set rng3 = .Range("A18:G37")
Set NewRng = .Range(rng1.Address & "," & rng2.Address & "," & rng3.Address)
Debug.Print NewRng.Address
Sheets("Sheet1").Activate
ActiveSheet.Range(NewRng.Address).Select
Sheets(Array("Sheet1")).Select
ThisWorkbook.Sheets(Array("Sheet1")).Select
Selection.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:="U:\Sample Excel File Saved As PDF", _
Quality:=xlQualityStandard, _
IncludeDocProperties:=False, _
IgnorePrintAreas:=True, _
From:=1, _
OpenAfterPublish:=True
End With
End Sub
Rather than select various ranges just hide the rows you don't want to print then print the entire range.
Option Explicit
Sub CreatePDF_Selection1()
Dim rng1 As Range
ThisWorkbook.Sheets("Sheet1").Activate
Set rng1 = Range("A1:G37")
Range("A10:A12").EntireRow.Hidden = True '*** Hide rows not to print ***
Range("A15:A17").EntireRow.Hidden = True
rng1.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:="U:\Sample Excel File Saved As PDF", _
Quality:=xlQualityStandard, _
IncludeDocProperties:=False, _
IgnorePrintAreas:=True, _
From:=1, _
OpenAfterPublish:=True
Rows("1:37").EntireRow.Hidden = False '*** Unhide hidden rows ***
End Sub 'CreatePDF_Selection1()
HTH
Edit: Attach test output.
I could not find a direct solution, so there is a work about here. A new worksheet will be added. The content will be copied there as a continuous range. The sheet will be exported as PDF, than the not needed sheet will be deleted.
Sub CreatePDF_Selection1()
Dim rng1 As Range, rng2 As Range, rng3 As Range
Dim NewRng As Range
Application.ScreenUpdating = False
With Sheet1
Set rng1 = .Range("A1:G9")
Set rng2 = .Range("A13:G14")
Set rng3 = .Range("A18:G37")
Set NewRng = Union(rng1, rng2, rng3)
End With
'Creating test values
rng1.Value = "Test 1"
rng2.Value = "Test 2"
rng3.Value = "Test 3"
NewRng.Copy
'adding a new sheet
Worksheets.Add after:=Sheet1
With ActiveSheet
.Paste
.ExportAsFixedFormat, _
Type:=xlTypePDF, _
Filename:="U:\Sample Excel File Saved As PDF", _
Quality:=xlQualityStandard, _
IncludeDocProperties:=False, _
IgnorePrintAreas:=True, _
From:=1, _
OpenAfterPublish:=True
Application.DisplayAlerts = False
.Delete 'delete the unwanted worksheet
Application.DisplayAlerts = True
End With
Application.ScreenUpdating = True
End Sub
Export Non-Contiguous Range to PDF
This solution uses the Application.Union method to create the range to be exported. The range is then copied using the Range.Copy method to a newly added worksheet and exported from there. Then the newly added worksheet is deleted.
Option Explicit
Sub CreatePDF_Selection1()
Const FilePath As String = "U:\Sample Excel File Saved As PDF"
Const SheetName As String = "Sheet1"
Dim wb As Workbook
Set wb = ThisWorkbook ' The workbook containing this code.
' Define Copy Range.
With wb.Worksheets(SheetName)
Dim rng As Range
Set rng = Union(.Range("A1:G9"), .Range("A13:G14"), .Range("A18:G37"))
End With
' Copy Copy Range to new worksheet, export to PDF and delete new worksheet.
With Worksheets.Add
' This will copy values and formats.
rng.Copy .Range("A1")
.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=FilePath, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=False, _
IgnorePrintAreas:=True, _
OpenAfterPublish:=True
Application.DisplayAlerts = False
.Delete
Application.DisplayAlerts = True
End With
End Sub
I would like to eligible my PDF document to save, although within the sheets with specified color.
How can I do that?
Sub SavecolorTabtoPDF()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
If Tab Color = "Blue, Accent 1, Darker 25%" Then
'export as pdf
ws.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:="C:\" & Left(ws.Name, Len(ws.Name) - 2) & ".pdf", _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=True
End If
Next ws
End Sub
You want to loop through the sheets and create an array of sheets based on tab color.
This will create your array of sheets, then pdf them as one.
Sub MakeArraySheets()
Dim sh As Worksheet
Dim ArraySheets() As String
Dim x As Variant
For Each sh In ActiveWorkbook.Worksheets
If sh.Tab.ColorIndex = 55 Then
ReDim Preserve ArraySheets(x)
ArraySheets(x) = sh.Name
x = x + 1
End If
Next sh
Sheets(ArraySheets).Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
ThisWorkbook.Path & "\" & ThisWorkbook.Name, _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=True
End Sub
dim wsheet as worksheet
For Each wsheet In Thisworkbook.Worksheets
if wsheet.tab.colorindex = 1 then
Worksheet.SaveAs(*parameters*)
end if
next wsheet
EDIT: The colorindex for your specified color is 55. Find out by filling a cell with that color and running "mgbox activecell.interior.colorindex"
EDIT2: Removed "interior" before colorindex
Currently have a message shown when items are exporting to PDF. Wanting to get the total amount of items from drop down to represent the x value of (Exporting y of x). Currently have a fix value.
Const SAVE_PATH = "C:\SC\"
Sub SCtoPDF()
Dim inputRange As Range
Dim cell As Range
Set inputRange = Evaluate(Range("G11").Validation.Formula1)
For Each cell In inputRange
If cell.Value <> "" Then
counter = counter + 1
Application.StatusBar = "Exporting Scorecard: " & counter & " of " & "32"
[G11] = cell.Value
ActiveSheet.Calculate
ActiveSheet.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=SAVE_PATH & cell.Value, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
End If
Next cell
End Sub
Assuming that no validation list rows are empty:
inputRange.Rows.Count
I recorded a macro.
It works if all of the sheets are visible but when I hide a sheet it will not save to pdf.
This is the code.
Sub save_pdf()
'
' save_pdf Macro
'
'
Sheets(Array("TITLE", "CML", "CLUSTER", "ORS", "MOBILE", "YPS", "DEVICES", "PORTS")).Select
Sheets("TITLE").Activate
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, fileName:= _ Sheets("MAIN").Range("customer_name") + " - Project Initiation_ Document.pdf", Quality:=xlQualityStandard, IncludeDocProperties:=True,_ IgnorePrintAreas:=False, OpenAfterPublish:=True
Sheets("MAIN").Select
End Sub
Something like this:
Sub ExportVisible()
Dim shts, sht As Worksheet, s, i As Long
shts = Array("TITLE", "CML", "CLUSTER", "ORS", "MOBILE", "YPS", "DEVICES", "PORTS")
i = 0
For Each s In shts
Set sht = ActiveWorkbook.Sheets(s)
If sht.Visible = xlSheetVisible Then
i = i + 1
sht.Select (i = 1) '"replace" parameter true when i=1
End If
Next s
'Sheets("TITLE").Activate '<<EDIT: remove this
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=Sheets("MAIN").Range("customer_name").Value & _
" - Project Initiation_Document.pdf ", _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=True
End Sub
It's likely due to the use of .Select. We want to avoid using .Select wherever possible. Instead, just set a loop to go through each worksheet in your workbook.
Sub save_PDFs()
Dim sht As Worksheet
For Each sht In ActiveWorkbook.Worksheets
sht.ExportAsFixedFormat Type:=xlTypePDF, fileName:=Sheets("MAIN").Range("customer_name") + " - Project Initiation_ Document.pdf", Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
Next sht
End Sub
However, if you want only visible sheets, then do this:
Sub save_PDFs()
Dim sht As Worksheet
For Each sht In ActiveWorkbook.Worksheets
If sht.Visible = True Then sht.ExportAsFixedFormat Type:=xlTypePDF, fileName:=Sheets("MAIN").Range("customer_name") + " - Project Initiation_ Document.pdf", Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
Next sht
End Sub
So this is my code that´s is supposed to loop through the pivot's SUPPLIER FILTER based on a list located in the same worksheet. After every loop it has to upload the file to the intranet in a .pdf format.
Sub Upload()
Dim pt As PivotTable
Dim pi As PivotItem
Dim pf As PivotField
Dim lLoop As Long
Set pt = Sheets("To Supplier").PivotTables("PivotTable1")
Set pf = pt.PivotFields("[Query].[SUPPLIER].[SUPPLIER]")
Sheets("To Supplier").Select
For Each pi In pf.PivotItems
On Error Resume Next
pf.CurrentPage = pi.Value
On Error GoTo 0
If pf.CurrentPage = pi.Value Then
If lLoop = 0 Then
With Sheets("To Supplier").PageSetup
.CenterFooter = pi.Value
.LeftHeader = pt.Name
.LeftFooter = Now
End With
End If
Range("L2").Select
ActiveCell.FormulaR1C1 = _
"=INDEX(RC[-6]:R[59]C[-5],MATCH(R[1]C[-10],RC[-6]:R[59]C[-6],0),2)"
Sheets("To Supplier").ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"http://collaborationx.com/portalone/sourcing/Supplier%20documents/" & Cell("L2").Value _
& "/Evaluations/" & Cell("L2").Value & "%20Credits.pdf" _
, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
:=False, OpenAfterPublish:=True
lLoop = lLoop + 1
End If
Next pi
End Sub
However, It keeps throwing me off... now the prob is that it says the function is not defined, before the problem was with the references...
Cell("L2") is not defined. You probably wanted Range("L2") but since you selected that range before you could use ActiveCell. Example, HTH.
Option Explicit
Sub test()
Range("L2").Select
ActiveCell.FormulaR1C1 = "=INDEX(RC[-6]:R[59]C[-5],MATCH(R[1]C[-10],RC[-6]:R[59]C[-6],0),2)"
Dim exportFileName As String
exportFileName = "http://collaborationx.com/portalone/sourcing/Supplier%20documents/{0}/Evaluations/{0}%20Credits.pdf"
exportFileName = Replace(exportFileName, "{0}", ActiveCell.Value)
Sheets("To Supplier").ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=exportFileName, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=True
End Sub