1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
Sub TestbyMfoxy()
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Dim f1 As Worksheet, f2 As Worksheet
Dim LastCel As Range, columnsToDelete As Range
With ThisWorkbook
Set f1 = .Worksheets("Feuil1")
Set f2 = .Worksheets("Feuil2")
Set LastCel = getLastCellWithData(f1) 'On récupère la dernière ligne et col utilisé dans F1
End With
With f2
.Cells.ClearContents 'Cleaning de l'extraction
.Range(.Cells(1, 1), .Cells(LastCel.Row, LastCel.Column)).Value = f1.Range(f1.Cells(1, 1), f1.Cells(LastCel.Row, LastCel.Column)).Value 'Copier-coller de toute la plage f1 vers f2
.Range(.Cells(1, 1), .Cells(LastCel.Row, LastCel.Column)).Value = .Range(.Cells(1, 1), .Cells(LastCel.Row, LastCel.Column)).Value 'On supprime toutes les Formules
For i = LastCel.Column To 2 Step -1 'On boucle sur chaque colonne pour créér une liste des colonnes à supprimer (colonne pair)
If (i Mod 2) = 0 And columnsToDelete Is Nothing Then Set columnsToDelete = .Columns(i)
If (i Mod 2) = 0 And Not columnsToDelete Is Nothing Then Set columnsToDelete = Application.Union(columnsToDelete, .Columns(i))
Next i
End With
If Not columnsToDelete Is Nothing Then columnsToDelete.Delete 'On Supprime Toutes les colonnes en une Fois
Set LastCel = Nothing: Set columnsToDelete = Nothing 'Cleaning Variable
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
Function getLastCellWithData(sh As Worksheet) As Range
'Pierre F sur DVP
Dim r As Long: r = 1
Dim c As Long: c = 1
Dim EnableEvents As Boolean
Dim cell As Range
On Error GoTo EndHandler
EnableEvents = Application.EnableEvents
Application.EnableEvents = False
If sh.Cells.SpecialCells(xlCellTypeLastCell).Address <> "$A$1" Then
Set cell = sh.Cells.Find(what:="*", after:=sh.Cells(1, 1), LookIn:=xlValues, lookat:=xlWhole, _
searchorder:=xlByRows, SearchDirection:=xlPrevious)
If Not cell Is Nothing Then
r = cell.Row
c = sh.Cells.Find(what:="*", after:=sh.Cells(1, 1), LookIn:=xlValues, lookat:=xlWhole, _
searchorder:=xlByColumns, SearchDirection:=xlPrevious).Column
End If
End If
Set getLastCellWithData = sh.Cells(r, c)
EndHandler:
Application.EnableEvents = EnableEvents
End Function |