On a recent project I need to read from a file that contains around 5K lines.
Each line has different fields and I need to collect some of them into specific cells in excel. Example of one line:
data1;data2;data3; ..... ;dataN\\r
N
can be as large as 600.
Currently I'm opening the file and looping through each line, splitting by ";" and saving.
What I need is an example:
avarSplit = Split(sBuf, ";")
Range("C" & i).Value = CDbl(avarSplit(meter))
This tends to be extremely slow and I already have,
Application.Calculation = xlCalculationManual
I'd recommend asking VBA to open the file as a delimited text file (the equivalent of File->Open, select , then choosing 'delimited' and ';' in the open text file wizard). If you need to see how to do that in VBA, record a macro, then open it manually. Then you have your split values in a temporary workbook, and can copy them into your worksheet as before.
5k lines, even with 600 columns shouldn't take that long. It's likely that the hold up is writing the values to the cells. It would be better to fill a two-dim output array and write the values to the spreadsheet all in one go. Here's an example.
Sub ReadTextFile()
Dim sFile As String
Dim lFile As Long
Dim vaLines As Variant, vaSplit As Variant
Dim aOutput() As String
Dim i As Long
Dim sInput As String
sFile = MyDocs & "Book4.csv"
lFile = FreeFile
'Read in data
Open sFile For Input As lFile
sInput = Input$(LOF(lFile), lFile)
'Split on new line
vaLines = Split(sInput, vbNewLine)
'Set up output array - split is zero based
'but you want 1-based to write to cells
ReDim aOutput(1 To UBound(vaLines, 1) + 1, 1 To 1)
'loop through lines
For i = LBound(vaLines) To UBound(vaLines)
If Len(vaLines(i)) > 0 Then
'split on delimiter
vaSplit = Split(vaLines(i), ";")
'store want you want in the output array
aOutput(i + 1, 1) = vaSplit(15)
End If
Next i
'write the output array to a range
Sheet1.Range("A2").Resize(UBound(aOutput, 1), UBound(aOutput, 2)).Value = aOutput
End Sub
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.