'Liberer mémoire:
Set xlSheetData = Nothing
Set xlSheetNew =Nothing
Set xlBook = Nothing
Set xlApp = Nothing
xlBook.Saved = True
xlBook.Close
Const xlValues = -4163 Const xlNone = -4142 Dim xlApp,xlBook,xlSheetData,xlSheetNew,k Path="C:\tmp\monfichier.xls" msgbox Path Set xlApp= CreateObject("Excel.Application") xlApp.Visible=True Msgbox "Ouverture du classeur" Set xlBook= xlApp.Workbooks.Open(Path) Msgbox " Ajout d'une feuille" xlBook.Worksheets.Add.Name = "Coucou" Set xlSheetData= xlBook.WorkSheets("Hello") Set xlSheetNew= xlBook.WorkSheets("Coucou") '------------- Msgbox "Copier les données" DerLigne = xlSheetData.UsedRange.Rows.Count xlSheetData.Rows("2:" & DerLigne).Copy Msgbox "Coller les données: " xlSheetNew.Rows("1").PasteSpecial xlValues, xlNone, False, False MsgBox "Supprimer l'ancienne feuille" xlApp.DisplayAlerts = False xlSheetData.Delete xlApp.DisplayAlerts = True '------------- Msgbox "Quitter et sauvegarder" xlBook.Close True xlApp.Quit '------------- 'Liberer mémoire: Set xlSheetData = Nothing Set xlSheetNew =Nothing Set xlBook = Nothing Set xlApp = Nothing
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question--
Set xlBook = xlApp.Workbooks.Open(Path)
xlBook.Worksheets.Add.Name = "Coucou"
'-------------
'Copier les données:
DerLigne = xlBook.Sheets("Hello").UsedRange.Rows.Count
xlBook.Sheets("Hello").Rows("2:" & DerLigne).Copy
'Coller les données:
xlBook.Sheets("Coucou").Rows("1").PasteSpecial xlValues, xlNone, False, False
'Supprimer l'ancienne feuille
xlApp.DisplayAlerts = False
xlBook.Sheets("Hello").Delete
xlBook.Close True
xlApp.DisplayAlerts = True
xlApp.Quit
'-------------
'Liberer mémoire:
Set xlBook = Nothing
Set xlApp = Nothing
Dim toto As WorkbookIl faut par ailleurs qu'il soit bien clair qu'il ne fonctionnerait (comme le tien, d'ailleurs) qu'une seule fois, puisque, dès la seconde, la feuille "Hello" n'existe déjà plus.
Set toto = Workbooks.Open("P:\monfichier.xls")
With toto
With .Worksheets("hello")
.UsedRange.Copy
.Range("A1").PasteSpecial xlValues, xlNone, False, False
.Rows(1).Delete
.Name = "coucou"
End With
.Close True
End With
With CreateObject("Excel.Application").Workbooks.Open("P:\monfichier.xls")--
With .Worksheets("Hello")
.UsedRange.Copy
.Range("A1").PasteSpecial -4163, -4142, False, False
.Rows(1).Delete
.Name = "Coucou"
.Range("A1").Select
End With
.Close True
End With
msgbox xlvalues & vbcrlf & xlnonexlvalues et xlnone sont des constantes VBA déjà existantes dans VBA
Dans la feuille "Hello" il y a des macros sur la ligne 1