Consulting

Results 1 to 3 of 3

Thread: Solved: Saving output as a text file with pre-fixed space between columns

  1. #1
    VBAX Contributor
    Joined
    Feb 2007
    Posts
    126
    Location

    Solved: Saving output as a text file with pre-fixed space between columns

    Hi All. Once again thanks a lot in advance for your great help!

    I am using this code provided by Matt here to save my worksheet into a txt file.

    I am trying to add a SaveAs Dialogue, so that user can choose where to save file.

    This is how I save to file

    [vba]
    Sub itipuexporttext()
    Dim CellData() As Variant, vFF As Long, vFile As String, i As Long
    vFile = "C:\exported.txt" 'what you want your exported file to be called
    CellData = Intersect(Columns("A:B"), ActiveSheet.UsedRange).Value
    vFF = FreeFile
    Open vFile For Output As #vFF
    For i = 1 To UBound(CellData, 1)
    Print #vFF, CellData(i, 1) & " " & CellData(i, 2) '6 spaces in between
    Next
    Close #vFF
    End Sub
    [/vba]

    This is how I think one can invoke the SaveAs Dialogue, but I don't seem to be able to combine these two codes....

    [vba]
    Dim filex As Variant

    filex = Application.GetSaveAsFileName(filex = Application.GetSaveAsFilename(FileFilter:="Text Files (*.txt), *.txt")
    If filex <> False Then
    MsgBox "File Saved as " filex
    End If
    [/vba]


    Again Thanks a lot to you All. Great forum great help!
    Last edited by itipu; 06-16-2007 at 11:18 AM.

  2. #2
    Hi

    I think this should do what you want

    Sub itipuexporttext()
    Dim CellData() As Variant, vFF As Long, vFile As String, i As Long, vFname As String

    vFname = "Exported" 'initial file name

    vFile = Application.GetSaveAsFilename(vFname, fileFilter:="Text Files (*.txt), *.txt") 'what you want your exported file to be called

    CellData = Intersect(Columns("A:B"), ActiveSheet.UsedRange).Value

    vFF = FreeFile
    Open vFile For Output As #vFF
    For i = 1 To UBound(CellData, 1)
    Print #vFF, CellData(i, 1); Spc(6); CellData(i, 2) '6 spaces in between
    Next
    Close #vFF

    End Sub

    all the best
    qff

  3. #3
    VBAX Contributor
    Joined
    Feb 2007
    Posts
    126
    Location

    Cheers m8

    Thats exactly it! Much obliged!

Posting Permissions

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