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
|
Sub ImportFeuillesXLS(pNomClasXLS As String)
'----------------------------------------------------
' paramètres:
' pNomClasXLS :nom du classeur avec chemin complet
'----------------------------------------------------
Dim xlApp As New Excel.Application
Dim xlWbk As Excel.Workbook
Dim xlWsh As Excel.Worksheet
Dim lgDerlig As Long
Dim lgDerCol As Integer
Dim F As Integer, C As Integer
Dim J As Integer, K As Integer, L As Integer
Dim strProduit As String ' Nom du produit
Dim stListeRef As String ' liste REF et tableau associé
Dim tabloRef() As String
Dim stListeValRef As String ' liste valeurs REF et tableau associé
Dim tabloValRef() As String
Dim oRst As Recordset
Set xlWbk = xlApp.Workbooks.Open(pNomClasXLS)
'-----------------------------------------------------
' Excel visible pour les tests
xlApp.Visible = True
' Ouverture table
Set oRst = CurrentDb.OpenRecordset("tbl_Produits", dbOpenDynaset)
For F = 1 To xlWbk.Sheets.Count
' chargement de la 1ère feuille
Set xlWsh = xlWbk.Worksheets(F)
' dernière ligne utile de la feuille
lgDerlig = xlWsh.UsedRange.Rows.Count
' dernière colonne utile de la feuille
lgDerCol = xlWsh.UsedRange.Columns.Count
For L = 1 To lgDerlig
' rupture sur le type de produit
If xlWsh.Cells(L, 1) <> "" And Left(xlWsh.Cells(L, 1), 9) <> "CONDITION" Then
If xlWsh.Cells(L, 1) <> strProduit Then
strProduit = xlWsh.Cells(L, 1)
End If
Else
'copie des colonnes
If xlWsh.Cells(L, 1) = "CONDITIONNEMENT" Then
stListeRef = ""
For C = 1 To lgDerCol
'If xlWsh.Cells(L, C) = "1782 C2" Then
'If xlWsh.Cells(L, C) = "1782 C2" Or "1782B2" Or "1782 A2" Or "1773B2" Or "MC 780 B1" Then
If Left(xlWsh.Cells(L, C), 3) = "REF" Then
stListeRef = stListeRef & xlWsh.Cells(L, C) & "|"
End If
Next C
' tableau des REFx
tabloRef = Split(CStr(Left(stListeRef, Len(stListeRef) - 1)), "|")
ReDim Preserve tabloRef(UBound(tabloRef))
Else
' tableau des valeurs: 2 fois le nombre de REF + la colonne 1
If xlWsh.Cells(L, 1) <> "" Then
For C = 0 To (UBound(tabloRef) + 1) * 2
stListeValRef = stListeValRef & xlWsh.Cells(L, C + 1) & "|"
' colonne suivante
Next C
' remplissage du tableau et redimensionnement
tabloValRef = Split(CStr(Left(stListeValRef, Len(stListeValRef) - 1)), "|")
ReDim Preserve tabloValRef(UBound(tabloValRef))
' Chargement des données dans la table
' traitement pour une ligne de la feuille
For K = 1 To UBound(tabloRef) + 1
oRst.AddNew
oRst.Fields("Produit") = strProduit 'ex.: Congélateur1
oRst.Fields("Conditionnement") = tabloValRef(0) 'ex.: CONDITIONNEMENT1
oRst.Fields("Ref") = tabloRef(K - 1) 'ex.: REF1 du tableau tabloRef
oRst.Fields("ValRef1") = Val(tabloValRef(K * 2 - 1)) 'ex.: 100 du tableau tabloValRef
oRst.Fields("ValRef2") = Val(tabloValRef(K * 2)) 'ex.: 30 du tableau tabloValRef
oRst.Update
Next K
' vidage des valeurs
stListeValRef = ""
End If 'If xlwsh.Cells(L, 1) <> ""
End If 'If xlwsh.Cells(L, 1) = "CONDITIONNEMENT"
End If 'If xlwsh.Cells(L, 1) <> "" And Left(xlwsh.Cells(L, 1), 9) <> "CONDITION"
' ligne suivante
Next L
' feuille suivante, initialisation des variables
strProduit = ""
stListeRef = ""
stListeValRef = ""
Next F
' fermeture des objets
Set oRst = Nothing
xlWbk.Close
xlApp.Quit
End Sub |
Partager