Results 1 to 10 of 10

Troubleshooting code for export to text file.

Threaded View

  1. #1
    Forum Contributor
    Join Date
    03-04-2008
    Location
    Ohio
    MS-Off Ver
    Office 2010
    Posts
    208

    Troubleshooting code for export to text file.

    I was wondering if someone would be willing to help me troubleshoot this code. I got the code from here: http://www.cpearson.com/excel/ImpText.aspx

    I'm sure that it works fine. But I was trying to modify it to fit my needs. I can step through it fine, but it doesn't generate the text file for the cells I'm selecting. At least, I think I'm selecting them. I might have goofed up the cell ranges.

    I want to select 15 rows in a single column. So each cell will go on it's own line in a text file, and I don't want any character separation. In the macro that calls the function(ExportToTextFile), I changed
    Sep:=";",
    to this
    Sep:="",
    So here's the full code that I'm trying to debug. I have a separate macro that calls this function:

    Function 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
    
            'Range(Cells(2, 1), Cells(17, 1)).Copy
            Application.ScreenUpdating = False
            On Error GoTo EndMacro:
            FNum = FreeFile
    
    
            If SelectionOnly = True Then
            With Selection
            StartRow = .Cells(8).Row
            StartCol = .Cells(5).Column
            EndRow = .Cells(23).Row
            EndCol = .Cells(5).Column
            End With
            End If
    
            If AppendData = True Then
            Open FName For Append 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).Text
            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 Function
    It appears that nothing happens when I get to this line of
    Open FName For Append Access Write As #FNum

    If anyone can take the time to explain to me what I'm doing wrong, I would really appreciate it.

    Thanks,
    -gshock
    Last edited by gshock; 11-03-2008 at 07:46 PM. Reason: Solved - Thanks to shg!!! Kudos

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