![]() |
Saving sheets as tab delimited text files
I have used the code from http://www.cpearson.com/excel/imptext.htm and
adapted it as below. There are two sheets named BOMIN & BOMINh, and the macro produces two files, but they are both the same. Whichever sheet is current when the macro is run is saved twice but with different filenames. How do I save each sheet? My code is as below Public Sub SaveTextFiles() Dim FName As Variant Dim Sep As String Dim m As Integer Dim sStr As String Dim MyPath As String MyPath = "C:\SAPDATA\" For m = 1 To Sheets.Count FName = MyPath & Sheets(m).Name FName = sStr & FName & ".txt" Sep = vbTab ExportToTextFile CStr(FName), Sep, False, False Next m End Sub Any help would be appreciated. |
Saving sheets as tab delimited text files
The ExportToTextFile subroutine works on the activesheet.
So make sure you change sheets in your code: For m = 1 To Sheets.Count sheets(m).select '<--added FName = MyPath & Sheets(m).Name FName = sStr & FName & ".txt" Sep = vbTab ExportToTextFile CStr(FName), Sep, False, False Next m PeteN wrote: I have used the code from http://www.cpearson.com/excel/imptext.htm and adapted it as below. There are two sheets named BOMIN & BOMINh, and the macro produces two files, but they are both the same. Whichever sheet is current when the macro is run is saved twice but with different filenames. How do I save each sheet? My code is as below Public Sub SaveTextFiles() Dim FName As Variant Dim Sep As String Dim m As Integer Dim sStr As String Dim MyPath As String MyPath = "C:\SAPDATA\" For m = 1 To Sheets.Count FName = MyPath & Sheets(m).Name FName = sStr & FName & ".txt" Sep = vbTab ExportToTextFile CStr(FName), Sep, False, False Next m End Sub Any help would be appreciated. -- Dave Peterson |
Saving sheets as tab delimited text files
Thanks Dave, worked a treat.
"Dave Peterson" wrote: The ExportToTextFile subroutine works on the activesheet. So make sure you change sheets in your code: For m = 1 To Sheets.Count sheets(m).select '<--added FName = MyPath & Sheets(m).Name FName = sStr & FName & ".txt" Sep = vbTab ExportToTextFile CStr(FName), Sep, False, False Next m PeteN wrote: I have used the code from http://www.cpearson.com/excel/imptext.htm and adapted it as below. There are two sheets named BOMIN & BOMINh, and the macro produces two files, but they are both the same. Whichever sheet is current when the macro is run is saved twice but with different filenames. How do I save each sheet? My code is as below Public Sub SaveTextFiles() Dim FName As Variant Dim Sep As String Dim m As Integer Dim sStr As String Dim MyPath As String MyPath = "C:\SAPDATA\" For m = 1 To Sheets.Count FName = MyPath & Sheets(m).Name FName = sStr & FName & ".txt" Sep = vbTab ExportToTextFile CStr(FName), Sep, False, False Next m End Sub Any help would be appreciated. -- Dave Peterson |
All times are GMT +1. The time now is 03:26 PM. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com