Re: output to txt file
Marc,
Code
Option Explicit
Private rngXLOrders As Range 'Range cobvering all order numbers in XL
Private rngOrderFound As Range 'Range returned from the find function
Private rng As Range 'Temporary range object if required
'Change these to reflect your files
Private Const _
strArchiveFilename As String = "D:\Answers\27325\archive.txt", _
strOutputFilename As String = "D:\Answers\27325\output-orders.txt"
Private intFileIn As Integer 'File number for input file
Private intFileOut As Integer 'File number for output file
Private strIn As String 'String read from input file and output if reqd
Private strOut As String 'String that will be written out
Private bolExportOrder As Boolean
Private bolOrderinExcel As Boolean
Private bolExportGroupTotals As Boolean
'The following long numbers are used to track the order number and the number of
'orders read and the number of line read.
Private lngOrder As Long, _
lngLastOrder As Long, _
lngOrdersFound As Long, _
lngOrdersNotFound As Long, _
lngLinesRead As Long, _
lngOrdersInGroup As Long
Public Sub CheckArchive()
'Set range of order numbers and clear worksheet of previous run data
Set rngXLOrders = Worksheets("Order_Nmbrs2").Range("A2", Range("A65536").End(xlUp))
Worksheets("Order_Nmbrs2").Range(Range("B2"), _
Range("B2").Offset(rngXLOrders.Rows.Count - 1)).ClearContents
' On Error GoTo DiskError
'Open input and output files
intFileIn = FreeFile()
Open strArchiveFilename For Input As #intFileIn
intFileOut = FreeFile()
Open strOutputFilename For Output As #intFileOut
'Initialise some counters
lngLastOrder = -1
lngOrder = -1
lngLinesRead = 0
lngOrdersFound = 0
lngOrdersNotFound = 0
bolExportOrder = False
bolOrderinExcel = False
'Read the header line
If Not EOF(intFileIn) Then Line Input #intFileIn, strIn
'Start a loop to read the file until the end
Do While Not EOF(intFileIn)
lngLastOrder = lngOrder
Line Input #intFileIn, strIn
lngLinesRead = lngLinesRead + 1
lngOrder = Val(Mid(Trim(strIn), 5, 4))
If lngOrder = lngLastOrder Then 'Order in current group
lngOrdersInGroup = lngOrdersInGroup + 1
If bolExportOrder Then
Print #intFileOut, strIn
End If
Else 'Order is start of a new group
lngOrdersInGroup = 1
CheckOrderExportStatus
If bolExportOrder Then
lngOrdersFound = lngOrdersFound + 1
Print #intFileOut, strIn
rngOrderFound.Offset(0, 1) = "Exported"
End If
End If
Loop
'Close files
Close #intFileIn
Close #intFileOut
MsgBox Format(lngLinesRead, "#,##0") & " Lines read" & vbNewLine & _
Format(lngOrdersFound, "#,##0") & " Orders found" & vbNewLine & _
Format(lngOrdersNotFound, "#,##0") & " Orders not found", vbOKOnly
On Error GoTo 0
Exit Sub
DiskError:
MsgBox Err.Description, vbCritical + vbOKOnly, "Error in accessing files"
End Sub
Private Sub CheckOrderExportStatus()
Set rngOrderFound = rngXLOrders.Find(lngOrder, , , xlWhole, xlByRows)
If (rngOrderFound Is Nothing) Then
'The archived order number is not in the excel file
lngOrdersNotFound = lngOrdersNotFound + 1
bolExportOrder = False
bolOrderinExcel = False
Else
'The archived order number is in the excel file
bolOrderinExcel = True
'Check to see if it has been exported, if not do it, if it has move on!
bolExportOrder = (rngOrderFound.Offset(0, 1) <> "Exported")
End If
End Sub
Display More
This now exports all lines from the first group containing an order number that appears in the Excel file.
Alan.