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
 

Some videos you may like

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
55,971
Office Version
  1. 365
Platform
  1. Windows
Try changing this line:
Code:
[COLOR=#333333]wbNewName = ws.ListObjects(1).Name[/COLOR]
to:
Code:
[COLOR=#333333]wbNewName = Range("L23")[/COLOR]
 

ESPN8

New Member
Joined
Aug 13, 2019
Messages
19
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
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
55,971
Office Version
  1. 365
Platform
  1. Windows
What exactly is in cell L23?
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
12,594
Office Version
  1. 2007
Platform
  1. Windows

ADVERTISEMENT

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]
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
12,594
Office Version
  1. 2007
Platform
  1. Windows

ADVERTISEMENT

(y)
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
12,594
Office Version
  1. 2007
Platform
  1. Windows
Then you must answer Joe's question in post #4
 

Watch MrExcel Video

Forum statistics

Threads
1,123,177
Messages
5,600,160
Members
414,367
Latest member
dw970906

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
Top