Bonjour le forum, bonjour le fil,
Une fois de plus je me tourne vers le forum et vous demande de l'aide SVP.
J'écris une macro pour importer des data à partir d'un fichier .csv. Ce fichier est une suite de nombre (data) mais certaines lignes sont affectées d'un "N/A" lorsque le mesure s'est mal passée.
Le but est de récupérer les data dans un tableau pour le copier sur mon classeur et ensuite générer des stats.
J'ai donc le code suivant:J'ai surligné en rouge les lignes qui renvoient l'erreur de type 13.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 Public System As String Option Explicit Sub IV_Import() Dim ws_IVData As Worksheet: Set ws_IVData = ThisWorkbook.Worksheets("IV_Data") 'déclaration variable & attribution d'un nom au classeur pour manipulation ws_IVData.Cells.Delete Shift:=xlUp 'Effacement des data Dim fichier As Variant, info() As Variant, i As Integer, lr As Integer, j As Integer Dim Batch_ID As Range, date_heure As String, c_ID As String, DataColumn As Variant Dim No_ID As Boolean: No_ID = False '----//// définition de la constante Disque, pour placement dans le répertoire Const Pasan As String = "S:blabla" Const Chroma As String = "S:ici" frmPattern.Show Dim Repertoire As String: Repertoire = IIf(System = "Pasan", Pasan, Chroma) ChDrive Repertoire ChDir Repertoire fichier = Application.GetOpenFilename(, , System & " File(s) Selection", , True) If IsArray(fichier) = False Then '---- condition pour continuer: si chemin vide (=False) MsgBox ("No File Selected") 'sortie de la boucle avec affichage d'un texte Exit Sub End If Application.ScreenUpdating = False 'non rafraichissement écran pour gain de temps For i = 1 To UBound(fichier) Workbooks.OpenText Filename:=fichier(i), StartRow:=1, DataType:=xlDelimited, TextQualifier:=xlTextQualifierDoubleQuote, Comma:=True, _ ConsecutiveDelimiter:=True, Semicolon:=True, Space:=False, Other:=False, DecimalSeparator:="," lr = Cells(Cells.Rows.Count, 1).End(xlUp).Row Select Case System Case Is = "Pasan" DataColumn = Array(8, 9, 15, 16, 10, 11, 12, 13, 14, 29, 30) Case Is = "Chroma" DataColumn = Array(15, 16, 19, 20, 12, 13, 14, 17, 18, 22, 23) Cells.Replace what:=".", Replacement:=".", LookAt:=xlPart, SearchOrder:=xlByRows End Select ReDim info(lr - 2, 17) For j = LBound(info) To UBound(info) info(j, 0) = System info(j, 1) = IIf(System = "Pasan", Cells(j + 2, 1).Value2, Left(Cells(j + 2, 6).Value2, 10)) 'date info(j, 2) = IIf(System = "Pasan", Cells(j + 2, 2).Value2, Right(Cells(j + 2, 6).Value2, 8)) 'heure info(j, 7) = IIf(IsNumeric(Cells(j + 2, DataColumn(0)).Value2 = True), Cells(j + 2, DataColumn(0)).Value2 * 1000, Cells(j + 2, DataColumn(0)).Value2) info(j, 8) = Cells(j + 2, DataColumn(1)).Value2 'Isc info(j, 9) = Cells(j + 2, DataColumn(2)).Value2 'FF 'info(j, 10) = IIf(System = "Pasan", Cells(j + 2, DataColumn(3)).Value2, Cells(j + 2, DataColumn(3)).Value2 * 100) 'Eff info(j, 11) = Cells(j + 2, DataColumn(4)).Value2 'Pmax info(j, 12) = Cells(j + 2, DataColumn(5)).Value2 'Vmax info(j, 13) = Cells(j + 2, DataColumn(6)).Value2 'Imax info(j, 14) = Cells(j + 2, DataColumn(7)).Value2 'Rs info(j, 15) = Cells(j + 2, DataColumn(8)).Value2 'Rshunt info(j, 16) = Cells(j + 2, DataColumn(9)).Value2 'Irev info(j, 17) = Cells(j + 2, DataColumn(10)).Value2 'Irev2 Next j ActiveWorkbook.Close (False) lr = Cells(ws_IVData.Cells.Rows.Count, 1).End(xlUp).Row + 1 Range(ws_IVData.Cells(lr, 1), ws_IVData.Cells(lr + UBound(info), 18)) = info() Erase info() Next i End Sub
Mon tablaeau "info()" est déclaré en tant que variant. La condition "IsNumeric" me revoie "True" lorsque que la cellule est convertible en nombre, "False" lorsque ce n'est pas le cas.
J'ai fait un essai avec VarType afin de connaitre le type de ma cellule lorsque "IsNumeric" me renvoie "False", qui est de type "String"; la condition "IIf" est donc censée me retourner le deuxième argument, mais l'erreur type 13 apparait à chaque fois que "IsNumeric" est "False", et uniquement si "IsNumeric" est "False".
Si je contourne le pb par :Je n'ai plus de problème d'incompatibilité de type et l'erreur de type 13 disparait donc.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 For j = LBound(info) To UBound(info) info(j, 0) = System info(j, 1) = IIf(System = "Pasan", Cells(j + 2, 1).Value2, Left(Cells(j + 2, 6).Value2, 10)) 'date info(j, 2) = IIf(System = "Pasan", Cells(j + 2, 2).Value2, Right(Cells(j + 2, 6).Value2, 8)) 'heure info(j, 8) = Cells(j + 2, DataColumn(1)).Value2 'Isc info(j, 9) = Cells(j + 2, DataColumn(2)).Value2 'FF info(j, 11) = Cells(j + 2, DataColumn(4)).Value2 'Pmax info(j, 12) = Cells(j + 2, DataColumn(5)).Value2 'Vmax info(j, 13) = Cells(j + 2, DataColumn(6)).Value2 'Imax info(j, 14) = Cells(j + 2, DataColumn(7)).Value2 'Rs info(j, 15) = Cells(j + 2, DataColumn(8)).Value2 'Rshunt info(j, 16) = Cells(j + 2, DataColumn(9)).Value2 'Irev info(j, 17) = Cells(j + 2, DataColumn(10)).Value2 'Irev2 Select Case System Case Is = "Pasan" info(j, 7) = Cells(j + 2, DataColumn(0)).Value2 If IsNumeric(info(j, 7)) = True Then info(j, 7) = info(j, 7) * 1000 info(j, 12) = Cells(j + 2, DataColumn(5)) If IsNumeric(info(j, 12)) = True Then info(j, 12) = info(j, 12) * 1000 Case Is = "Chroma" info(j, 7) = (Cells(j + 2, DataColumn(0)).Value2) * 1000 info(j, 12) = (Cells(j + 2, DataColumn(5)).Value2) * 1000 End Select Next j
Pas que cette solution me déplaise, mais j'aimerais tout de même comprendre pq dans le premier cas cela ne fonctionne pas.
Si je peux me permettre (dans les choses que je ne comprends pas non plus), pourquoi malgré un délimitateur de décimale clairement mentionné comme "," dansj'ai encore des points dans le cas "Chroma", ce qui oblige de faire l'opération
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3 Workbooks.OpenText Filename:=fichier(i), StartRow:=1, DataType:=xlDelimited, TextQualifier:=xlTextQualifierDoubleQuote, Comma:=True, _ ConsecutiveDelimiter:=True, Semicolon:=True, Space:=False, Other:=False, DecimalSeparator:=","seule solution que j'ai trouvée pour remplacée les points par des virgules (oui oui, j'ai bien écrit les bien par des virgules, et non pas des points par des points)!!
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2 Cells.Replace what:=".", Replacement:=".", LookAt:=xlPart, SearchOrder:=xlByRows
Merci d'avance de votre aide,
Cdt
Partager