PDA

View Full Version : Export to PDF (specific sheets) if cells in Input sheet are >0, if condition is met



15mahe
08-24-2020, 01:40 PM
Dear, if anyone knows please help me to solve this problem, didn't find anywhere anything similar, and with VBA well im not quite good.

Here is description of problem:
Sheets (Report 1, 2, 3, and or 4) needs to be exported as one PDF if example condition on Input Sheet is >0
PDF document need to export on desktop
Document name needs to be as C18
By this example reports would be exported Report1, Report 3, Report 4.
Example condidtion is changing, so sometimes there will be exported just one report, sometimes all four of them
*If possible, to open email outlook, with saved PDF, with adress in sheet Input on cell C19, ready to be sent

Attachment excel is below.

Many thanks if anyone helps!
27002

15mahe
08-26-2020, 12:54 PM
Guys people in mrexcel.com already posted solution, i paste it here for purpose of education and solve problem for others, just move table on B9, and code work.

Public Sub Save_Sheets_As_PDF()

Dim DesktopFolder As String
Dim PDFfile As String
Dim cell As Range
Dim replaceSelected As Boolean
Dim currentSheet As Worksheet

DesktopFolder = CreateObject("WScript.Shell").SpecialFolders("Desktop") & ""

Set currentSheet = ActiveSheet
replaceSelected = True
With ThisWorkbook
PDFfile = DesktopFolder & .Worksheets("Input").Range("C17").Value
For Each cell In .Worksheets("Input").Range("B10:B13")
If cell.Offset(, 1).Value > 0 Then
.Worksheets(cell.Value).Select Replace:=replaceSelected
replaceSelected = False
End If
Next
End With

ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=PDFfile, OpenAfterPublish:=False, IgnorePrintAreas:=False

currentSheet.Select

End Sub


II Second solution:


Sub ExportPdf()
Dim hojas() As String, dFolder As String
Dim i As Long, n As Long

dFolder = CreateObject("WScript.Shell").SpecialFolders("Desktop") & Application.PathSeparator

With Sheets("Input")
For i = 10 To 13
If .Range("C" & i) > 0 Then
ReDim Preserve hojas(n)
hojas(n) = Sheets(.Range("B" & i).Value).Name
n = n + 1
End If
Next

If n > 0 Then
Sheets(hojas).Select
ActiveSheet.ExportAsFixedFormat xlTypePDF, dFolder & .[C17] & ".pdf", 0, True, False, , , False
.Select
End If
End With
End Sub

mancubus
08-28-2020, 05:19 AM
welcome to the forum.

http://www.vbaexpress.com/forum/faq.php?faq=new_faq_item#faq_new_faq_item3
when you post your question to multiple forums pleas add a link to your thread in other forums. and pls do this in all your threads in all forums.

btw, thanks for the info on mrexcel