Macro that stores all sheets as tab-delimited text files

B

b.d.jensen

Hi!
I'm trying to write an macro that converts one single xls-file
to multiple text file (tab delimited) with filenames corrsponding to
the names of the sheets.

How to do that?

I recorded my actions:
Sub GemAsTxt()
ActiveWorkbook.SaveAs Filename:= _
"H:\Develop\work\TestData\Testmodel.txt", FileFormat:= _
xlText, CreateBackup:=False
End Sub

But what I need is:
Sub GemAsTxt()
for all sheets:
ActiveWorkbook.SaveAs Filename:= _

"<directoryOfCurrentExcelFile>\<excelfileWithoutFileType>\<sheetname>",
FileFormat:= _
xlText, CreateBackup:=False
End Sub
 
G

Guest

Doesn't work unless a directory matching the name of the workbook (without
the .xls) already exists

Sub SaveEm()
Dim strPath As String
Dim ws As Worksheet

With ActiveWorkbook
strPath = .Path & "\" & WorksheetFunction.Substitute(.Name, ".xls", "\")
For Each ws In .Worksheets
ws.SaveAs strPath & ws.Name & ".txt", xlTextWindows
Next
End With
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top