Saving workbook to CSV with Custom Name

ESPN8

New Member
Joined
Aug 13, 2019
Messages
19
Hi Everyone,

I have figured out some code to copy all the data from a table, create a new workbook, paste the data and then save the workbook as a CSV as the name of the original table the data came from. I now am trying to figure out how to save the file as a custom name from cell L23 from the original workbook. Any help would be appreciated.

Thank you in Advance

Sub ExportTableToCSVFile()
Dim wb As Workbook, wbNew As Workbook
Dim ws As Worksheet, wsNew As Worksheet
Dim wbNewName As String


Set wb = ThisWorkbook
Set ws = ActiveSheet
Set wbNew = Workbooks.Add
With wbNew
Set wsNew = wbNew.Sheets("Sheet1")
wbNewName = ws.ListObjects(1).Name
ws.ListObjects(1).DataBodyRange.Copy
wsNew.Range("A1").PasteSpecial Paste:=xlPasteValuesAndNumberFormats
.SaveAs Filename:=wb.Path & "" & wbNewName & ".csv", _
FileFormat:=xlCSVMSDOS, CreateBackup:=False
End With
ActiveWorkbook.Close SaveChanges:=True

End Sub
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Try changing this line:
Code:
[COLOR=#333333]wbNewName = ws.ListObjects(1).Name[/COLOR]
to:
Code:
[COLOR=#333333]wbNewName = Range("L23")[/COLOR]
 
Upvote 0
Try changing this line:
Code:
[COLOR=#333333]wbNewName = ws.ListObjects(1).Name[/COLOR]
to:
Code:
[COLOR=#333333]wbNewName = Range("L23")[/COLOR]
@Joe4 I have tried that and got an error

Run-Time Error 1004'
Method SaveAs' of object'_Workbook' failed

then
.SaveAs Filename:=wb.Path & "" & wbNewName & ".csv", _
FileFormat:=xlCSVMSDOS, CreateBackup:=False
is highlighted in yellow
 
Upvote 0
What exactly is in cell L23?
 
Upvote 0
The reference to the sheet was missing:

Try changing this line:
Code:
[COLOR=#333333]wbNewName = ws.ListObjects(1).Name[/COLOR]
to:
Code:
[COLOR=#333333]wbNewName = [/COLOR][COLOR=#ff0000][B]ws.[/B][/COLOR][COLOR=#333333]Range("L23")[/COLOR]
 
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,305
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