+ Reply to Thread
Results 1 to 5 of 5

writing excel file to separate tab separated text files with header in each

Hybrid View

gsindby writing excel file to... 09-15-2014, 12:03 AM
fredlo2010 Re: writing excel file to... 09-15-2014, 02:10 AM
Marc L Try this demonstration ! 09-15-2014, 04:49 AM
gsindby Re: writing excel file to... 09-15-2014, 10:14 AM
fredlo2010 Re: writing excel file to... 09-15-2014, 10:19 AM
  1. #1
    Registered User
    Join Date
    09-14-2014
    Location
    3819
    MS-Off Ver
    Windows 2013
    Posts
    2

    writing excel file to separate tab separated text files with header in each

    Hello,
    I am relatively new to VBA, so apologies if this is naive but I have been struggling with this. I have tried to look through past posts on this subject but have had a hard time resolving having headers and tab file output in the same solution (individually yes, but the code in each answer seemed very different and didn't know how to combine them. I am using Office 2013.

    I have an excel file with 12 columns and 123 rows. The first row is a header row that I would like repeated in every file, along with one row of data in each file (so 122 files total). I need the files to be named what is in column A for each row, but that column not be included in the file (so column 2-12 will be written to file). Finally, I need the output saved as a Tab separated txt file.

    Thank you very much in advance for your help...having a hard time figuring this one out on my own.

    EcoHIV_metadata.xlsm

  2. #2
    Valued Forum Contributor fredlo2010's Avatar
    Join Date
    07-04-2012
    Location
    Miami, United States
    MS-Off Ver
    Excel 365
    Posts
    762

    Re: writing excel file to separate tab separated text files with header in each

    Hi see if this helps. Run the code named CreateFiles. Note that if there is a file with the same under the same folder it will be deleted and replaced with a new one.

    Option Explicit
    
    Sub CreateFiles()
        
        ' Change this to the path in your computer
        Const strBASE_PATH As String = "C:\Users\Alfred\Desktop\MyData\"
        
        ' The total amount of columns to be copied
        Const lDATA_COLUMN_WIDTH As Long = 11
        Const strEXTENSION As String = ".txt"
        
        
        Dim wbData As Workbook
        Dim wbFile As Workbook
        
        Dim lRow As Long
        Dim strFullPath As String
        Dim i As Long
        
        Call TurnExtrasOff
        
        ' Asign the variables
        Set wbData = ActiveWorkbook
        lRow = wbData.Sheets(1).Cells(wbData.Sheets(1).Rows.Count, "A").End(xlUp).Row
        
        ' Check if the filder exits if not create one
        If IsValidPath(strBASE_PATH, True) Then
        
            With wbData.Sheets(1)
                ' Loop through the data.
                For i = 2 To lRow
                    
                    ' Prepare the file path.
                    strFullPath = strBASE_PATH & Cells(i, 1).Value & strEXTENSION
                    
                    ' If there is a file with the same name delete it.
                    On Error Resume Next
                    Kill strFullPath
                    On Error GoTo 0
                    
                    ' Add a new workbook and copy the data.
                    Workbooks.Add
                    Set wbFile = ActiveWorkbook
                    
                    ' Copy the headers.
                    wbFile.Sheets(1).Cells(1, 1).Resize(, lDATA_COLUMN_WIDTH).Value = _
                    .Cells(1, 2).Resize(, lDATA_COLUMN_WIDTH).Value
                    
                    ' Copy the data.
                    wbFile.Sheets(1).Cells(2, 1).Resize(, lDATA_COLUMN_WIDTH).Value = _
                    .Cells(i, 2).Resize(, lDATA_COLUMN_WIDTH).Value
                    
                    
                    ' Save and close the file the file.
                    wbFile.SaveAs Filename:=strFullPath, FileFormat:=xlText
                    wbFile.Close saveChanges:=True
                Next i
            End With
        End If
        
        Call TurnExtrasOn
        
        ' Cleanup
        Set wbData = Nothing
        Set wbFile = Nothing
          
        
    End Sub
    
    Private Function IsValidPath(ByVal strPath As String, Optional ByVal bCreatePath As Boolean = False) As Boolean
        
        Dim fso As Object
    
        Set fso = CreateObject("Scripting.FileSystemObject")
    
        'Check if folder exists.
        IsValidPath = fso.folderExists(strPath)
    
        'Create the directory.
        If bCreatePath And Not IsValidPath Then
            fso.CreateFolder strPath
            IsValidPath = True
        End If
    
        ' Clean up.
        Set fso = Nothing
    
    End Function
    
    Private Sub TurnExtrasOff()
        
        With Application
            .ScreenUpdating = False
            .EnableEvents = False
            .Calculation = xlCalculationManual
        End With
        
    End Sub
    Private Sub TurnExtrasOn()
        
        With Application
            .Calculation = xlCalculationAutomatic
            .ScreenUpdating = True
            .EnableEvents = True
        End With
        
    End Sub
    Hope it helps

  3. #3
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Lightbulb Try this demonstration !


    Hi,

    needs less than 20 code lines with standard VBA functions !

    Code to paste directly in the worksheet module :

    Sub Demo()
        With [A1].CurrentRegion
            VN = .Columns(1).Value
            VA = .Columns(2).Resize(, .Columns.Count - 1).Value
            H$ = Join(Application.Index(VA, 1), vbTab) & vbNewLine
            F% = FreeFile
            
            For R& = 2 To .Rows.Count
                Open ThisWorkbook.Path & "\" & VN(R, 1) & ".txt" For Output As #F
                Print #F, H & Join(Application.Index(VA, R), vbTab)
                Close #F
            Next
            
            Erase VA, VN
        End With
    End Sub
    Enjoy it and don't forget to clik on bottom left star "Add Reputation", thanks !

  4. #4
    Registered User
    Join Date
    09-14-2014
    Location
    3819
    MS-Off Ver
    Windows 2013
    Posts
    2

    Re: writing excel file to separate tab separated text files with header in each

    Thank you both so much. I ended up using Marc's reply and it worked perfectly!

  5. #5
    Valued Forum Contributor fredlo2010's Avatar
    Join Date
    07-04-2012
    Location
    Miami, United States
    MS-Off Ver
    Excel 365
    Posts
    762

    Re: writing excel file to separate tab separated text files with header in each

    I am glad I was able to help. My code is longer indeed but easier to understand and debug. Just saying yeah its more than 20 lines!

    Thanks

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 7
    Last Post: 12-02-2013, 03:16 AM
  2. Search cell for multiple text , return comma separated text in separate cell if found
    By dangerdoug in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-01-2013, 01:52 PM
  3. Writing data into excel from a text file
    By chris10 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-24-2011, 07:04 AM
  4. [SOLVED] Writing Excel data to 2 separate text files
    By new2vb in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-06-2011, 12:51 PM
  5. [SOLVED] writing non-ascii text to a file from an Excel functio
    By maurice.roach@gmail.com in forum Excel General
    Replies: 0
    Last Post: 05-04-2006, 06:35 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1