Exporting worksheet to CSV Runtime Error

tj_g_88

New Member
Joined
Jul 7, 2014
Messages
2
Hi all,

As a newbie to macros I am having trouble trying to save a worksheet to CSV file. The macro worked to .txt but when I change it to .xlCSV it comes up with runtime 1004 error, method 'save as' of object '_workbook' failed.

Any help would be amazing.

Cheers

Sub Export7400_setup_Click()

'
' Exportsub Macro
'
Dim sPath As String
Dim FName As String
FName = Range("rng7400Filename")
sPath = Range("strWorksheetPath")

Sheets("Caliper Sub").Select
Range("A1:E111").Select
Selection.Copy
Workbooks.Add
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
ActiveWorkbook.SaveAs filename:=(sPath) & FName & ".csv", _
FileFormat:=xlCSV
MsgBox "Click OK and Yes to save as Tab Delimited file format"
ActiveWorkbook.Close


End Sub
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Hi and welcome to the forum.

Does sPath include the trailing"\" to join to the filename?
 
Upvote 0
Try the following and see if it works for you.

Code:
Sub Export7400_setup_Click()
Dim NewWB As Workbook
Dim sPath As String
Dim FName As String

Application.DisplayAlerts = False
FName = Range("rng7400Filename")
sPath = Range("strWorksheetPath")
ThisWorkbook.Sheets("Caliper Sub").Copy
Set NewWB = ActiveWorkbook
NewWB.SaveAs Filename:=(sPath) & FName & ".csv", _
 FileFormat:=xlCSV
Application.DisplayAlerts = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,176
Members
451,543
Latest member
cesymcox

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