Comment manipuler plusieurs fichiers Excel
Comme vous pouvez le voir, je suis tout nouveau que ce sois sur ce site ou même VBA, je m'y suis mis avant-hier (je connais quand même le C). Ayant quelques 200 feuilles Excel à faire, j'en suis venu à penser que les macros pouvais m'aider, mais j'ai malheureusement des problèmes, le programme que je souhaite faire nécessite l'utilisation de 3 fichiers (2 sources + 1) et ne fonctionne pas je ne vois pas comment faire autrement pour faire l'appel des fichiers (je crois que c'est ca qui marche pas entre autres).
d'ou ma question, comment faire pour utiliser plusieurs fichiers Excel sous VB?
je mets mon programme pour que vous compreniez mieux comment j'ai fais et qui n'est pas bon.:(
--
Code:
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
|
Sub OM()
'Declaration des variables
Dim FP As Excel.Workbook
Dim FPws As Excel.Worksheet
Dim Listing As Excel.Workbook
Dim Listingws As Excel.Worksheet
Dim Produit As Excel.Workbook
Dim Produitws As Excel.Worksheet
Dim lig
Dim I As Integer
'Ouverture des applications
Workbooks.Add ("C:\Documents and Settings\tai-sama\Mes documents\1. Olivier\ESIREM\Stage\Stage Bodycote\FP.xls")
Set FP = ActiveWorkbook
Set FPws = FP.Worksheets(1)
FP.Sheets.Add After:=Sheets(FP.Sheets.Count), Count:=200
Call Trierfeuilles
Set Listing = Workbooks.Open("C:\Documents and Settings\tai-sama\Mes documents\1. Olivier\ESIREM\Stage\Stage Bodycote\AA Listing produits chimiques.xls")
Set Listingws = Listing.Worksheets(2)
Set Produit = Workbooks.Open("C:\Documents and Settings\tai-sama\Mes documents\1. Olivier\ESIREM\Stage\Stage Bodycote\Création Fiche de produits\Fiche Produits.xls")
Set Produitws = Produit.Worksheets(1)
'Debut du programme de copie d'une sélection vers une autre feuille
Set Produitws = ActiveWorkbook.Sheets(1)
Produitws("Feuil1").Range("A1:AS42").Copy
Set FPws = ActiveWorkbook.Sheets(1) 'activation de la feuille que je souhaite
FPws("Feuil1").Paste Destination:=FPws("Feuil1").Range("A1:AS42")
'ou FPws("Feuil1").Paste Destination:=FPws("Feuil1").Range("A1")
Application.CutCopyMode = False
Worksheets().FillAcrossSheets Worksheets("Feuil1").Range("A1:AS42", xlFillWithAll) 'doit être rediriger vers le fichier avec les 200 feuilles
'Debut du programme de remplissage des données
Set Listing = ActiveWorkbook
Set Listingws = Listing.ActiveSheet
I = 1
lig = 8
For lig = 8 To 12
Listingws("Feuil2").Range(Listingws.Cells(lig, 3)).Copy
Set FP = ActiveWorkbook
Set FPws = FP.ActiveSheet
Sheets("Feuil(" & I & ")").Select
FPws("Feuil(" & I & ")").Paste Destination:=FPws("Feuil(" & I & ")").Range(FPws.Cells(7, 12), FPws.Cells(9, 12))
Set Listing = ActiveWorkbook
Set Listingws = Listing.ActiveSheet
Application.CutCopyMode = False
Listingws("Feuil2").Range(Listingws.Cells(lig, 2)).Copy
Set FP = ActiveWorkbook
Set FPws = FP.ActiveSheet
Sheets("Feuil(" & I & ")").Select
FPws("Feuil(" & I & ")").Paste Destination:=FPws("Feuil(" & I & ")").Range(FPws.Cells(5, 12))
Set Listing = ActiveWorkbook
Set Listingws = Listing.ActiveSheet
Application.CutCopyMode = False
Listingws("Feuil2").Range(Listingws.Cells(lig, 41)).Copy
Set FP = ActiveWorkbook
Set FPws = FP.ActiveSheet
Sheets("Feuil(" & I & ")").Select
FPws("Feuil(" & I & ")").Paste Destination:=FPws("Feuil(" & I & ")").Range(FPws.Cells(9, 12))
Set Listing = ActiveWorkbook
Set Listingws = Listing.ActiveSheet
Application.CutCopyMode = False
I = I + 1
Next lig
'Fermeture d'Excel
FP.Close
Listing.Close
Produit.Close
Set FPws = Nothing
Set Listingws = Nothing
Set Listing = Nothing
Set Produitws = Nothing
Set Produit = Nothing
'Fin du programme
End Sub
Sub Trierfeuilles()
Dim Ws As Object
Dim I As Byte
Application.ScreenUpdating = False
For Each Ws In ActiveWorkbook.Sheets
For I = 2 To ActiveWorkbook.Sheets.Count
If Sheets(I - 1).Name > Sheets(I).Name Then Sheets(I - 1).Move After:=Sheets(I)
Next I
Next Ws
End Sub |
--
Merci pour l'aide que vous pourrez m'apporter