Exportation Access vers Excel

marachinaru Messages postés 3 Statut Membre -  
pijaku Messages postés 13513 Date d'inscription   Statut Modérateur Dernière intervention   -
Bonjour,

J'utilise VBA pour exporter à partir d'Access des données vers un excel mis en forme.
Cependant quand je lance l'exportation deux fois d'affilés, j'ai une erreur disant :

Erreur d'exécution '1004' :
« La méthode 'Cells' de l'objet'_Global' a échoué »

Cette erreur est sur la ligne en gras du code ci-dessous. Savez-vous comment rectifier cela ? Il doit falloir réinitialiser mais je n'y arrive pas.
C'est ici une partie de mon code mais le problème est seulement ici.
Merci d'avance.

Sub expExcel()

Dim appexcel As Excel.Application
Dim wbexcel As Excel.Workbook
Set appexcel = CreateObject("Excel.Application")
appexcel.Visible = True
Set wbexcel = appexcel.Workbooks.Open("C:\Users\moi\Documents\test.xlsx")
appexcel.Sheets("Feuil1").Select
appexcel.Cells(1, 1) = "Title"
appexcel.Range(Cells(1, 1), Cells(1, 6)).MergeCells = True
End Sub

1 réponse

  1. pijaku Messages postés 13513 Date d'inscription   Statut Modérateur Dernière intervention   2 772
     
    Bonjour,

    appexcel est ta variable représentant l'application Excel, wbexcel ton classeur. Sert toi donc de la variable "classeru", comme ceci :

    Sub expExcel()

    Dim appexcel As Excel.Application
    Dim wbexcel As Excel.Workbook
    Set appexcel = CreateObject("Excel.Application")
    appexcel.Visible = True
    Set wbexcel = appexcel.Workbooks.Open("C:\Users\moi\Documents\test.xlsx")
    With wbexcel.Sheets("Feuil1")
    .Cells(1, 1) = "Title"
    .Range(Cells(1, 1), Cells(1, 6)).MergeCells = True
    End With
    End Sub

    0