VBA to create PDF and named with cell text.

asyamonique

Well-known Member
Joined
Jan 29, 2008
Messages
1,280
Office Version
  1. 2013
Platform
  1. Windows
Good Day,
Below code is creating the PDF file and named with worksheet name.
My question is how can we change it, instead of worksheet name to create as cell text or value?
Many Thanks

VBA Code:
Sub Button1_Click()
Dim wsA     As Worksheet
Dim strFile, myfile As String
On Error Resume Next
wsA.Activate
    strFile = "\\172.16.23.181\folder\xxxxx\xxxxx\"
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
strFile & "" & ActiveSheet.Name, _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=True
End Sub
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Instead of ActiveSheet.Name use ActiveSheet.Range("A1") for whatever cell you want to reference.
 
Upvote 0
Have you actually tried to run this code yet? The red line will raise an error because wsA has not been Set:
Rich (BB code):
Sub Button1_Click()
Dim wsA     As Worksheet
Dim strFile, myfile As String
On Error Resume Next
wsA.Activate
    strFile = "\\172.16.23.181\folder\xxxxx\xxxxx\"
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
strFile & "" & ActiveSheet.Name, _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=True
End Sub
 
Upvote 0
Have you actually tried to run this code yet? The red line will raise an error because wsA has not been Set:
Rich (BB code):
Sub Button1_Click()
Dim wsA     As Worksheet
Dim strFile, myfile As String
On Error Resume Next
wsA.Activate
    strFile = "\\172.16.23.181\folder\xxxxx\xxxxx\"
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
strFile & "" & ActiveSheet.Name, _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=True
End Sub
Its working with my file.
 
Upvote 0
Aha. You have On Error Resume Next. I don't advise that because it will cause errors to be ignored. The reason your code works is that the line
VBA Code:
wsA.Activate
raises an error, the error is ignored, so it does nothing it all. You might as well delete it. Whatever sheet is active when the macro runs is that one that will be exported to the PDF.
 
Upvote 0
Yes you are right:)
I have summary report which located on one worksheet named as Summary Report, so everyday that report converting to pdf and kept as pdf file by changing name as date due to pdf automaticly creating with the worksheet name.
I was wonderind that the cell value a1 has date formula =today() which will help me to create pdf with that cell name.
Hope its clear.
 
Upvote 0
If you refer to that cell value in VBA you will get the default date format, such as "9/9/2022", which is an illegal file name, so will cause an error.

If you want to use the current date you don't need to refer to a cell at all. You can do this. Adjust date format as desired:

Rich (BB code):
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
strFile & " " & ActiveSheet.Name & " " & Format(Date, "yyyy-mm-dd"), _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=True
 
Upvote 0

Forum statistics

Threads
1,215,028
Messages
6,122,749
Members
449,094
Latest member
dsharae57

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