kgadhi
Messages postés121Date d'inscriptionmardi 9 août 2005StatutMembreDernière intervention18 décembre 2007
-
18 déc. 2007 à 10:44
cs_MPi
Messages postés3877Date d'inscriptionmardi 19 mars 2002StatutMembreDernière intervention17 août 2018
-
18 déc. 2007 à 11:18
Bonjour tous le monde et merci d'avance pour votre aide,
J'ai développé une macro qui permet de mettre à jour un fichier excel depuis un autre fichier.
Je travaille sous excel 2003. Aprés plusieurs tentatives, la macro marche.
Apres j'ai voulu afficher le fichier sous excel 2007 et la j'ai l'erreur suivante :
Erreur d'execution '1004' : Excel ne parvient pas à insérer les feuilles dans le classeur de destination car il contient moins de lignes et de colonnes que le classeur source. Pour déplacer ou copier les données vers le classeur de destination, vous pouvez les selectionner, puis utiliser les commandes Copier et Coller pour les insérer dans les feuilles d'un autre classeur.
Je vous mets aussi le code source
<code source>
' Récuperer le nom du rep courant
nomRepCourant = ThisWorkbook.Path
longueur = Len(nomRepCourant)
If longueur > 0 Then
pos = InStr(nomRepCourant, "")
' MsgBox nomRepCourant & " " & pos
If (pos <> 0) Then
trouve = True
While trouve
longueur = Len(nomRepCourant)
nomRepCourant = Right(nomRepCourant, longueur - pos)
pos = InStr(nomRepCourant, "")
' MsgBox "rep1 : " & nomRepCourant & " " & pos
If (pos = 0) Then
trouve = False
End If
Wend
End If
End If
' MsgBox "Fichier: " & nomRepCourant
' Le nom du fichier
Fichier = nomRepCourant & ".txt"
nomFichier = nomRepCourant
' MsgBox "fichier : " & nomFichier
Sheets(nomFichier).Move After:=Workbooks("SUIVI DES VISITŠ'ENTREPRISE.xls").Sheets(1) L'erreur est à cette ligne
' Compter le nombre de lignes recupérées
Open CheminFichier For Input As #1
While Not EOF(1)
Line Input #1, Texte
NbLignes = NbLignes + 1
Wend
Close #1
Lignes = 0
' Verifier l'existance de la feuille modPortefeuille
If FeuilleExiste(ThisWorkbook, "modPortefeuille") Then ' La feuille existe donc on la vide
Sheets("modPortefeuille").Select
ActiveSheet.Shapes.SelectAll
Selection.Delete
ActiveSheet.Cells.Clear
Else ' On ajoute la feuille
Sheets.Add
ActiveSheet.Name = "modPortefeuille"
Sheets("modPortefeuille").Select
Sheets("modPortefeuille").Move Before:=Sheets(4)
End If
Sheets("Suivi des visites entreprise").Select
Range("B4:G4").Select
Selection.Copy
Sheets("modPortefeuille").Select
Range("B4").Select
ActiveSheet.Paste
Range("I4").Select
ActiveSheet.Paste
Range("C3:F3").Select
Application.CutCopyMode = False
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Range("J3:M3").Select
Application.CutCopyMode = False
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Range("B5").Select
Sheets(nomFichier).Select
' MsgBox "nbrelignes : " & NbLignes
For i = 1 To NbLignes + 1
DoEvents
RefAMI = Cells(i, 1).Value
' MsgBox "i : " & i & " , RefAmi : " & RefAMI
If Cells(i, 9).Value = "" Then 'Ajout de la ligne
Sheets("Suivi des visites entreprise").Select
' Verifier que le Ref Ami n'existe pas deja dans la liste
TrouveRef = False
For j = 1 To ActiveSheet.UsedRange.Rows.Count + 1
If Cells(j, 2).Value = RefAMI Then
TrouveRef = True
End If
Next j
' Ajouter la ligne
If TrouveRef = False Then
Sheets(nomFichier).Select
Lignes = Lignes + 1
Range(Cells(i, 1), Cells(i, 8)).Select
Selection.Copy
Sheets("Suivi des visites entreprise").Select
Range("B65536").End(xlUp).Offset(1, 0).PasteSpecial
' Cadrer
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
End If
' Cadrer
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
Sheets(nomFichier).Select
Else ' Suppression de la ligne
Sheets("Suivi des visites entreprise").Select
For j = 1 To ActiveSheet.UsedRange.Rows.Count
If Cells(j, 2).Value = RefAMI Then
Cells(j, 1).EntireRow.Delete
'Range(Trim(Str(ActiveCell.Row)) & ":" & Trim(Str(ActiveCell.Row))).Select
'Selection.Delete Shift:=xlUp
End If
Next j
NoLgnFin = ActiveSheet.UsedRange.Rows.Count
' MsgBox "Nb de ligne du fichier modPortefeuilleMed.txt : " & NoLgnFin
' Cadrer
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
'Range("B5").CurrentRegion.Rows(Range("B5").CurrentRegion.Rows.Count).Copy
'Range("B65536").End(xlUp).Offset(1, 0).PasteSpecial
'Selection.ClearContents
End If
' Sheets("Suivi des visites entreprise").Select
Sheets("modPortefeuille").Select
Application.ScreenUpdating = True
End Sub
</code source>
Et le probleme c'est que sous excel 2003 ça marche tres bien.
Je suis débutant en vba.
cs_MPi
Messages postés3877Date d'inscriptionmardi 19 mars 2002StatutMembreDernière intervention17 août 201823 18 déc. 2007 à 11:18
Comme le message te l'indique, pourquoi ne pas utiliser le copier/coller plutôt que Move ?
Est-ce que les 2 classeurs ont le même nombre de lignes et de colonnes ? Je pense que la version 2007 a beaucoup plus de colonnes et de lignes que les versions précédentes. Il y donc peut-être conflit entre tes 2 classeurs à ce niveau (?)