Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

pdfname =

0

 In using a macro to print to a PDF, can I use more that one cell for my "pdfname="   ie.: pdfname = Range ("A2:A3") . Text  ?  My macro works with one cell, but does not work with two cells.

This is my macro:

Sub Print_PDF()

    Range("Print_Area").Select

    pdfname = Range("A2").Text

    ActiveSheet.ExportAsFixedFormat Type:=xlsxTypePDF, Filename:="C:\Users\Bar\Documents\Steph\Pay\" + pdfname + ".pdf" _

, IgnorePrintAreas:=False, OpenAfterPublish:=False

 End Sub

Answer
Discuss

Answers

0
Selected Answer

You can concatenate the values in any number of cells simply with an ampersand. 

Sub Print_PDF()
    Dim MyPath As String
    Dim PDFname As String

    Range("Print_Area").Select
    MyPath = "C:\Users\Bar\Documents\Steph\Pay\"
    PDFname = Cells(2, "A").Value & Cells(2, "B").Value &  ".PDF"
    ActiveSheet.ExportAsFixedFormat Type:=xlsxTypePDF, Filename:=MyPath & PDFname, _
                         IgnorePrintAreas:=False, OpenAfterPublish:=False
 End Sub

Use an ampersand to concatenate text and the plus sign (+) to add numbers. Use the syntax for addressing cells when you address cells. Reserve the syntax for addressing ranges for when you address groups of cells. A cell's Text property may not give you the correct result if the cell contains a formula. The Value property will return whatever you see on the screen.

Discuss


Answer the Question

You must create an account to use the forum. Create an Account or Login