VBA macro for REPLACE cell value from a list THEN print in a single PDF Document!

INOpportuno

New Member
Joined
Jul 5, 2023
Messages
6
Office Version
  1. 2021
Platform
  1. Windows
I found in this forum how to printout sheets with different values, but I'd like to print all the sheets in a single PDF Document.
How can I fix this VBA code to do this?

Sub PrintAll()


Dim i As Long, LastRow As Long


LastRow = Worksheets("Names").Range("A65536").End(xlUp).Row


For i = 1 To LastRow
Worksheets("Form").Range("A1").Value = Worksheets("Names").Range("A" & i).Value
Worksheets("Form").Range("Print_Me").PrintOut
Next i


End Sub
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
You want to print all worksheets in a workbook to a single pdf?
 
Upvote 0
You asked for all sheets to be saved as a single PDF file. This will do that.
File will be saved in the same Folder where the excel workbook that has the macro in it is saved.
Make sure that this workbook was indeed saved before running the macro.
Name of the PDF file will be "INOpportuno At MrExcel. PDF"
Code:
Sub Maybe()
    Dim SheetNames() As String
    ReDim SheetNames(1 To 1)
    Dim ws As Worksheet
    For Each ws In Worksheets
        SheetNames(UBound(SheetNames)) = ws.Name
        ReDim Preserve SheetNames(1 To UBound(SheetNames) + 1)
    Next ws
    ReDim Preserve SheetNames(1 To UBound(SheetNames) - 1)
    Sheets(SheetNames).Select
    ActiveSheet.ExportAsFixedFormat xlTypePDF, ThisWorkbook.Path & "\" & "INOpportuno At MrExcel"  & ".pdf"
End Sub
Of course it would help if we knew if the print range needed to be set.
 
Upvote 0
You asked for all sheets to be saved as a single PDF file. This will do that.
File will be saved in the same Folder where the excel workbook that has the macro in it is saved.
Make sure that this workbook was indeed saved before running the macro.
Name of the PDF file will be "INOpportuno At MrExcel. PDF"
Code:
Sub Maybe()
    Dim SheetNames() As String
    ReDim SheetNames(1 To 1)
    Dim ws As Worksheet
    For Each ws In Worksheets
        SheetNames(UBound(SheetNames)) = ws.Name
        ReDim Preserve SheetNames(1 To UBound(SheetNames) + 1)
    Next ws
    ReDim Preserve SheetNames(1 To UBound(SheetNames) - 1)
    Sheets(SheetNames).Select
    ActiveSheet.ExportAsFixedFormat xlTypePDF, ThisWorkbook.Path & "\" & "INOpportuno At MrExcel"  & ".pdf"
End Sub
Of course it would help if we knew if the print range needed to be set.
Can I combine this code with the other one I sent?
 
Upvote 0
Re: "Can I combine this code with the other one I sent?"

Make an effort in explaining what you want.
It looks like you want to change Range A1 in Forms sheet with values from Column A in Names sheet, print a range set by a NamedRange (Print_Me) and do it all over with the next value.
Is that right?
 
Upvote 0
Exactly, now I can print out all the sheets but the program asks me every time to save a single sheet with a new name.
I'd like to print all the sheets automatically in one file pdf.
 
Upvote 0
Try this. Check and change references like sheet names if required.
Code:
Sub Try_So()
Dim a As String, shts, ws As Worksheet, prntNames, i As Long
a = ActiveSheet.Name
prntNames = Sheets("Names").Range("A1:A" & Sheets("Names").Cells(Rows.Count, 1).End(xlUp).Row).Value
Worksheets("Form").PageSetup.PrintArea = Range("Print_Me").Address
    For i = LBound(prntNames) To UBound(prntNames)
        With Sheets("Form")
            .Range("A1").Value = prntNames(i, 1)
            .PrintOut
        End With
    Next i
    For Each ws In ThisWorkbook.Worksheets
        shts = shts & "," & ws.Name
    Next ws
Sheets(Split(Mid(shts, 2), ",")).Select
ActiveSheet.ExportAsFixedFormat xlTypePDF, ThisWorkbook.Path & "\" & "INOpportuno At MrExcel" & ".pdf"
Sheets(a).Select
End Sub
 
Upvote 0
Try this. Check and change references like sheet names if required.
Code:
Sub Try_So()
Dim a As String, shts, ws As Worksheet, prntNames, i As Long
a = ActiveSheet.Name
prntNames = Sheets("Names").Range("A1:A" & Sheets("Names").Cells(Rows.Count, 1).End(xlUp).Row).Value
Worksheets("Form").PageSetup.PrintArea = Range("Print_Me").Address
    For i = LBound(prntNames) To UBound(prntNames)
        With Sheets("Form")
            .Range("A1").Value = prntNames(i, 1)
            .PrintOut
        End With
    Next i
    For Each ws In ThisWorkbook.Worksheets
        shts = shts & "," & ws.Name
    Next ws
Sheets(Split(Mid(shts, 2), ",")).Select
ActiveSheet.ExportAsFixedFormat xlTypePDF, ThisWorkbook.Path & "\" & "INOpportuno At MrExcel" & ".pdf"
Sheets(a).Select
End Sub
It's the same, It prints 15 different pdf and one more with 2 sheets.
I have one sheet who generates different value based on different value of one cell (list).
I'm not good, maybe I do some mistakes...
I need to generate one Pdf with all these sheets based on one sheet.
 
Upvote 0
Change your active printer to a line printer.

"with all these sheets based on one sheet"
This is new.

Last kick at the cat.
Explain, as previously asked, what you want. You never said that you wanted to save all these sheets as PDF.
In your first post, it just says Printout which means to make a hardcopy on the printer. Certainly mention which printer everything has to go to.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,813
Messages
6,181,116
Members
453,021
Latest member
Justyna P

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