Worksheet save as .csv(Comma deli) and rename as .txt by cmd.exe

haribole

New Member
Joined
Apr 10, 2018
Messages
19
I am trying to save a sheet as .Csv (Comma delimited) and then rename as .Txt by Cmd.exe (Shell) but not working.
Please help me !!!

Dim MyFolder As String, MyFile As String, MySheetName as String
Dim ShellComand As String

.........

MyFolder = "D:\Revise"
MyFile = Dir(MyFolder & "\*.txt")
Workbooks.OpenText Filename:=MyFolder & "" & MyFile, StartRow:=1, DataType:=xlDelimited, Comma:=True, Tab:=False, Semicolon:=False, Space:=False, Other:=False, FieldInfo:=Array(Array(1, xlTextFormat), Array(2, xlTextFormat), Array(3, xlTextFormat))
MySheetName = Replace(ActiveWorkbook.Name, ".txt", "")
ActiveWorkbook.Sheets(1).Copy After:=Workbooks("Revise.xlsm").Worksheets(Sheets.Count)
ActiveSheet.Name = MySheetName
Worksheets(2).Activate
Application.DisplayAlerts = False
ActiveSheet.SaveAs Filename:=MyFolder & "" & MySheetName + "_Rev_" + Replace(Date, "-", ""), FileFormat:=xlCSV, CreateBackup:=True
'Problem here: .Csv file not saving with same cell format (text)
Application.DisplayAlerts = True
ShellComand = "cmd.exe /k ""cd " & """ & ThisWorkbook.path & """ & " ren *.csv *.txt"""
Shell ShellComand
'Problem here: it can't rename into .txt


Can anybody help me out of this problem ?
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Instead of using notepad, why not save the file directly to a .txt extension?

Code:
Sub CSVFileSaveAsTxt()    ' save a .csv file with a .txt extention
    Dim MyFolder As String, MyFile As String, MySheetName As String
    Dim SaveAsFileName As String

    MyFolder = "D:\Revise"
    MyFile = Dir(MyFolder & "\*.txt")

    Workbooks.OpenText Filename:=MyFolder & "" & MyFile, StartRow:=1, DataType:=xlDelimited, Comma:=True, Tab:=False, Semicolon:=False, Space:=False, Other:=False, FieldInfo:=Array(Array(1, xlTextFormat), Array(2, xlTextFormat), Array(3, xlTextFormat))
    MySheetName = Replace(ActiveWorkbook.Name, ".txt", "")
    ActiveWorkbook.Sheets(1).Copy After:=Workbooks("Revise.xlsm").Worksheets(Sheets.Count)
    ActiveSheet.Name = MySheetName
    Worksheets(2).Activate
    Application.DisplayAlerts = False

    SaveAsFileName = MyFolder & "\" & MySheetName & "_Rev_" & Day(Date) & Month(Date) & Year(Date) & ".txt"
    ActiveSheet.Copy
    ActiveWorkbook.SaveAs Filename:=SaveAsFileName, FileFormat:=xlCSV, CreateBackup:=True
    ActiveWorkbook.Close False
    Application.DisplayAlerts = True
End Sub

BTW, for clarity when posting code
attachment.php
 
Upvote 0

Forum statistics

Threads
1,215,046
Messages
6,122,849
Members
449,096
Latest member
Erald

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