J'ai du mal à accélérer le traitement d'un très gros fichier texte (~ 100 Meg ou plus). J'ai fait attention à être très diligent en utilisant les appels redim preserve, et pourtant la fonction prend encore 5 minutes ou plus pour fonctionner. Le fichier texte est essentiellement des sous-rapports que j'essaie d'analyser. J'ai seulement accès au gros fichier. Qu'est-ce qu'une personne à faire. VBA est-il si lent? Voici le code, l'objet "Report" est une classe que j'ai créée. La plupart des rapports sont juste quelques centaines de lignes, donc ce pourquoi je choisis 1000 pour le ubound:Traiter de gros fichiers texte rapidement avec VBA
Public Function GetPages(originalFilePath As String) As Collection
Dim myReport As report
Dim reportPageCollection As Collection
Dim startLine As Long
Dim endLine As Long
Dim fso As FileSystemObject
Dim file As textStream
Dim lineStr As String
Dim index As Long
Dim lines() As String
Set fso = New FileSystemObject
Set reportPageCollection = New Collection 'initialize the collection
Set file = fso.OpenTextFile(originalFilePath, ForReading)
ReDim lines(0 To 1000)
lineStr = file.ReadLine 'skip the first line so the loop doesnt add a blank report
lines(0) = lineStr
index = 1
Do Until file.AtEndOfLine 'loop through from the startline to find the end line
lineStr = file.ReadLine
If lineStr Like "1JOBNAME:*" Then 'next report, so we want to return an array of the single line
'load this page into our report page collection for further processing
Set myReport = New report
myReport.setDataLines = lines() 'Fill in 'ReportPage' Array
reportPageCollection.Add myReport 'add our report to the collection
'set up array for new report
ReDim lines(0 To 1000)
index = 0
lines(index) = lineStr
index = index + 1
Else
'============================ store into array
If index = UBound(lines) Then
ReDim Preserve lines(0 To UBound(lines) + 1000)
lines(index) = lineStr
index = index + 1
Else
lines(index) = lineStr
index = index + 1
End If
'============================
End If
Loop
file.Close
Set fso = Nothing
Set GetPages = reportPageCollection
End Function
Toute aide est appréciée. Merci!
le fichier est sorti sur un lecteur réseau, mais j'ai omis de mentionner que je le copie sur le lecteur local avant de commencer à le lire. Je vais essayer. Merci! – Fink
a bien fonctionné! Pris <10 sec pour un fichier de 150 mégaoctets. Je ne sais pas trop pourquoi c'était si lent avant. Maintenant, pour ajuster les autres fonctions. – Fink