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
to this
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
Bookmarks