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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
| Public Sub LireCSV(ByVal Chemin As String, col1 As Integer, col2 As Integer, col3 As Integer, col4 As Integer, col5 As Integer)
Dim Chaine As String, Chaine2 As String
Dim Ar() As String
Dim tab1() As String
Dim i As Long, j As Long
Dim iRow As Long, iCol As Long
Dim NumFichier As Integer
Dim Separateur As String * 1
col1 = 0
col2 = 1
col3 = 2
col4 = 37
col5 = 43
Sheets("reception_donnees").Activate
Separateur = ";"
Cells.Clear
Application.ScreenUpdating = False
NumFichier = FreeFile
iRow = 0
Open Chemin For Input As #NumFichier
Do While Not EOF(NumFichier)
iCol = 1
iRow = iRow + 1
Line Input #NumFichier, Chaine
If InStr(1, Chaine, Chr(10)) > 0 Then 'si les retours ligne sont au format unix (LF seul)
'dans ce cas, la variable Chaine contient tout le fichier dès la première passe
tab1 = Split(Chaine, Chr(10))
For j = LBound(tab1) To UBound(tab1)
iCol = 1
iRow = iRow + 1
Chaine2 = tab1(j)
Ar = Split(Chaine2, Separateur)
For i = LBound(Ar) To UBound(Ar)
If (i = col1 Or i = col2 Or i = col3 Or i = col4 Or i = col5) Then
Cells(iRow, iCol) = Ar(i)
iCol = iCol + 1
End If
Next i
Next j
Else 'si les retours ligne sont au format windows (CR LF), code d'origine Ar = Split(Chaine, Separateur)
For i = LBound(Ar) To UBound(Ar)
If (i = col1 Or i = col2 Or i = col3 Or i = col4 Or i = col5) Then
Cells(iRow, iCol) = Ar(i)
iCol = iCol + 1
End If
Next i
End If
Loop
Close #NumFichier
Application.ScreenUpdating = True
MsgBox "Le fichier à bien été importé !", vbInformation, "Importation de données"
End Sub |
Partager