Print macro

GregoryT

New Member
Joined
Oct 30, 2019
Messages
13
I have A print button on Sheet1 and created the following macro. The sheet I want to print is on sheet6. Where did I screw up, kinda green at this stuff. TIA

Sub CreatePDF()

Dim ID As String

ID = " Range("B1").Text

activesheet.exportasfixedformat _
Type:"xltTypePDF, _
Filename:"C:\Users\Greg\Documents\RecipePdf\"+ID+".pdf", _
IgnorePrintAreas:"False, _
OpenAfterPublish:"False

End Sub
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Try the below..

VBA Code:
ID = " Range("B1").Text
should be
VBA Code:
ID = Range("B1").Value
(i.e. no quotes at the front) if Range("B1") is on the ActiveSheet and contains "Sheet6" without the quotes

and

VBA Code:
Filename:"C:\Users\Greg\Documents\RecipePdf\"+ID+".pdf"
change to
VBA Code:
Filename:"C:\Users\Greg\Documents\RecipePdf\" & ID & ".pdf"
 
Upvote 0
@MARK858 hi made the corrections but got a syntax error and when I hit ok the first Line "Sub CreatePDF() Turned yellow

Screenshot (37).png
 
Upvote 0
Please paste the code directly in the thread (preferably in code tags.... paste the code, select the code and click the VBA icon in the reply window) rather than an image.
 
Upvote 0
Here it is Hope thats right. My print button is on Sheet1 - Sheet 6 is the sheet that is auto populated and printed to pdf (Not shown/not hidden).
Again thanks for your help.

VBA Code:
Sub CreatePDF()

Dim ID As String

ID = Range("B2").Value

activesheet.exportasfixedformat _
Type:"xltTypePDF, _
Filename:"C:\Users\Greg\Documents\RecipePdf\" & ID & ".pdf" _
IgnorePrintAreas:"False, _
OpenAfterPublish:"False

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,976
Messages
6,122,541
Members
449,089
Latest member
davidcom

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