Probleme code vba sous access
derek
-
derek -
derek -
Bonjour, j'ai besoin daide pour un module access en vba.
Je souhaite générer un tableau sous excel grace a ce module en fonction de 3 champs d'une de mes table access.
Le code que j'ai créé est le suivant :
Option Compare Database
Option Explicit
Option Private Module
Sub InitialiseExcel()
Dim xlApp, xlBook, xlRange As Variant 'Objets classeur
Dim xlWks, iRows, iCols, iRotate As Variant 'Objets feuille
Dim FichierXL As String
Dim Boucle, Cmpt As Integer
FichierXL = "C:\WINDOWS\Bureau\Preventif_maintenance\PréventifMill1bis2.xls"
'Création de l'objet Excel (main mise sur !)
Set xlApp = CreateObject("Excel.Application")
'Création de l'objet [Fichier Excel]
Set xlBook = xlApp.Workbooks.Open(FichierXL)
'Création de l'objet [Feuille] du classeur Excel.
xlApp.ActiveWorkbook.Worksheets.Add
Set xlWks = xlBook.ActiveSheet
'Création de la plage permise dans la feuille Excel.
Set xlRange = xlWks.Range("A1:A65535")
DoCmd.GoToRecord , , acLast
Cmpt = Forms![Générer_tableauExcel_Mill1].Recordset.RecordCount
DoCmd.GoToRecord , , acFirst
For Boucle = 1 To Cmpt
xlRange.Cells((Boucle + 1), 1).Value = Forms![Générer_tableauExcel_Mill1].[Tache]
DoCmd.GoToRecord , , acNext
Next Boucle
DoCmd.GoToRecord , , acFirst
For Boucle = 1 To Cmpt
xlRange.Cells(1, (Boucle + 1)).Value = Forms![Générer_tableauExcel_Mill1].[Semaine]
DoCmd.GoToRecord , , acNext
Next Boucle
DoCmd.GoToRecord , , acFirst
For Boucle = 1 To Cmpt
xlRange.Cells((Boucle + 1), (Boucle + 1)).Value = Forms![Générer_tableauExcel_Mill1].[Travail_Effectuer]
DoCmd.GoToRecord , , acNext
Next Boucle
DoCmd.GoToRecord , , acFirst
'Fermeture de Excel
'Rend Excel visible
xlApp.Visible = True
'Active le classeur
xlWks.Activate
'Positionnement en [ A1 ]
xlRange.Cells(1, 1).Select
'Message d'erreur en arrêt
xlApp.DisplayAlerts = False
'Sauvegarde du fichier Excel créé.
xlBook.SaveAs FichierXL
'Ferme l'application Excel
'xlApp.Quit
'Message d'erreur actif
xlApp.DisplayAlerts = True
Set xlRange = Nothing
Set xlWks = Nothing
Set xlBook = Nothing
Set xlApp = Nothing
MsgBox "Tableau créé"
End Sub
Mon probleme se situe au niveau de ma boucle car le tableau générer est de la forme :
sem5 sem13 sem32
reparation X oui
reparation X oui
reparation Y oui
etc...
alor que je voudrais :
sem5 sem13 sem32
reparation X oui oui
reparation Y oui
J'ai donc des difficultés a gerer les dédoublements de valeurs...
Quelqu'un pourrait-il m'aider a corriger mon code (la boucle) ?
Merci d'avance please help!
Je souhaite générer un tableau sous excel grace a ce module en fonction de 3 champs d'une de mes table access.
Le code que j'ai créé est le suivant :
Option Compare Database
Option Explicit
Option Private Module
Sub InitialiseExcel()
Dim xlApp, xlBook, xlRange As Variant 'Objets classeur
Dim xlWks, iRows, iCols, iRotate As Variant 'Objets feuille
Dim FichierXL As String
Dim Boucle, Cmpt As Integer
FichierXL = "C:\WINDOWS\Bureau\Preventif_maintenance\PréventifMill1bis2.xls"
'Création de l'objet Excel (main mise sur !)
Set xlApp = CreateObject("Excel.Application")
'Création de l'objet [Fichier Excel]
Set xlBook = xlApp.Workbooks.Open(FichierXL)
'Création de l'objet [Feuille] du classeur Excel.
xlApp.ActiveWorkbook.Worksheets.Add
Set xlWks = xlBook.ActiveSheet
'Création de la plage permise dans la feuille Excel.
Set xlRange = xlWks.Range("A1:A65535")
DoCmd.GoToRecord , , acLast
Cmpt = Forms![Générer_tableauExcel_Mill1].Recordset.RecordCount
DoCmd.GoToRecord , , acFirst
For Boucle = 1 To Cmpt
xlRange.Cells((Boucle + 1), 1).Value = Forms![Générer_tableauExcel_Mill1].[Tache]
DoCmd.GoToRecord , , acNext
Next Boucle
DoCmd.GoToRecord , , acFirst
For Boucle = 1 To Cmpt
xlRange.Cells(1, (Boucle + 1)).Value = Forms![Générer_tableauExcel_Mill1].[Semaine]
DoCmd.GoToRecord , , acNext
Next Boucle
DoCmd.GoToRecord , , acFirst
For Boucle = 1 To Cmpt
xlRange.Cells((Boucle + 1), (Boucle + 1)).Value = Forms![Générer_tableauExcel_Mill1].[Travail_Effectuer]
DoCmd.GoToRecord , , acNext
Next Boucle
DoCmd.GoToRecord , , acFirst
'Fermeture de Excel
'Rend Excel visible
xlApp.Visible = True
'Active le classeur
xlWks.Activate
'Positionnement en [ A1 ]
xlRange.Cells(1, 1).Select
'Message d'erreur en arrêt
xlApp.DisplayAlerts = False
'Sauvegarde du fichier Excel créé.
xlBook.SaveAs FichierXL
'Ferme l'application Excel
'xlApp.Quit
'Message d'erreur actif
xlApp.DisplayAlerts = True
Set xlRange = Nothing
Set xlWks = Nothing
Set xlBook = Nothing
Set xlApp = Nothing
MsgBox "Tableau créé"
End Sub
Mon probleme se situe au niveau de ma boucle car le tableau générer est de la forme :
sem5 sem13 sem32
reparation X oui
reparation X oui
reparation Y oui
etc...
alor que je voudrais :
sem5 sem13 sem32
reparation X oui oui
reparation Y oui
J'ai donc des difficultés a gerer les dédoublements de valeurs...
Quelqu'un pourrait-il m'aider a corriger mon code (la boucle) ?
Merci d'avance please help!
A voir également:
- Probleme code vba sous access
- Code ascii - Guide
- Code puk bloqué - Guide
- Comment déverrouiller un téléphone quand on a oublié le code - Guide
- Code activation windows 10 - Guide
- Scanner qr code pc - Guide
2 réponses
J'ai réecris la boucle en la simplifiant pour plus de compréhension :
DoCmd.GoToRecord , , acLast
Cmpt = Forms![MaTable].Recordset.RecordCount
DoCmd.GoToRecord , , acFirst
For Boucle = 1 To Cmpt
xlRange.Cells((Boucle + 1), 1).Value = Forms![MaTable].[Tache]
xlRange.Cells(1, (Boucle + 1)).Value = Forms![MaTable].[Semaine]
xlRange.Cells((Boucle + 1), (Boucle + 1)).Value = Forms![MaTable].[Travail_Effectuer]
DoCmd.GoToRecord , , acNext
Next
DoCmd.GoToRecord , , acFirst
Pouvez vous m'aider a réaliser ce que j'ai expliqué ds mon précedent message? (ya urgence en plus!)
Merci d'avance...
DoCmd.GoToRecord , , acLast
Cmpt = Forms![MaTable].Recordset.RecordCount
DoCmd.GoToRecord , , acFirst
For Boucle = 1 To Cmpt
xlRange.Cells((Boucle + 1), 1).Value = Forms![MaTable].[Tache]
xlRange.Cells(1, (Boucle + 1)).Value = Forms![MaTable].[Semaine]
xlRange.Cells((Boucle + 1), (Boucle + 1)).Value = Forms![MaTable].[Travail_Effectuer]
DoCmd.GoToRecord , , acNext
Next
DoCmd.GoToRecord , , acFirst
Pouvez vous m'aider a réaliser ce que j'ai expliqué ds mon précedent message? (ya urgence en plus!)
Merci d'avance...