File name from two cell values

VbaHell

Well-known Member
Joined
Jan 30, 2011
Messages
1,220
Hello All

This code saves my excel sheet as a PDF and then saves this named as Range "H7"

How can I change this please to save as "D19" & "-" & "H7"

Code:
Sheet1.Range("A4:J56").ExportAsFixedFormat xlTypePDF, Filename:= _
"C:\Invoices\" & Sheet1.Range("H17").Value, Openafterpublish:=True
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
I think it usually looks cleaner to build the file name separately in a variable, then reference that variable, i.e.
Code:
Dim myFileName as String
myFileName = [COLOR=#333333]"C:\Invoices" & [/COLOR][COLOR=#333333]Sheet1.Range("D19") & "-" & [/COLOR][COLOR=#333333]Sheet1.Range("H7")[/COLOR]
Sheet1.Range("A4:J56").ExportAsFixedFormat xlTypePDF, Filename:= _
[FONT=Verdana]    myFileName[/FONT], Openafterpublish:=True
 
Last edited:
Upvote 0
You are welcome.

By the way, there should be a backslash after "Invoices" in my variable.
Sometimes, the board does weird things and drops backslashes and greater than or less than signs.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,306
Members
452,633
Latest member
DougMo

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