Results 1 to 30 of 30

Exporting files to text files

Threaded View

  1. #14
    Registered User
    Join Date
    08-02-2012
    Location
    Dordrecht, Netherlands
    MS-Off Ver
    Excel 2010
    Posts
    31

    Re: Exporting files to text files

    Mr Shorty.

    Thanks the first part is done: See the code below, he even saves the file as a .DAT extention which i need.

    Sub DoTheExport()
        Dim FileName As Variant
        Dim Sep As String
        
        FileName = ThisWorkbook.Path & Application.PathSeparator & Worksheets("OFFPIPE").Cells(3, 21) & ".DAT" 'Application.GetSaveAsFilename(InitialFileName:=vbNullString, FileFilter:="Text Files (*.txt),*.txt")
        If FileName = False Then
            ''''''''''''''''''''''''''
            ' user cancelled, get out
            ''''''''''''''''''''''''''
            Exit Sub
        End If
        Sep = " " 'Application.InputBox("Enter a separator character.", Type:=2)
        If Sep = vbNullString Then
            ''''''''''''''''''''''''''
            ' user cancelled, get out
            ''''''''''''''''''''''''''
            Exit Sub
        End If
        Debug.Print "FileName: " & FileName, "Separator: " & Sep
        ExportToTextFile FName:=CStr(FileName), Sep:=CStr(Sep), _
           SelectionOnly:=False, AppendData:=False
    
    End Sub
    
    Public Sub ExportToTextFile(FName As String, _
        Sep As String, SelectionOnly As Boolean, _
        AppendData As Boolean)
    
    Dim WholeLine As String
    Dim FNum As Integer
    Dim RowNdx As Long
    Dim ColNdx As Integer
    Dim StartRow As Long
    Dim EndRow As Long
    Dim StartCol As Integer
    Dim EndCol As Integer
    Dim CellValue As String
    
    
    Application.ScreenUpdating = False
    On Error GoTo EndMacro:
    FNum = FreeFile
    
    If SelectionOnly = True Then
        With Selection
            StartRow = .Cells(1).Row
            StartCol = .Cells(1).Column
            EndRow = .Cells(.Cells.Count).Row
            EndCol = .Cells(.Cells.Count).Column
        End With
    Else
        With ActiveSheet.UsedRange
            StartRow = .Cells(1).Row
            StartCol = .Cells(1).Column
            EndRow = .Cells(.Cells.Count).Row
            EndCol = .Cells(.Cells.Count).Column
        End With
    End If
    
    If AppendData = True Then
        Open FName For Append Access Write As #FNum
    Else
        Open FName For Output Access Write As #FNum
    End If
    
    For RowNdx = StartRow To EndRow
        WholeLine = ""
        For ColNdx = StartCol To EndCol
            If Cells(RowNdx, ColNdx).Value = "" Then
                CellValue = Chr(34) & Chr(34)
            Else
               CellValue = Cells(RowNdx, ColNdx).Value
            End If
            WholeLine = WholeLine & CellValue & Sep
        Next ColNdx
        WholeLine = Left(WholeLine, Len(WholeLine) - Len(Sep))
        Print #FNum, WholeLine
    Next RowNdx
    
    EndMacro:
    On Error GoTo 0
    Application.ScreenUpdating = True
    Close #FNum
    
    End Sub
    but about the 2nd and 3th question, see both textfile attachments. The first one is what i export. The other one is how it should be.
    I know that all the ""sign are empty cells but i can't figure out to use the ''selectrange'' code you tried to explain.

    Another thing, when i was trying to understand the code i just trialed/errored to learn. When i fill in:

    Else
        With ActiveSheet.UsedRange
            StartRow = .Cells(3).Row
            StartCol = .Cells([B]6[B]).Column
            EndRow = .Cells([B]104[B]).Row
            EndCol = .Cells([B]6[B]).Column
        End With
    I expect than also range R3C6 to R104C6 but only row 3 is exported :S?

    And I also created a link between the button ''generate'' to do the export.

    So step by step i'm getting there
    Thanks again.
    Attached Files Attached Files
    Last edited by Cutter; 08-16-2012 at 08:23 PM. Reason: Added code tags

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