2 questions related to VBA

STIRRELL

Board Regular
Joined
Dec 30, 2010
Messages
67
Office Version
  1. 365
Hi,
I have 3 worksheets in a workbook. Spreadsheets named EXCEL, PDF and CSV in the TEMPLATE workbook. ( different details on each tab). I want to save off the Excel tab as its own excel workbook and the name based on the value in B1. I would like to save to the C Drive in a folder called Macro. The second step is the PDF spreadsheet as Name in Cell B2. I would like to save off the PDF tab to the same location and save as PDF. and similar on the 3rd tab save of as CSV also saved to the Macro folder. I have not created the name field yet but to make is easy say its cell B3.
Can you please help me with the code below. I am guessing once I have one step working the others will only need slight modifications.
VBA hates the Path field as well. And when I run it exports as excel and does not save anywhere. ( probably due to the error)
Any help is appreciated. Can an VBA master please help.

Dim name As String
Dim Path As String
Path = C:\MACRO
Name = Range("B2")
ActiveSpreadsheet.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=Path &Name& ".pdf"

1733253267103.png
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Best not to use reserved words in variable names.
Also, missing last backslash and quotes around Path value.

Try this:
VBA Code:
Dim myName As String
Dim myPath As String
myPath = "C:\MACRO\"
myName = Range("B2")
ActiveSpreadsheet.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=myPath & myName & ".pdf"
 
Upvote 0
Hi thank you so much, I was able to get it to export to the specific file on my C drive to the Macro1 folder ( big win) :) but for some reason its exporting them all as PDF's even though I changed the Xltype to XLSX and CSV respectively. Any suggestions?




Sheets("EXCEL1").Select
Dim MyName1 As String
Dim MyPath1 As String
MyPath1 = "C:\MACRO1\"
MyName1 = Range("A2")
ActiveSheet.ExportAsFixedFormat xlXLSX, MyPath & MyName & ".xlsx", Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
 
Upvote 0
Turn on your Macro Recorder, and record yourself saving as an XLSX and a CSV file type manually, and then view the code that you recorded.
They all have different file types, so you need to make sure you have all that code correct.
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,285
Members
452,902
Latest member
Knuddeluff

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