Consulting

Results 1 to 7 of 7

Thread: Excel sheet to textfile

  1. #1

    Excel sheet to textfile

    Hi,

    I would like to export an sheet in excel into a text file.

    This is my code:

    Sub text()
    Dim myFile As String, rng As Range, cellValue As Variant, i As Integer, j As Integer
    myFile = "D:\test.txt"
    Set rng = Selection
    Open myFile For Output As #1
    For i = 1 To rng.Rows.Count
        For j = 3 To rng.Columns.Count
        cellValue = rng.Cells(i, j).Value
        If j = rng.Columns.Count Then
        Write #1, cellValue
    Else
        Write #1, cellValue,
    End If
    Next j
    Next i
    Close #1
    End Sub
    Problem: I need changes in the code so that

    1. no selection is nessesery - just take the open sheet, take all the rows and lines with a content ( if no content just go to the next )
    2. start in line 1 row 3
    3. never export rows 5 to 6 no matter if empty or not
    4. write all in a textfile looking like in excel

    THX a lot!
    Last edited by Aussiebear; 10-10-2015 at 02:40 PM. Reason: added code tags

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    2. start in line 1 row 3

    4. write all in a textfile looking like in excel
    What does line 1 row 3 mean?

    What does looking like in Excel mean?
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    Sorry "row" was supposed to be column

    Looking like excel: lines and columns in textfile the same like in excel.
    Empty lines in excel are supposed to be ignored, so no empry lines in textfile.

    THX

  4. #4
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    You can use .Saveas

  5. #5
    I need VBA code.

    Dim myFile As String, rng As Range, cellValue As Variant, i As Integer, j As Integer
    myFile = "C:\test.txt"
    Set rng = Selection
    Open myFile For Output As #1
    For i = 1 To rng.Rows.Count
        For j = 1 To rng.Columns.Count
        cellValue = rng.Cells(i, j).Value
        If j = rng.Columns.Count Then
        Write #1, cellValue
    Else
        Write #1, cellValue,
    End If
    Next j
    Next i
    Close #1
    End Sub
    Does anyone knows how to change this code, so that the selection is not needed?

    Instead of Selection the code should start in line 1, column 4 go through each cell of line one until the column is empty and then go to the next line and do the sam, all until there is a first cell in a line with value "STOP"

  6. #6

  7. #7
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    With code

    1. Delete empty rows
    2. Delete Rows 5 & 6
    3. Delete Column A & B
    4. SaveAs text file.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

Posting Permissions

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