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 ?
 

rlv01

Active Member
Joined
May 16, 2017
Messages
469
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
 

Forum statistics

Threads
1,082,167
Messages
5,363,532
Members
400,747
Latest member
monty_gl

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top