Shg, thanks for your patience with me on this one.
Ok, I followed your suggestion and copied Chip's original code exactly. For my needs, I created a loop, and I'm calling the function 4 times for each iteration of the loop. I commented out the Application.Screenupdating=False, and the OnError statements as you suggested from your last post. When I step through the code, it seems to be grabbing the text that I want, but it's not creating the text file. Can you help me figure out what's wrong? Thanks again for your time. I'm very grateful for all of your help.
Sub exp_File_Export()
For Each Cell In Range("E6:BB6")
If Cell.Value = "Make Expression File" Then
count = count + 1
End If
Next Cell
Dim RetStr As String, Flags As Long, DoCenter As Boolean
Flags = BIF_RETURNONLYFSDIRS
Flags = Flags + BIF_NEWDIALOGSTYLE
RetStr = GetDirectory(CurDir, Flags, DoCenter, "Please select a location to store .exp files")
If RetStr <> "" Then MsgBox RetStr
Sheets("Expression_Files").Activate
Set Wsht = Worksheets("Expression_Files")
For i = 5 To 6 'Change this back to "count" when stable.
Cells(6, i).Select
Set Rng = ActiveCell
Wrd = "Make Expression File"
With Wsht
Set Fnd = Rng.Find(Wrd, LookAt:=xlPart, MatchCase:=True)
If Not Fnd Is Nothing Then
For j = 8 To 23
Range(Cells(8, i), Cells(23, i)).Select
ExportToTextFile FName:="C:\Test1.txt", Sep:=" ", _
SelectionOnly:=True, AppendData:=True
Next j
For j = 28 To 70
Range(Cells(28, i), Cells(70, i)).Select
ExportToTextFile FName:="C:\Test2.txt", Sep:=" ", _
SelectionOnly:=True, AppendData:=True
Next j
For j = 75 To 101
Range(Cells(75, i), Cells(101, i)).Select
ExportToTextFile FName:="C:\Test3.txt", Sep:=" ", _
SelectionOnly:=True, AppendData:=True
Next j
For j = 106 To 117
Range(Cells(106, i), Cells(117, i)).Select
ExportToTextFile FName:="C:\Test4.txt", Sep:=" ", _
SelectionOnly:=True, AppendData:=True
Next j
End If
End With
Next i
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).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 Sub
Regards,
-gshock
Bookmarks