+ Reply to Thread
Results 1 to 4 of 4

Export to text file

Hybrid View

  1. #1
    Registered User
    Join Date
    08-09-2011
    Location
    Netherlands
    MS-Off Ver
    Excel 2010
    Posts
    13

    Question Export to text file

    Hello,

    At the moment I have a project where I need to export columns in an Excel file into a text file. This is almost finished. The code I have now saves each row into a separate text file.

    The only thing left to do is to select which columns are exported. Not every column is needed in the text file. It needs to become something like: Column 1,3,5 need to be exported. This is where i need some help.

    Can annyone give me some pointers?

    Here is my code so far:

    
    Sub exporttxt()
    
    Dim firstrow As Integer
    Dim rownum As Integer
    Dim colnum As Integer
    Dim filename As String
    Dim Filelocation As String
    Dim ws As Worksheet
    
    Set ws = Worksheets("form")
    
    Filelocation = "c:\"
    
        With ws
            rownum = 3
            While .Cells(rownum, 1) <> ""
                filename = .Cells(rownum, 1).Value
                Open (Filelocation & filename & ".txt") For Output As #1
                colnum = 2
                
                While .Cells(rownum, colnum) <> 0
                     Print #1, ws.Cells(rownum, colnum) & vbNewLine;
                    colnum = colnum + 1
                    
                Wend
                Close #1
                rownum = rownum + 1
            Wend
            
        End With
    
    End Sub

    Thanks,
    Sebas123
    Last edited by Sebas123; 08-10-2011 at 07:39 AM.

  2. #2
    Valued Forum Contributor
    Join Date
    06-19-2010
    Location
    Holywell, N Wales, UK
    MS-Off Ver
    Excel 2013
    Posts
    470

    Re: Export to text file

    Hi sebas123
    Using your code structure, one way to give you an idea?

    Option Explicit
    Option Base 1
    
    Sub exporttxt2()
    
    Dim firstrow As Integer
    Dim rownum As Integer
    Dim colnum As Integer
    Dim cnt As Integer
    Dim colarr
    Dim filename As String
    Dim Filelocation As String
    Dim ws As Worksheet
    
    Set ws = Worksheets("form")
    
    'Put columns to be output here
    'In this case columns B, D, F and K
    colarr = Array(2, 4, 6, 11)
    
    Filelocation = "c:\"
    
        With ws
            rownum = 3
            While .Cells(rownum, 1) <> ""
                filename = .Cells(rownum, 1).Value
                Open (Filelocation & filename & ".txt") For Output As #1
                
                    For cnt = 1 To UBound(colarr)
                         'Debug.Print filename, ws.Cells(rownum, colarr(cnt)) & vbNewLine
                         Print #1, ws.Cells(rownum, colnum) & vbNewLine;
                    Next cnt
                    
                Close #1
                rownum = rownum + 1
            Wend
            
        End With
    
    End Sub
    I have only tested this with Debug.Print and not to file(s).
    Barry

  3. #3
    Registered User
    Join Date
    08-09-2011
    Location
    Netherlands
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: Export to text file

    Barry,

    Thx for the pointer.

    With this i can finally complete my coding.

    Thanks,
    Sebas123

  4. #4
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Export to text file

    Why don't you delete the columns you don't have to export and save the workbook as a txt file ?



+ 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