VBA Loop to PDF

smartpat19

Board Regular
Joined
Sep 3, 2014
Messages
114
Hi,

I have a list of value in tab "List" A2:A##

I want to update the tab "summary" "A1" with each value from the list which updates the summary Tab with data.

I then want to copy this print area from the tab "summary" to a new workbook until every value in the Tab "List" is used.

I then want to pdf the entire new workbook into one PDF.

Thank you so much for your help! I spend hours doing this task.

Best,
Patrick
 
Last edited:

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Try this

The names of the xlsx and pdf files will have as their name the value of the cell in the list sheet.
The files will be saved in the same folder where the book is located with the macro.

Code:
Sub Loop_to_PDF()
  Dim sh1 As Worksheet, sh2 As Worksheet, c As Range, wb As Workbook, wPath As String
  Application.ScreenUpdating = False
  Application.DisplayAlerts = False
  wPath = ThisWorkbook.Path & "\"
  Set sh1 = Sheets("List")
  Set sh2 = Sheets("summary")
  For Each c In sh1.Range("A2", sh1.Range("A" & Rows.Count).End(xlUp))
    sh2.Range("A1").Value = c.Value
    sh2.Copy
    Set wb = ActiveWorkbook
    wb.SaveAs Filename:=wPath & c.Value & ".xlsx"
    wb.ExportAsFixedFormat Type:=xlTypePDF, Filename:=wPath & c.Value & ".pdf", OpenAfterPublish:=False
    wb.Close False
  Next
  MsgBox "End"
End Sub
 
Upvote 0
Dante,

Thank you for your help on this, it works great!

Can you make the macro PDF all the sheets together?

So when copying the summary tab, add all the summary tabs into one workbook and then PDF the entire workbook?

Thank you,
patrick
 
Upvote 0
Try this

Code:
Sub Loop_to_PDF()
  Dim sh1 As Worksheet, sh2 As Worksheet, c As Range, wb As Workbook, wPath As String
  Application.ScreenUpdating = False
  Application.DisplayAlerts = False
  wPath = ThisWorkbook.Path & "\"
  Set sh1 = Sheets("List")
  Set sh2 = Sheets("summary")
  For Each c In sh1.Range("A2", sh1.Range("A" & Rows.Count).End(xlUp))
    sh2.Range("A1").Value = c.Value
[COLOR=#0000cd]    Sheets.Copy[/COLOR]
    Set wb = ActiveWorkbook
    wb.SaveAs Filename:=wPath & c.Value & ".xlsx"
    wb.ExportAsFixedFormat Type:=xlTypePDF, Filename:=wPath & c.Value & ".pdf", OpenAfterPublish:=False
    wb.Close False
  Next
  MsgBox "End"
End Sub
 
Upvote 0
Almost there.

heres an example.

I have 5 values in the list.

I want to update the summary tab with each value so at the end i will have 5 summary tabs in one new file and then PDF that new workbook.

Not sure if that helps.

Thank you for all your help.
 
Upvote 0
"La tercera es la vencida"

It is an expression, it almost always works on the third attempt.

Code:
Sub Loop_to_PDF()
  Dim sh1 As Worksheet, sh2 As Worksheet, c As Range, wb As Workbook, wPath As String
  Application.ScreenUpdating = False
  Application.DisplayAlerts = False
  wPath = ThisWorkbook.Path & "\"
  Set sh1 = Sheets("List")
  Set sh2 = Sheets("summary")
  Set wb = Workbooks.Add
  For Each c In sh1.Range("A2", sh1.Range("A" & Rows.Count).End(xlUp))
    sh2.Range("A1").Value = c.Value
    sh2.Copy after:=wb.Sheets(wb.Sheets.Count)
  Next
  wb.Sheets(1).Delete
  wb.ExportAsFixedFormat Type:=xlTypePDF, Filename:=wPath & "new workbook.pdf", OpenAfterPublish:=False
  wb.Close False
  MsgBox "End"
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,196
Members
452,616
Latest member
intern444

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top