+ Reply to Thread
Results 1 to 4 of 4

need macro to create pipe delimited text file from excel

Hybrid View

  1. #1
    Registered User
    Join Date
    11-05-2011
    Location
    cebu
    MS-Off Ver
    Excel 2003
    Posts
    7

    need macro to create pipe delimited text file from excel

    hi,

    anyone can help me to create pipe delimited text file from excel via macro?

    attached is the excel source and output text file format.

    the test file will have the following format:

    |date|ID|WorkingDays|DaysPresent|AbsentHours|LateHours|PresentHours|

    |yyyy-mm-dd|ID|13|12|0.00|0.00|96.00|


    thanks,
    Attached Files Attached Files

  2. #2
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: need macro to create pipe delimited text file from excel

    You can use this code -
     
    Option Explicit
    Dim lrow As Long
    Dim lcol As Long
    Dim txtcsv As String
    Dim i As Long
    Dim j As Long
    
    Sub create_txt()
    
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    
    lrow = ThisWorkbook.Worksheets(1).Range("A" & Rows.Count).End(xlUp).Row
    lcol = ThisWorkbook.Worksheets(1).Range("IV1").End(xlToLeft).Column
    
    Workbooks.Add
    ActiveWorkbook.SaveAs "Test.txt"
    
    txtcsv = ""
    
    For i = 1 To lrow
        For j = 1 To lcol
            txtcsv = txtcsv & "|" & ThisWorkbook.Worksheets(1).Cells(i, j).Value
            If j = lcol Then
                txtcsv = vbLF & txtcsv & vbLf
            End If
        Next j
        Workbooks("Test.txt").Worksheets(1).Range("A1").Value = txtcsv
    Next i
    
    ActiveWorkbook.SaveAs Filename:="Test.txt", FileFormat:=xlText, CreateBackup:=False
    
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    
    End Sub
    Last edited by arlu1201; 02-03-2012 at 07:08 AM.
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  3. #3
    Registered User
    Join Date
    11-05-2011
    Location
    cebu
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: need macro to create pipe delimited text file from excel

    thanks,

    how do i add the row one after the other on the txt file, so far it create one row for all excel rows? my output should be:
    |yyyy-mm-dd|ID|13|12|0.00|0.00|96.00|
    |yyyy-mm-dd|ID|13|12|0.00|0.00|96.00|
    |yyyy-mm-dd|ID|13|12|0.00|0.00|96.00|

  4. #4
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: need macro to create pipe delimited text file from excel

    The vbLF is the line feed character which will copy the data to the next line. I tested it at my end and it works.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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