1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| Private Sub ChargerLesDonnees(nomCompletFichierSource As String, celCible As Range)
Dim buf As String 'buffer
Dim n°F As Integer 'numéro du fichier
Dim tbL As Variant 'tableau de lignes
Dim tbV As Variant 'tableau de valeurs
Dim tbR As Variant 'tableau résultat
Dim nbC As Long 'nombre de colonnes
Dim ixL As Long 'index ligne
Dim ixC As Long 'index colonne
' Chargement des données contenues dans le fichier source
n°F = FreeFile
Open nomCompletFichierSource For Binary Access Read As #n°F
buf = Space$(LOF(n°F))
Get #n°F, , buf
Close #n°F
' Traitement des données
If Mid(buf, Len(buf) - 1) = vbCrLf Then buf = Left(buf, Len(buf) - 2)
If Right(nomCompletFichierSource, 4) = ".csv" Then buf = Replace(Replace(buf, """", ""), ";", " ")
buf = Replace(buf, ",", ".")
tbL = Split(buf, vbCrLf)
' Création du résultat
nbC = UBound(Split(tbL(0), " "))
ReDim tbR(1 To UBound(tbL) + 1, 1 To nbC + 1)
For ixL = 0 To UBound(tbL)
tbV = Split(tbL(ixL), " ")
For ixC = 0 To nbC
tbR(ixL + 1, ixC + 1) = tbV(ixC)
Next ixC
Next ixL
' Écriture dans le fichier cible
celCible.Resize(UBound(tbR), UBound(tbR, 2)).Value = tbR
End Sub |
Partager