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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
| Sub customers()
' Cette procédure lit un fichier ASCII CUSTOMERS.TXT
' et le place sur une feuille CUSTOMERS.
' Déclarations variables locales (déclaration explicite)
Dim LIGNE_LUE As String ' Ligne lue dans le fichier externe ASCII
Dim POSITION As Integer ' Position d'un caractére dans une chaine
Dim customerNumber As Integer ' Nom
Dim customerName As String ' Prénom
Dim contactLastName As String ' Age
Dim contactFirstName As String
Dim phone As String
Dim addressLine1 As String
Dim addressLine2 As Variant
Dim city As String
Dim state As Variant
Dim postalCode As Variant
Dim country As String
Dim salesRepEmployeeNumber As Variant
Dim creditLimit As Double
' Sélection feuille GARCONS
Sheets("CUSTOMERS").Select
' Suppression du contenu éventuel de la feuille CUSTOMERS
Cells.Select
Selection.Delete
' Positionnnement des titres des colonnes
Range("A1").Select
ActiveCell.Value = "customerNumber"
Range("B1").Select
ActiveCell.Value = "customerName"
Range("C1").Select
ActiveCell.Value = "contactLastName"
Range("D1").Select
ActiveCell.Value = "contactFirstName"
Range("E1").Select
ActiveCell.Value = "phone"
Range("F1").Select
ActiveCell.Value = "addressLine1"
Range("G1").Select
ActiveCell.Value = "addressLine2"
Range("H1").Select
ActiveCell.Value = "city"
Range("I1").Select
ActiveCell.Value = "state"
Range("J1").Select
ActiveCell.Value = "postalCode"
Range("K1").Select
ActiveCell.Value = "country"
Range("L1").Select
ActiveCell.Value = "salesRepEmployeeNumber"
Range("M1").Select
ActiveCell.Value = "creditLimit"
Range("A2").Select
' Ouverture du fichier ASCII CUSTOMERS.TXT en lecture
Open "C:\CUSTOMERS.TXT" For Input As #1
' Lecture séquentielle fichier ASCII externe
Range("A2").Select ' Sélection cellule A2
Do While Not EOF(1) ' Continue jusqu'à la fin du fichier
' Lecture d'une ligne de données
Line Input #1, LIGNE_LUE
' MsgBox ("LIGNE LUE : " + LIGNE_LUE)
' Repérage du customerNumber
POSITION = InStr(LIGNE_LUE, ";")
customerNumber = Left(LIGNE_LUE, POSITION - 1)
' MsgBox ("customerNumber : " + customerNumber)
LIGNE_LUE = Mid(LIGNE_LUE, POSITION + 2)
' Repérage du customerName
POSITION = InStr(LIGNE_LUE, ";")
customerName = Left(LIGNE_LUE, POSITION - 2)
' MsgBox ("customerName : " + customerName)
LIGNE_LUE = Mid(LIGNE_LUE, POSITION + 2)
' Repérage de contactLastName
POSITION = InStr(LIGNE_LUE, ";")
contactLastName = Left(LIGNE_LUE, POSITION - 2)
' MsgBox ("contactLastName : " + contactLastName)
LIGNE_LUE = Mid(LIGNE_LUE, POSITION + 2)
' Repérage de contactFirstName
POSITION = InStr(LIGNE_LUE, ";")
contactFirstName = Left(LIGNE_LUE, POSITION - 2)
' MsgBox ("contactFirstName : " + contactFirstName)
LIGNE_LUE = Mid(LIGNE_LUE, POSITION + 2)
' Repérage de l'indicateur phone
POSITION = InStr(LIGNE_LUE, ";")
phone = Left(LIGNE_LUE, POSITION - 2)
' MsgBox ("phone : " + phone)
LIGNE_LUE = Mid(LIGNE_LUE, POSITION + 2)
' Repérage de l'indicateur addressLine1
POSITION = InStr(LIGNE_LUE, ";")
addressLine1 = Left(LIGNE_LUE, POSITION - 2)
' MsgBox ("addressLine1 : " + addressLine1)
LIGNE_LUE = Mid(LIGNE_LUE, POSITION + 1)
' Repérage de l'indicateur addressLine2
POSITION = InStr(LIGNE_LUE, ";")
addressLine2 = Left(LIGNE_LUE, POSITION - 1)
' MsgBox ("addressLine2 : " + addressLine2)
LIGNE_LUE = Mid(LIGNE_LUE, POSITION + 2)
' Repérage de l'indicateur city
POSITION = InStr(LIGNE_LUE, ";")
city = Left(LIGNE_LUE, POSITION - 2)
' MsgBox ("city : " + city)
LIGNE_LUE = Mid(LIGNE_LUE, POSITION + 1)
' Repérage de l'indicateur state
POSITION = InStr(LIGNE_LUE, ";")
state = Left(LIGNE_LUE, POSITION - 1)
' MsgBox ("state : " + state)
LIGNE_LUE = Mid(LIGNE_LUE, POSITION + 1)
' Repérage de l'indicateur postalCode
POSITION = InStr(LIGNE_LUE, ";")
postalCode = Left(LIGNE_LUE, POSITION - 1)
' MsgBox ("postalCode : " + postalCode)
LIGNE_LUE = Mid(LIGNE_LUE, POSITION + 2)
' Repérage de l'indicateur country
POSITION = InStr(LIGNE_LUE, ";")
country = Left(LIGNE_LUE, POSITION - 2)
' MsgBox ("country : " + country)
LIGNE_LUE = Mid(LIGNE_LUE, POSITION + 1)
' Repérage de l'indicateur salesRepEmployeeNumber
POSITION = InStr(LIGNE_LUE, ";")
salesRepEmployeeNumber = Left(LIGNE_LUE, POSITION - 1)
' MsgBox ("salesRepEmployeeNumber : " + salesRepEmployeeNumber)
LIGNE_LUE = Mid(LIGNE_LUE, POSITION + 1)
' Repérage de l'indicateur creditLimit
creditLimit = LIGNE_LUE
' MsgBox ("creditLimit : " + creditLimit)
' Report des informations sur la feuille sélectionnée
ActiveCell.Value = customerNumber
ActiveCell.Offset(0, 1).Select ' Déplacement latéral vers la droite
ActiveCell.Value = customerName
ActiveCell.Offset(0, 1).Select ' Déplacement latéral vers la droite
ActiveCell.Value = contactLastName
ActiveCell.Offset(0, 1).Select ' Déplacement latéral vers la droite
ActiveCell.Value = contactFirstName
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = phone
ActiveCell.Offset(0, 1).Select ' Déplacement latéral vers la droite
ActiveCell.Value = addressLine1
ActiveCell.Offset(0, 1).Select ' Déplacement latéral vers la droite
ActiveCell.Value = addressLine2
ActiveCell.Offset(0, 1).Select ' Déplacement latéral vers la droite
ActiveCell.Value = city
ActiveCell.Offset(0, 1).Select ' Déplacement latéral vers la droite
ActiveCell.Value = state
ActiveCell.Offset(0, 1).Select ' Déplacement latéral vers la droite
ActiveCell.Value = postalCode
ActiveCell.Offset(0, 1).Select ' Déplacement latéral vers la droite
ActiveCell.Value = country
ActiveCell.Offset(0, 1).Select ' Déplacement latéral vers la droite
ActiveCell.Value = salesRepEmployeeNumber
ActiveCell.Offset(0, 1).Select ' Déplacement latéral vers la droite
ActiveCell.Value = creditLimit
ActiveCell.Offset(1, -12).Select ' Déplacement latéral vers la droite
Loop
' Fin du traitement
Close #1 ' Fermeture du fichier ASCII externe
MsgBox ("Lecture du fichier ASCII terminée")
' Sélection cellule A1 dans la feuille CUSTOMERS
Sheets("CUSTOMERS").Select
Range("A1").Select
End Sub |