Discussion: macro et lenteur
Afficher un message
Vieux 13/05/2008, 22h48   #15 (permalink)
Roland_M
XLDnaute Impliqué
 
Date d'inscription: avril 2007
Localisation: 59186 ANOR
Version Excel : Excel 2003 (PC)
Messages: 539
Par défaut Re : macro et lenteur

bonsoir

je viens d'essayer ce même code en créant 30 fichiers de 500 lignes chacun
ça prend à peine 1 seconde pour les créer !
et environ 1 seconde pour loader le tout !?
ceci sous Excel 2003 et Excel 2007 !
je précise que j'ai l'antivirus qui tourne et j'étais connecté sur le net(sur le forum)

donc ce n'est pas le code !? mais probablement les fichiers sources !?
ça doit boucler quelque part !?

Code:
Sub exporte()
Directory = ThisWorkbook.Path & "\" & "essai\"
For I = 1 To 30
fich$ = Directory & "fich" & I & ".txt"
Open fich$ For Output As #1
For L = 1 To 500
Print #1, "line" & L
Next
Close #1
Next
End Sub

Sub import()
Dim Directory As String, File As String, Temp As String
Dim NumRow As Long, NumCol As Integer
Dim FF As Integer, I As Integer
Dim LigFic As Long

Directory = ThisWorkbook.Path & "\" & "essai\"
File = Dir(Directory & "*.txt")
NumRow = ActiveCell.Row
NumCol = ActiveCell.Column
With ActiveSheet
FF = FreeFile
LigFic = 0
Do While File <> ""
Open Directory & File For Input As #FF
Do While Not EOF(FF)
Line Input #FF, Temp
If LigFic > 4 Then
Table = Split(Temp, vbTab)
For I = 0 To UBound(Table)
If IsDate(Table(I)) Then
.Cells(NumRow, NumCol + I) = CDate(Table(I))
Else
.Cells(NumRow, NumCol + I) = Table(I)
End If
Next
NumRow = NumRow + 1
End If
LigFic = LigFic + 1
Loop
LigFic = 0
Close #FF
File = Dir
Loop
End With
End Sub
Roland
Roland_M est connecté maintenant   Réponse avec citation