0

I am trying to find a fast way to save my xlsx files as csv files with the same filename as the xlsx file (just in csv format). I have recorded a macro with shortcut, but the issue is that whenever I try with a new file it saves as a the same filename I recorded initial macro with (i.e. see below, probably because I have the file labelled in code as: 3WDL_1 (2014-08-07)10secDataTable sit.csv). Is there something I need to replace 3WDL_1 (2014-08-07)10secDataTable sit.csv with to make the macro save with the same filename as the actual workbook I am working with.

So basically I have a folder full of xlsx files and I want to use a shortcut/macro on each xslx file to convert them into a csv files that have exactly the same name as original xlsx file, and are saved into the same folder.

I need to replace the 3WDL_1 (2014-08-07)10secDataTable sit.csv portion of code with something that will work with any new xlsx file I open up. Otherwise I may as well just do it the old fashioned way with mouse

Thanks so much in adcance,

Paddy

Sub SaveAsCSVFile

ChDir "C:\Users\paddy\Desktop\NEW CSV files whole CGM date ok!"
ActiveWorkbook.SaveAs Filename:= _
    "C:\Users\paddy\Desktop\NEW CSV files whole CGM date ok!\" & _
         "3WDL_1 (2014-08-07)10secDataTable sit.csv", _
        FileFormat:=xlCSVMac, CreateBackup:=False
End Sub
Maciej Los
  • 8,468
  • 1
  • 20
  • 35
  • 3
    possible duplicate of [vba excel file to csv, but keep csv filename same as original workbook](http://stackoverflow.com/questions/29874807/vba-excel-file-to-csv-but-keep-csv-filename-same-as-original-workbook) – DeanOC Apr 26 '15 at 09:05

1 Answers1

2

To get file name, use: ActiveWorkbook.Name to get path, use: ActiveWorkbook.Path

Something like this should work as well:

ActiveWorkbook.SaveAs FileName:= ActiveWorkbook.Path & "\" & _
    Replace(ActiveWorkbook.Name, "xslx", "csv"), _
    FileFormat:=xlCSVMac, CreateBackup:=False
Maciej Los
  • 8,468
  • 1
  • 20
  • 35