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
 
Upload the file to Dropbox and then post a link back here !
 
Upvote 0

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
I downloaded the file and copied that code to the spreadsheet I want to use it on and i get {If Dir(stFileName) <> "" Then} will be highlighted and a error code of {error 52 bad file name or number}.

this is the entire code

cell O2 has the date formated as 12-May-19
cell
Option Explicit


Sub ExportToPDFs()
Dim nm As String
Dim ws As Worksheet
Dim stFileName As String
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("O2").Value

stFileName = saveInFolder & celOne & celTwo & nm & celThree & ".pdf"


If Dir(stFileName) <> "" Then
MsgBox "Specified File Exists", vbInformation, "File Error"
Exit Sub
Else
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=saveInFolder & celOne & celTwo & nm & celThree & ".pdf", _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=False
End If


End Sub




 
Upvote 0
Cell d2 client name
cell c5 numbers
cell o2 date 12-May-19


I downloaded the file and copied that code to the spreadsheet I want to use it on and i get {If Dir(stFileName) <> "" Then} will be highlighted and a error code of {error 52 bad file name or number}.

this is the entire code

cell O2 has the date formated as 12-May-19
cell
Option Explicit


Sub ExportToPDFs()
Dim nm As String
Dim ws As Worksheet
Dim stFileName As String
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("O2").Value

stFileName = saveInFolder & celOne & celTwo & nm & celThree & ".pdf"


If Dir(stFileName) <> "" Then
MsgBox "Specified File Exists", vbInformation, "File Error"
Exit Sub
Else
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=saveInFolder & celOne & celTwo & nm & celThree & ".pdf", _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=False
End If


End Sub
 
Upvote 0
.
These lines should not be in your code :

cell O2 has the date formated as 12-May-19
cell

Even if you remove those two lines, the macro should not error. I don't have an answer for why it is. Sorry.

If you run just the workbook I provided for download, without changing anything, does it work there ?
 
Upvote 0
Well, it appears you are changing the code provided by @Logit and / or myself.
AND you cell references are now different for celOne, two and three
The code in your latest post is missing a backslash on this line


Code:
saveInFolder = Environ("USERPROFILE") & "\Desktop"

it should be AND was

Code:
saveInFolder = Environ("USERPROFILE") & "\Desktop\"

The error code you are receiving suggest the file location OR file name is incorrect or INVALID

Code:
Sub ExportToPDFs()
Dim nm As String
Dim ws As Worksheet
Dim stFileName As String
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("O2").Value

stFileName = saveInFolder & celOne & celTwo & nm & celThree & ".pdf"


If Dir(stFileName) <> "" Then
MsgBox "Specified File Exists", vbInformation, "File Error"
Exit Sub
Else
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=saveInFolder & celOne & celTwo & nm & celThree & ".pdf", _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=False
End If


End Sub
 
Upvote 0
.
My code had these lines after the ENVIRON statement :

Code:
If Right(saveInFolder, 1) <> "\" Then
        saveInFolder = saveInFolder & "\"
End If
 
Upvote 0
@Logit...I know !! We both provided the backslash in different ways, but the OP is modifying the code !!....:banghead:
 
Upvote 0
Sorry those aren't in the code

.
These lines should not be in your code :

cell O2 has the date formated as 12-May-19
cell

Even if you remove those two lines, the macro should not error. I don't have an answer for why it is. Sorry.

If you run just the workbook I provided for download, without changing anything, does it work there ?
 
Upvote 0
@inspectorNDT
If you save the file as an .xlsx extension, the code will be removed.
Instead save the file as an .xlsm (macro enabled) extension.
That might help solve the problem.
 
Upvote 0

Forum statistics

Threads
1,224,945
Messages
6,181,937
Members
453,074
Latest member
JefersonKollet

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