XL 2019 Autofill - Une seule ligne

Inés310790

XLDnaute Nouveau
Bonjour tout le monde,

J'aurais besoin d'un conseil pour un problème de macro :) je suis très débutante !
Je m'explique : je reçois mensuellement des fichiers (ci joint un exemple).

Ma macro réalise les action suivantes:
- tri en fonction des opérations "DST" et "DOMV" sur deux feuillez différentes
- réalisation de "modèle" sur feuille séparé en fonction des opérations

Mon problème: j'utilise la fonction Autofill à plusieurs reprises. Cela fonction très bien quand il y a plusieurs opérations, mais malheureusement parfois il n'y a qu'une seul ligne pour un type d'opération et cela ne fonctionne plus du tout.

Auriez vous une solution à mon problème... ci dessous le code que j'ai essayé mais qui ne fonctionne pas ... et maintenant je suis complètement perdue

Sub MACROCHINOIS()
Sheets.Add(After:=Worksheets(Worksheets.Count)).Name = "DST"
Sheets.Add(After:=Worksheets(Worksheets.Count)).Name = "DOMV"
Sheets(1).Select
derli = Sheets(1).Cells.Find("*", , , , xlByRows, xlPrevious).Row
Sheets(1).Cells(derli, 1).EntireRow.Delete
Dim plage As Range
Application.ScreenUpdating = False
With Sheets(1)
.AutoFilterMode = False
.[1:1].Insert 'nécessaire si pas de titres
Set plage = .Range("A2:R" & .[A65536].End(xlUp).Row)
plage.AutoFilter 2, "*DST*"
With Sheets("DST")
.[A1:B65536].Clear
plage.SpecialCells(xlCellTypeVisible).Copy .[A1]
.[A1:R1].Delete xlUp
.Activate 'facultatif
End With
.[1:1].Delete
End With
Application.ScreenUpdating = False
With Sheets(1)
.AutoFilterMode = False
.[1:1].Insert 'nécessaire si pas de titres
Set plage = .Range("A2:R" & .[A65536].End(xlUp).Row)
plage.AutoFilter 2, "*DOMV*"
With Sheets("DOMV")
.[A1:B65536].Clear
plage.SpecialCells(xlCellTypeVisible).Copy .[A1]
.[A1:R1].Delete xlUp
.Activate 'facultatif
End With
.[1:1].Delete
End With
Application.DisplayAlerts = False
Sheets(1).Delete
Sheets.Add(After:=Worksheets(Worksheets.Count)).Name = "Modèle TVA 3" 'ajoute une Feuille devant la Feuille active et la nomme "NouvelleFeuille"
Sheets.Add(After:=Worksheets(Worksheets.Count)).Name = "Modèle TVA 4" 'ajoute une Feuille devant la Feuille active et la nomme "NouvelleFeuille"
Sheets.Add(After:=Worksheets(Worksheets.Count)).Name = "Modèle TVA 5" 'ajoute une Feuille devant la Feuille active et la nomme "NouvelleFeuille"
With Sheets(2).Select
DernLigne = .Range("A" & .Rows.Count).End(xlUp).Row
If DernLigne < 1 Then Exit Sub
If DernLigne >= 1 Then
Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
With Sheets("Modèle TVA 3").Select
Range("A1").Select
ActiveSheet.Paste
Range("B1").Select
Application.CutCopyMode = False
DernLigne = .Range("A" & .Rows.Count).End(xlUp).Row
If DernLigne = 1 Then ActiveCell.FormulaR1C1 = "707021"
If DernLigne > 1 Then
End If
End With
Dim DerLigne As Long
DerLigne = Range("A65536").End(xlUp).Row
ActiveCell.FormulaR1C1 = "707021"
Selection.AutoFill Destination:=Range("B1:B" & DerLigne)
Range("C1").Select
Application.CutCopyMode = False
DernLigne = .Range("A" & .Rows.Count).End(xlUp).Row
If DernLigne = 1 Then ActiveCell.FormulaR1C1 = "VF1"
If DernLigne > 1 Then
DerLigne = Range("B65536").End(xlUp).Row
ActiveCell.FormulaR1C1 = "VF1"
Selection.AutoFill Destination:=Range("C1:C" & DerLigne), Type:=xlFillCopy
Sheets(2).Select
Range("G1").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets("Modèle TVA 3").Select
Range("D1").Select
ActiveSheet.Paste
Sheets(2).Select
Range("Q1").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Modèle TVA 3").Select
Range("E1").Select
ActiveSheet.Paste
Sheets(2).Select
Range("I1").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Modèle TVA 3").Select
Range("G1").Select
ActiveSheet.Paste
Sheets(2).Select
Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
With Sheets("Modèle TVA 4").Select
Range("A1").Select
ActiveSheet.Paste
Range("B1").Select
Application.CutCopyMode = False
DernLigne = .Range("A" & .Rows.Count).End(xlUp).Row
If DernLigne = 1 Then ActiveCell.FormulaR1C1 = "445721"
If DernLigne > 1 Then
DerLigne = Range("A65536").End(xlUp).Row
ActiveCell.FormulaR1C1 = "445721"
Selection.AutoFill Destination:=Range("B1:B" & DerLigne)
Range("C1").Select
Application.CutCopyMode = False
DernLigne = .Range("A" & .Rows.Count).End(xlUp).Row
If DernLigne = 1 Then ActiveCell.FormulaR1C1 = "VF1"
If DernLigne > 1 Then
DerLigne = Range("B65536").End(xlUp).Row
ActiveCell.FormulaR1C1 = "VF1"
Selection.AutoFill Destination:=Range("C1:C" & DerLigne), Type:=xlFillCopy
Sheets(2).Select
Range("G1").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets("Modèle TVA 4").Select
Range("D1").Select
ActiveSheet.Paste
Sheets(2).Select
Range("Q1").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
With Sheets("Modèle TVA 4").Select
Range("E1").Select
ActiveSheet.Paste
Range("G1").Select
Application.CutCopyMode = False
DernLigne = .Range("A" & .Rows.Count).End(xlUp).Row
If DernLigne = 1 Then ActiveCell.FormulaR1C1 = "='Modèle TVA 3'!RC*20%"
If DernLigne > 1 Then
DerLigne = Range("E65536").End(xlUp).Row
ActiveCell.FormulaR1C1 = "='Modèle TVA 3'!RC*20%"
Selection.AutoFill Destination:=Range("G1:G" & DerLigne)
Sheets(2).Select
Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
With Sheets("Modèle TVA 5").Select
Range("A1").Select
ActiveSheet.Paste
Range("B1").Select
Application.CutCopyMode = False
DernLigne = .Range("A" & .Rows.Count).End(xlUp).Row
If DernLigne = 1 Then ActiveCell.FormulaR1C1 = "411000"
If DernLigne > 1 Then
DerLigne = Range("A65536").End(xlUp).Row
ActiveCell.FormulaR1C1 = "411000"
Selection.AutoFill Destination:=Range("B1:B" & DerLigne)
Range("C1").Select
Application.CutCopyMode = False
DernLigne = .Range("A" & .Rows.Count).End(xlUp).Row
If DernLigne = 1 Then ActiveCell.FormulaR1C1 = "VF1"
If DernLigne > 1 Then
DerLigne = Range("B65536").End(xlUp).Row
ActiveCell.FormulaR1C1 = "VF1"
Selection.AutoFill Destination:=Range("C1:C" & DerLigne), Type:=xlFillCopy
Sheets(2).Select
Range("G1").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets("Modèle TVA 5").Select
Range("D1").Select
ActiveSheet.Paste
Sheets(2).Select
Range("Q1").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
With Sheets("Modèle TVA 5").Select
Range("E1").Select
ActiveSheet.Paste
Range("F1").Select
Application.CutCopyMode = False
DernLigne = .Range("A" & .Rows.Count).End(xlUp).Row
If DernLigne = 1 Then ActiveCell.FormulaR1C1 = "='Modèle TVA 3'!RC[1]+'Modèle TVA 4'!RC[1]"
If DernLigne > 1 Then
DerLigne = Range("E65536").End(xlUp).Row
ActiveCell.FormulaR1C1 = "='Modèle TVA 3'!RC[1]+'Modèle TVA 4'!RC[1]"
Selection.AutoFill Destination:=Range("F1:F" & DerLigne)
Sheets("Modèle TVA 4").Select
Range("A1").Select
ActiveCell.EntireRow.Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets("Modèle TVA 3").Select
Range("A1").End(xlDown).Offset(1).Select
ActiveSheet.Paste
Sheets("Modèle TVA 5").Select
Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
Sheets("Modèle TVA 3").Select
Range("A1").End(xlDown).Offset(1).Select
ActiveSheet.Paste
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.DisplayAlerts = False
Sheets("Modèle TVA 4").Delete
Sheets("Modèle TVA 5").Delete
Sheets.Add(After:=Worksheets(Worksheets.Count)).Name = "Modèle TVA " 'ajoute une Feuille devant la Feuille active et la nomme "NouvelleFeuille"
Sheets.Add(After:=Worksheets(Worksheets.Count)).Name = "Modèle TVA 2" 'ajoute une Feuille devant la Feuille active et la nomme "NouvelleFeuille"
With Sheets(1).Select
DernLigne = .Range("A" & .Rows.Count).End(xlUp).Row
If DernLigne < 1 Then Exit Sub
If DernLigne >= 1 Then
Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
With Sheets("Modèle TVA ").Select
Range("A1").Select
ActiveSheet.Paste
Range("B1").Select
Application.CutCopyMode = False
DernLigne = .Range("A" & .Rows.Count).End(xlUp).Row
If DernLigne = 1 Then ActiveCell.FormulaR1C1 = "607031"
If DernLigne > 1 Then
DerLigne = Range("A65536").End(xlUp).Row
ActiveCell.FormulaR1C1 = "607031"
Selection.AutoFill Destination:=Range("B1:B" & DerLigne)
Range("C1").Select
Application.CutCopyMode = False
If DernLigne = 1 Then ActiveCell.FormulaR1C1 = "ACI"
If DernLigne > 1 Then
DerLigne = Range("B65536").End(xlUp).Row
ActiveCell.FormulaR1C1 = "ACI"
Selection.AutoFill Destination:=Range("C1:C" & DerLigne)
Sheets(1).Select
Range("G1").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets("Modèle TVA ").Select
Range("D1").Select
ActiveSheet.Paste
Sheets(1).Select
Range("Q1").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Modèle TVA ").Select
Range("E1").Select
ActiveSheet.Paste
Sheets(1).Select
Range("I1").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Modèle TVA ").Select
Range("F1").Select
ActiveSheet.Paste
Sheets(1).Select
Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
With Sheets("Modèle TVA 2").Select
Range("A1").Select
ActiveSheet.Paste
Range("B1").Select
Application.CutCopyMode = False
DernLigne = .Range("A" & .Rows.Count).End(xlUp).Row
If DernLigne = 1 Then ActiveCell.FormulaR1C1 = "401000"
If DernLigne > 1 Then
DerLigne = Range("A65536").End(xlUp).Row
ActiveCell.FormulaR1C1 = "401000"
Selection.AutoFill Destination:=Range("B1:B" & DerLigne)
Range("C1").Select
Application.CutCopyMode = False
If DernLigne = 1 Then ActiveCell.FormulaR1C1 = "ACI"
If DernLigne > 1 Then
DerLigne = Range("B65536").End(xlUp).Row
ActiveCell.FormulaR1C1 = "ACI"
Selection.AutoFill Destination:=Range("C1:C" & DerLigne)
Sheets(1).Select
Range("G1").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets("Modèle TVA 2").Select
Range("D1").Select
ActiveSheet.Paste
Sheets(1).Select
Range("Q1").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Modèle TVA 2").Select
Range("E1").Select
ActiveSheet.Paste
Sheets(1).Select
Range("I1").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Modèle TVA 2").Select
Range("G1").Select
ActiveSheet.Paste
Sheets("Modèle TVA ").Select
Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
Sheets("Modèle TVA 2").Select
Range("A1").End(xlDown).Offset(1).Select
ActiveSheet.Paste
Application.DisplayAlerts = False
Sheets("Modèle TVA ").Delete

End Sub

Un grand merci par avance de votre aide
 

Pièces jointes

  • 007-VAT--FR70842167470.xls
    54 KB · Affichages: 19

Staple1600

XLDnaute Barbatruc
Bonjour Inès, le fil, le forum

Un exemple pour se simplifier la vie
(sans utiliser Autofill)
VB:
Sub Exemple()
Dim Formule1 As String, Formule2 As String
Dim DerLigne As Long
Formule1 = "='Modèle TVA 3'!RC[1]+'Modèle TVA 4'!RC[1]"
Formule2 = "='Modèle TVA 3'!RC*20%"
DerLigne = Sheets("Feuil1").Cells(Rows.Count, 1).End(3).Row
Sheets("Feuil1").Range("B2:B" & DerLigne).FormulaR1C1 = Formule1
Sheets("Feuil1").Range("G2:G" & DerLigne).FormulaR1C1 = Formule2
End Sub
A toi de voir, si cette façon de faire t'inspire ;)
 

Discussions similaires

Statistiques des forums

Discussions
311 734
Messages
2 082 020
Membres
101 872
dernier inscrit
Colin T