Help with VBA

inspectorNDT

New Member
Joined
May 10, 2019
Messages
16
I am really new to VBA's. I am trying to make a "create PDF" button to make a PDF out of the active tab. The file name would be using cells on the active tab then the name of the tab and then another cell.

I would also like it to prompt the user if a file of the same name exist and I would like it to auto save to the active users desktop.

thank you in advance for your help
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
.
This is one method :

Code:
Option Explicit


Sub ExportToPDFs()
Dim nm As String
Dim ws As Worksheet
Dim saveInFolder As String
Dim celOne As String, celTwo As String, celThree As String
    
    saveInFolder = Environ("USERPROFILE") & "\Desktop"
    If Right(saveInFolder, 1) <> "\" Then
        saveInFolder = saveInFolder & "\"
    End If
                    nm = ActiveSheet.Name
                    celOne = Range("A1").Value
                    celTwo = Range("C2").Value
                    celThree = Range("F1").Value
            
                    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
                    Filename:=saveInFolder & celOne & celTwo & nm & celThree & ".pdf", _
                    Quality:=xlQualityStandard, IncludeDocProperties:=True, _
                    IgnorePrintAreas:=False, OpenAfterPublish:=False
        
End Sub
 
Upvote 0
thank you for you help but all that happens is an 'Run time error '1004' application-defined or object-defined error. I did change the cell numbers to reflect what I need.

Option Explicit




Sub ExportToPDFs()
Dim nm As String
Dim ws As Worksheet
Dim saveInFolder As String
Dim celOne As String, celTwo As String, celThree As String

saveInFolder = Environ("USERPROFILE") & "\Desktop"
If Right(saveInFolder, 1) <> "" Then
saveInFolder = saveInFolder & ""
End If
nm = ActiveSheet.Name
celOne = Range("D1").Value
celTwo = Range("C5").Value
celThree = Range("O2").Value

ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=saveInFolder & celOne & celTwo & nm & celThree & ".pdf", _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=False

End Sub
 
Upvote 0
Ok, try this....AND make sure the cell references for the filenames are correct AND populated

Code:
Sub ExportToPDFs()
Dim nm As String
Dim ws As Worksheet
Dim saveInFolder As String
Dim celOne As String, celTwo As String, celThree As String

saveInFolder = Environ("USERPROFILE") & "\Desktop\"
If Right(saveInFolder, 1) <> "" Then
saveInFolder = saveInFolder & ""
End If
nm = ActiveSheet.Name
celOne = Range("D1").Value
celTwo = Range("C5").Value
celThree = Range("O2").Value

ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
filename:=saveInFolder & celOne & celTwo & nm & celThree & ".pdf", _
Quality:=xlQualityStandard, IncludeDocProperties:=True
End Sub
 
Last edited:
Upvote 0
.
You were missing part of the macro code :

Code:
Sub ExportPDF()
Dim nm As String
Dim ws As Worksheet
Dim saveInFolder As String
Dim celOne As String, celTwo As String, celThree As String
    
    saveInFolder = Environ("USERPROFILE") & "\Desktop"
    If Right(saveInFolder, 1) <> "\" Then
        saveInFolder = saveInFolder & "\"
    End If
                    nm = ActiveSheet.Name
                    celOne = Range("D1").Value
                    celTwo = Range("C5").Value
                    celThree = Range("O2").Value


                    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
                    Filename:=saveInFolder & celOne & celTwo & nm & celThree & ".pdf", _
                    Quality:=xlQualityStandard, IncludeDocProperties:=True, _
                    IgnorePrintAreas:=False, OpenAfterPublish:=False
End Sub
 
Upvote 0
.
You are welcome. Glad to help.
 
Upvote 0
Still getting the run time error with this code

.
You were missing part of the macro code :

Code:
Sub ExportPDF()
Dim nm As String
Dim ws As Worksheet
Dim saveInFolder As String
Dim celOne As String, celTwo As String, celThree As String
    
    saveInFolder = Environ("USERPROFILE") & "\Desktop"
    If Right(saveInFolder, 1) <> "\" Then
        saveInFolder = saveInFolder & "\"
    End If
                    nm = ActiveSheet.Name
                    celOne = Range("D1").Value
                    celTwo = Range("C5").Value
                    celThree = Range("O2").Value


                    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
                    Filename:=saveInFolder & celOne & celTwo & nm & celThree & ".pdf", _
                    Quality:=xlQualityStandard, IncludeDocProperties:=True, _
                    IgnorePrintAreas:=False, OpenAfterPublish:=False
End Sub
 
Upvote 0
Also I can't tell what is different between this code and the first one you sent me.

.
You were missing part of the macro code :

Code:
Sub ExportPDF()
Dim nm As String
Dim ws As Worksheet
Dim saveInFolder As String
Dim celOne As String, celTwo As String, celThree As String
    
    saveInFolder = Environ("USERPROFILE") & "\Desktop"
    If Right(saveInFolder, 1) <> "\" Then
        saveInFolder = saveInFolder & "\"
    End If
                    nm = ActiveSheet.Name
                    celOne = Range("D1").Value
                    celTwo = Range("C5").Value
                    celThree = Range("O2").Value


                    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
                    Filename:=saveInFolder & celOne & celTwo & nm & celThree & ".pdf", _
                    Quality:=xlQualityStandard, IncludeDocProperties:=True, _
                    IgnorePrintAreas:=False, OpenAfterPublish:=False
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,935
Messages
6,175,494
Members
452,649
Latest member
mr_bhavesh

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