Methode range de l'objet global a echoué

Résolu
RodolpheG Messages postés 13 Date d'inscription   Statut Membre Dernière intervention   -  
 RodolpheG -

Bonjour

Quand je clique sur mon bouton exporter, le code ci joint se déroule parfaitement. Je ferme mon excel sauvegardé et je clique une deuxième fois pour demander un nouvel export statistiques avec de nouvelles données et la le code plante avec le message "Méthode range de l'objet global a échoué" à DerLig = Range("A1").CurrentRegion.Rows.Count

Ne connaissant pas très bien, je ne sais ce que cela veut dire; merci par avance de votre aide

Dim Xlapp As Object
Dim xlBook As Excel.Workbook
Dim monchemin As String, monfichier As String, lechemin As String, montitre As String, MaPlage As Range, MaSomme As Single
Dim DerCol As Integer
Dim DerLig As Integer
Dim J As Integer
Dim I As Integer


monchemin = CurrentProject.Path
monfichier = Environ("UserName") & "_" & Format(Now, "yymmddhhnnss") ' je ne mets pas d'extension, elle sera crée par l'exportation
lechemin = monchemin & "\" & monfichier
montitre = "Liste des dossiers avec l'évenement :" & Evt.Value & " du " & DatDeb.Value & " au " & DatFin.Value

DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, "Vue_Statistiques", lechemin, True

DoCmd.Close acForm, "F_STATS"

Set Xlapp = CreateObject("Excel.Application")
Xlapp.Visible = True
Set xlBook = Xlapp.Workbooks.Open(lechemin)


'Sheets("Vue_Statistiques").Select
'Range("A1").Select
'' Insertion Ligne & Fusion

'Rows(1).Insert
'Range("A1:L1").Insert

'' Nb ligne & colonne

'DerCol = ActiveSheet.UsedRange.Columns.Count
'DerCol = Range("A1").CurrentRegion.Columns.Count
DerCol = 12
'MsgBox Range("A1").CurrentRegion.Columns.Count
DerLig = Range("A1").CurrentRegion.Rows.Count
MsgBox Range("A1").CurrentRegion.Rows.Count


Range("A1:L" & DerLig).Cut Range("A2:L" & DerLig + 1)
 
Range("A1") = montitre
Range("A1").Interior.Color = RGB(255, 255, 0)
Range("A1:L1").Merge
Range("A1:L1").HorizontalAlignment = xlCenter
Range("A1:L1").Font.Bold = True

For J = 0 To DerCol - 1
    With Cells(2, J + 1)
            .Interior.ColorIndex = 15
            .Interior.Pattern = xlSolid
            .Borders(xlEdgeBottom).LineStyle = xlContinuous
            .Borders(xlEdgeBottom).Weight = xlThin
            .Borders(xlEdgeBottom).ColorIndex = xlAutomatic
            .Borders(xlEdgeRight).LineStyle = xlContinuous
            .Borders(xlEdgeRight).Weight = xlThin
            .Borders(xlEdgeRight).ColorIndex = xlAutomatic
            .HorizontalAlignment = xlCenter
            .Interior.PatternColorIndex = xlAutomatic
            .Interior.ThemeColor = xlThemeColorDark1
            .Interior.TintAndShade = -0.149998474074526
            .Interior.PatternTintAndShade = 0

    End With
Next J



'For J = 3 To DerniereLigne
'  For i = 1 To DerniereColonne
'    With Cells(J, i)
'            .Borders(xlEdgeBottom).LineStyle = xlContinuous
'            .Borders(xlEdgeBottom).Weight = xlThin
'            .Borders(xlEdgeBottom).ColorIndex = xlAutomatic
'            .Borders(xlEdgeRight).LineStyle = xlContinuous
'            .Borders(xlEdgeRight).Weight = xlThin
'            .Borders(xlEdgeRight).ColorIndex = xlAutomatic
'            .HorizontalAlignment = xlCenter
'    End With
'  Next i
'Next J

'' Largeur colonne

Columns("A:L").EntireColumn.AutoFit
Columns("K").EntireColumn.ColumnWidth = 50

'' Format & Somme & Gras

Set MaPlage = Range("I3:I" & DerLig + 1)
MaSomme = WorksheetFunction.Sum(MaPlage)

Range("I" & DerLig + 2) = MaSomme
Range("I" & DerLig + 2).Font.Bold = True
Range("I3:I" & DerLig + 2).NumberFormat = "#,###"

Range("A" & DerLig + 2 & ":L" & DerLig + 2).Interior.ColorIndex = 15

Set mafeuille = Nothing
Set xlBook = Nothing
Set Xlapp = Nothing

End Sub

2 réponses

NonoM45 Messages postés 773 Date d'inscription   Statut Membre Dernière intervention  
 

Bonjour,

Très étonné que cela est fonctionné une 1ère fois vous ne faites pas référence à l'objet Worksheet

Sub test()
  Dim Xlapp As Object
  Dim xlBook As Excel.Workbook
  Dim xlSheet As Excel.Worksheet
  Dim monchemin As String, monfichier As String, lechemin As String, montitre As String, MaPlage As Range, MaSomme As Single
  Dim DerCol As Integer
  Dim DerLig As Integer
  Dim J As Integer
  Dim I As Integer


  monchemin = CurrentProject.Path
  monfichier = Environ("UserName") & "_" & Format(Now, "yymmddhhnnss") ' je ne mets pas d'extension, elle sera crée par l'exportation
  lechemin = monchemin & "\" & monfichier
  montitre = "Liste des dossiers avec l'évenement :" & Evt.Value & " du " & DatDeb.Value & " au " & DatFin.Value

  DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, "Vue_Statistiques", lechemin, True

  DoCmd.Close acForm, "F_STATS"

  Set Xlapp = CreateObject("Excel.Application")
  Xlapp.Visible = True
  Set xlBook = Xlapp.Workbooks.Open(lechemin)
  DerCol = 12
  With xlBook.Sheets("Vue_Statistiques")
    DerLig = .Range("A1").CurrentRegion.Rows.Count
    .Range("A1:L" & DerLig).Cut .Range("A2:L" & DerLig + 1)
    .Range("A1") = montitre
    .Range("A1").Interior.Color = RGB(255, 255, 0)
    .Range("A1:L1").Merge
    .Range("A1:L1").HorizontalAlignment = xlCenter
    .Range("A1:L1").Font.Bold = True
    For J = 0 To DerCol - 1
      With .Cells(2, J + 1)
        .Interior.ColorIndex = 15
        .Interior.Pattern = xlSolid
        .Borders(xlEdgeBottom).LineStyle = xlContinuous
        .Borders(xlEdgeBottom).Weight = xlThin
        .Borders(xlEdgeBottom).ColorIndex = xlAutomatic
        .Borders(xlEdgeRight).LineStyle = xlContinuous
        .Borders(xlEdgeRight).Weight = xlThin
        .Borders(xlEdgeRight).ColorIndex = xlAutomatic
        .HorizontalAlignment = xlCenter
        .Interior.PatternColorIndex = xlAutomatic
        .Interior.ThemeColor = xlThemeColorDark1
        .Interior.TintAndShade = -0.149998474074526
        .Interior.PatternTintAndShade = 0
      End With
    Next J
    '' Largeur colonne
    .Columns("A:L").EntireColumn.AutoFit
    .Columns("K").EntireColumn.ColumnWidth = 50

    '' Format & Somme & Gras

    Set MaPlage = .Range("I3:I" & DerLig + 1)
    MaSomme = WorksheetFunction.Sum(MaPlage)

    .Range("I" & DerLig + 2) = MaSomme
    .Range("I" & DerLig + 2).Font.Bold = True
    .Range("I3:I" & DerLig + 2).NumberFormat = "#,###"
    .Range("A" & DerLig + 2 & ":L" & DerLig + 2).Interior.ColorIndex = 15
  End With

  Set mafeuille = Nothing
  Set xlBook = Nothing
  Set Xlapp = Nothing

End Sub

A+

0
RodolpheG
 

Bonjour NONOM45

Merci pour la réponse et surtout pour la simplification de ma requête

Tout et ok

RodolpheG

0