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

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)

MARK858

MrExcel MVP
Joined
Nov 12, 2010
Messages
14,041
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
  2. Mobile
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"
 

GregoryT

New Member
Joined
Oct 30, 2019
Messages
13
@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
 

MARK858

MrExcel MVP
Joined
Nov 12, 2010
Messages
14,041
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
  2. Mobile
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.
 

GregoryT

New Member
Joined
Oct 30, 2019
Messages
13
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
 

Watch MrExcel Video

Forum statistics

Threads
1,129,410
Messages
5,636,118
Members
416,900
Latest member
Mokor

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
Top