You made me code in the MSDN VBA Forum to select files using filepicker then pulled the record into spreadsheet. The program works GREAT!!!! but i have discovered a small quirk. All the files are not the same dataset in the record so coolum A might be Foo then the next time Bug. Information is the same as long as the file size is the same. How do I fix this. This is not the whole thing but hopefully enough for you to help. I can provide more if needed. What happened to VBA Fourms ? the new format is really bad.
Code: Select all
With Application.FileDialog(msoFileDialogFilePicker)
.Filters.clear
.Filters.Add "Text Files", "*.txt"
.Title = "Select Required Files"
.AllowMultiSelect = True
.InitialFileName = FolderChooser & "*_chr.*"
If .Show Then
Set arrFiles = .SelectedItems
Else
MsgBox "No File Was Selected.", vbExclamation
Exit Sub
End If
End With
f = FreeFile
'//////////////////////////////////////////
'Get the Header Information
'//////////////////////////////////////////
Open arrFiles.Item(1) For Input As #f ' Open 1st _chr file and get Header Information to put on Wrksht
Line Input #f, strLine ' skip past the first line of the file
c = c + 1 'set row to cnt to position cell
Do While Not EOF(f)
Line Input #f, strLine
If strLine = "END" Then
Exit Do
End If
arrParts = Split(strLine, vbTab)
strPartA = arrParts(2) ' 3rd part
strPartC = arrParts(6) 'Nominal
strPartD = arrParts(7) 'High Tol
strPartE = arrParts(8) ' Lower Tol
Sheet1.Cells(r, c) = strPartA
Sheet1.Cells(r + 1, c) = strPartC
Sheet1.Cells(r + 2, c) = Left(strPartE, 6) & "/" & Left(strPartD, 6)
Range(Cells(r, c), Cells(r + 2, c)).Borders.LineStyle = xlContinuous
c = c + 1
Loop 'loop thru to get all headers
Close #f
\\\\\\\\\
For Each varFile In arrFiles
'reposition the active cell
r = r + 1
'//////////////////////////
'Change from _chr file to _hdr file to get HDR infomation
'//////////////////////////
'open _hdr file
Open Replace(varFile, "_chr.txt", "_hdr.txt") For Input As #f
'Read _hdr file
Line Input #f, strLine 'skip 1st record
Line Input #f, strLine
arrParts = Split(strLine, vbTab)
HDR = arrParts(38) ' 38st part of HDR file to save in variable HDR
DDR = arrParts(4) ' get the date
TDR = arrParts(6)
Close #f
'open _chr file
Open varFile For Input As #f
Line Input #f, strLine 'skip 1st record
'add the HDR information
c = 1
Sheet1.Cells(r, c) = DDR & " " & TDR
Sheet1.Cells(r, c + 1) = HDR
'Sheet1.Cells(r, c + xlendleft + 1) = Right(HDR, 2)
'Line Input #f, strLine 'skip 1st record
' ...
' ...
' Loop through the rest
'r = 2
c = 2
'cnt = 1
Do While Not EOF(f)
' Read a line
Line Input #f, strLine
' Get out if we reached END
If strLine = "END" Then
Sheet1.Cells(r, c + xlendleft + 1) = Right(HDR, 2)
c = 3
'r = r + 1
Exit Do
End If
' Move to next column
c = c + 1
' Enter filename in column A
'Sheet1.Cells(r, c) = strFilename
' Split line
arrParts = Split(strLine, vbTab)
'strPartA = arrParts(2) ' 3rd part
strPartB = arrParts(5) ' 6th part
strPartA = arrParts(10)
If strPartA <> "" Then
Sheet1.Cells(r, c).Interior.Color = vbYellow
End If
' Do something with these parts
'Sheet1.Cells(r, c) = strPartA
Sheet1.Cells(r, c) = strPartB
'...
Loop
Close #f