Consulting

Results 1 to 5 of 5

Thread: Solved: Writing data from workbook to text file

  1. #1
    VBAX Regular
    Joined
    Aug 2011
    Posts
    18
    Location

    Solved: Writing data from workbook to text file

    Hi,
    I have an excel sheet with one colum and many rows that filled with long deciaml numbers(positive and negetive).
    I need to write all these numbers into a txt file with the following terms:
    1. the decimal numbers must be shorten to 6 digits after the point.
    2. the numbers must come one after the other with a ";" between them
    not in lines as thay appear in the workbook.

    in the following code i used round function to define the numbers of decimal places but the problom that it drops sometimes the first digit in the number,
    for example "0.123456789" i see as ".123456".
    i also dont know how to combine them into one line with a sepeprator.

    [VBA][/VBA][VBA][/VBA][VBA][/VBA][VBA][/VBA][VBA]Public Sub save_to_txt()
    Close #1
    Open "D:\WRITE_TEST.txt" For Output As #1
    Dim RowIndex As Integer
    Dim cellsvalue As Double
    RowIndex = 1

    While IsEmpty(Cells(RowIndex, 3)) = 0
    cellsvalue = Round(Cells(RowIndex, 3), 6)
    Write #1, cellsvalue
    RowIndex = RowIndex + 1
    Wend
    Close #1
    End Sub[/VBA]

  2. #2
    VBAX Expert
    Joined
    Feb 2010
    Posts
    696
    Location
    The following works for me if all you want is a single string with comma-separated values. You will need to change the data range to suit your needs.

    [VBA]
    Sub save_to_txt()

    Dim dataRng As Range, i As Long, output As String

    'Change the range to suit your needs
    Set rng = Sheets(1).Range("A1:A10")

    Close #1

    Open "DWRITE_TEST.txt" For Output As #1

    For i = 1 To rng.Count

    output = output & Round(rng(i).Value, 6) & ", "

    Next

    output = Left(output, Len(output) - 2)

    Write #1, output

    Close #1

    End Sub
    [/VBA]
    Last edited by Opv; 05-26-2012 at 06:22 PM.

  3. #3
    VBAX Expert
    Joined
    Feb 2010
    Posts
    696
    Location
    Here's the corrected code. I noticed that I left an error in the path statement when I changed the path from the test path I was using back to your path. Sorry about that.

    [VBA]
    Sub save_to_txt()

    Dim dataRng As Range, i As Long, output As String

    'Change the range to suit your needs
    Set rng = Sheets(1).Range("A1:A10")

    Close #1

    Open "D:\WRITE_TEST.txt" For Output As #1

    For i = 1 To rng.Count

    output = output & Round(rng(i).Value, 6) & ", "

    Next

    output = Left(output, Len(output) - 2)

    Write #1, output

    Close #1

    End Sub


    [/VBA]
    Last edited by Opv; 05-26-2012 at 07:32 PM.

  4. #4
    VBAX Expert shrivallabha's Avatar
    Joined
    Jan 2010
    Location
    Mumbai
    Posts
    750
    Location
    Try:
    [VBA]Print #1, cellsvalue[/VBA]
    instead of:
    [VBA]Write #1, cellsvalue[/VBA]
    for the code posted at #1.
    Regards,
    --------------------------------------------------------------------------------------------------------
    Shrivallabha
    --------------------------------------------------------------------------------------------------------
    Using Excel 2016 in Home / 2010 in Office
    --------------------------------------------------------------------------------------------------------

  5. #5
    VBAX Regular
    Joined
    Aug 2011
    Posts
    18
    Location

    cheers

    thanks for the help.

Posting Permissions

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