I am struggling to write a macro for a multi sheet WB. Here is what I'm facing.
Here are the Requirements i'm attempting to address.
Each individual tab needs to be exported to a pipe delimited text file.
Each Text File needs to named according to a cell in a separate "Outbound File" Tab
The Files should be saved in the same directory as the Original File if possible.
To give more context
Take for example my Tabs are named Tab1, Tab2, Tab3, and Outbound File.
I need Tab1 to be saved under the name found in Cell A1 of the "Outbound file Tab". Tab2 would correspond to A2, and Tab 3 would correspond to A3.
Thanks
Hello Machie,
Welcome to the Forum!
This macro will create a text file from worksheet, except "Outbound File", and delimit the data with the pipe character. The files will be saved in the default location for saved workbooks.
Sub SaveSheetsAsTextFiles() Dim Data() As Variant Dim FileName As String Dim FilePath As String Dim FSO As Object Dim I As Long Dim LastCol As Long Dim LastRow As Long Dim NamesWks As Worksheet Dim R As Long Dim Rng As Range Dim RngEnd As Range Dim Text As String Dim TextFile As Object Dim Wks As Worksheet Set NamesWks = Worksheets("OutBound File") FilePath = Application.DefaultFilePath & "\" Set Rng = NamesWks.Range("A1") Set RngEnd = NamesWks.Cells(Rows.Count, Rng.Column).End(xlUp) Set Rng = NamesWks.Range(Rng, RngEnd) Set FSO = CreateObject("Scripting.FileSystemObject") For Each FileName In Rng I = I + 1 If I > Worksheets.Count Then GoTo Finished If Worksheets(I).Name <> NamesWks.Name Then Set TextFile = FSO.OpenTextFile(FilePath & FileName, 2, True, -2) LastCol = Wks.Cells.Find("*", , xlValues, xlWhole, xlByRows, xlPrevious, False).Column LastRow = Wks.Cells.Find(What:="*", SearchOrder:=xlByColumns).Row ReDim Data(1 To LastCol) For R = 1 To LastRow Data = Wks.Cells(R, 1).Resize(1, LastCol).Value Text = Join(WorksheetFunction.Index(Data, 1, 0), "|") & vbCrLf TextFile.Write Text Next R TextFile.Close End If Next FileName Finished: Set FSO = Nothing Set TextFile = Nothing End Sub
Sincerely,
Leith Ross
Remember To Do the Following....
1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.2. Thank those who have helped you by clicking the Starbelow the post.
3. Please mark your post [SOLVED] if it has been answered satisfactorily.
Old Scottish Proverb...
Luathaid gu deanamh maille! (Rushing causes delays!)
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks