Forum Discussion

Deleted's avatar
Deleted
Jul 27, 2018

Export Excel worksheet to PDF using cell value make a filename

Dear all,

 

I'm making an invoice system most of the functions work good so far but I got stuck in this one.

 

Private Sub CommandButton2_Click()
    Sheets("Pakbon").ExportAsFixedFormat Type:=xlTypePDF, _
    Filename:="C:\Users\Mann\Desktop\NaLeveringTool\Pakbon_" & Format(Now(), "dd-mm-yyyy_hh_mm") & ".pdf"

 

this works but I want to add a project number in the front of the filename using a cell information.

 

sample:

(Projectnumber from worksheet Cell)Pakbon_(Date_hour).pdf

PR12315Pakbon_27-07-2018_13_11.pdf

 

but I'm not sure how to do it.

 

is there an expert can help me?

 

Thanks in advance.

  • Haytham Amairah's avatar
    Haytham Amairah
    Silver Contributor

    Hi Nico,

     

    To get access to a specific cell value in a specific sheet, you can use the Value property of Range object in VBA as below example:

    Sheets("Pakbon").Range("A1").Value

     

    Let's say that the project number is in cell A1, so please update the code as follows:

    Private Sub CommandButton2_Click()
        
        Dim path As String
        path = "C:\Users\Mann\Desktop\NaLeveringTool\" & Sheets("Pakbon").Range("A1").Value & _
                                                 "Pakbon_" & _
                                                  Format(Now(), "dd-mm-yyyy_hh_mm") & ".pdf"
                                                  
        Sheets("Pakbon").ExportAsFixedFormat Type:=xlTypePDF, _
        Filename:=path
        
    End Sub

     

    Regards

Resources