Consulting

Results 1 to 4 of 4

Thread: export to txt file

  1. #1
    VBAX Newbie
    Joined
    Apr 2015
    Posts
    3
    Location

    Question export to txt file

    Hi all, could you be so kind as to help me to solve problem with my code, please? I need this code to export two columns from excel into one column to textfile.
    Untitled.jpg

    Problem to be solved: My code exports this entries as string in format "000000". I need to change it to export it in format without "" and first column with 8 digits and second with 6 digits. Both these columns have to be under each other in one column in textfile.

    My code is:
    Sub variant2()


    Dim myFile As String, rng1 As Range, rng2 As Range, cellValue As Variant, i As Integer, j As Integer
    myFile = Application.DefaultFilePath & "\test.txt"
    Set rng = Range("C35000")
    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

    Thank you in advance!

  2. #2
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    Sub M_snb()
      CreateObject("scripting.filesystemobject").createtextfile("G:\OF\example.txt").write Join([transpose(text(A2:A200,"00000000")&char(10)&text(B2:B200,"000000"))], vbLf)
    End Sub

  3. #3
    VBAX Expert
    Joined
    Oct 2012
    Posts
    726
    Location
    Sub test()
        Export ActiveSheet, "a1:b5", "C:\test.txt"
    End Sub
    
    Sub Export(sht As Worksheet, rng As String, file As String)
        Set tf = CreateObject("Scripting.FileSystemObject").CreateTextFile(file, True)
        Set r = sht.Range(rng)
        For Each c In r
            tf.Writeline c.Text
        Next
        tf.Close
    End Sub

  4. #4
    VBAX Newbie
    Joined
    Apr 2015
    Posts
    3
    Location
    Hi Jonh, it works excellent !! Many Thanks.

Posting Permissions

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