Etendre formules range variable

Bonjour à tous,

J'essaye d'étendre des formules en VBA mais avec des lignes variables, mon code ne fonctionne pas et active le debug sur la derniere ligne de mon code, une idee?

 Sub etirer_formules()

Dim wsbdd As Worksheet
Dim wsmaj As Worksheet
Dim wsmaj2 As Worksheet
Dim flag As Boolean

Set wsbdd = ActiveWorkbook.Sheets("BDD")
Set wsmaj = ActiveWorkbook.Sheets("MAJ")
Set wsmaj2 = ActiveWorkbook.Sheets("MAJ2")

wsbdd.Range("R1").Select
Selection.End(xlDown).Select
    Range(Selection, Selection.End(xlToRight)).Select
premiere_ligne = Selection

p = 1

While Not IsEmpty(wsbdd.Cells(p, 1)) = True
    If Not IsEmpty(wsbdd.Cells(p, 1)) = True Then
        p = p + 1
    End If
Wend

p = p - 1

derniere_ligne = wsbdd.Range(Cells(p, 18), Cells(p, 34))

premiere_ligne.AutoFill Destination:=Range(premiere_ligne & derniere_ligne)

End Sub

Hello,

essaye ceci pour voir :

premiere_ligne.AutoFill Destination:=union(Range(premiere_ligne),range(derniere_ligne))

Bonne journée.

Hello Bigdaddy

merci pour ta reactivité!

Malheureusement, cela ne fonctionne toujours pas

bug 1004: method range of object _global failed

une idee?

Bonsoir whereisbryan,

Essaye : premiere_ligne.AutoFill Destination:=Range(premiere_ligne & ":" & derniere_ligne)

dhany

Bonjour,

Merci dhany je vais tester ta solution!

J'ai entre temps trouver autre chose qui fonctionne très bien mais est un peu plus long.

Je le partage afin d'apporter une solution éventuel à un autre participant du forum:

Sub etirer_formules()

Dim wsbdd As Worksheet
Dim wsmaj As Worksheet
Dim wsmaj2 As Worksheet
Dim flag As Boolean

Set wsbdd = ActiveWorkbook.Sheets("BDD")
Set wsmaj = ActiveWorkbook.Sheets("MAJ")
Set wsmaj2 = ActiveWorkbook.Sheets("MAJ2")

q = 1
While Not IsEmpty(wsbdd.Cells(q, 18)) = True
    If Not IsEmpty(wsbdd.Cells(q, 18)) = True Then
        q = q + 1
    End If
Wend

q = q - 1

p = 1

While Not IsEmpty(wsbdd.Cells(p, 1)) = True
    If Not IsEmpty(wsbdd.Cells(p, 1)) = True Then
        p = p + 1
    End If
Wend

p = p - 1

Range(wsbdd.Cells(q, 18), wsbdd.Cells(q, 34)).Select
Selection.AutoFill Destination:=Range(wsbdd.Cells(q, 18), wsbdd.Cells(p, 34))

End Sub

Bonjour whereisbryan,

Je te propose d'ajouter , q&, p& au bout de Dim wsbdd As Worksheet et d'enlever tout ceci (car non utilisé dans ta sub) :

Dim wsmaj As Worksheet
Dim wsmaj2 As Worksheet
Dim flag As Boolean

Set wsmaj = ActiveWorkbook.Sheets("MAJ")
Set wsmaj2 = ActiveWorkbook.Sheets("MAJ2")

Ensuite, remplace :

q = 1
While Not IsEmpty(wsbdd.Cells(q, 18)) = True
    If Not IsEmpty(wsbdd.Cells(q, 18)) = True Then
        q = q + 1
    End If
Wend

par :

q = 1
Do While Not IsEmpty(wsbdd.Cells(q, 18)): q = q + 1: Loop

idem pour l'autre bloc de code similaire, remplace :

p = 1
While Not IsEmpty(wsbdd.Cells(p, 1)) = True
    If Not IsEmpty(wsbdd.Cells(p, 1)) = True Then
        p = p + 1
    End If
Wend

par :

p = 1
Do While Not IsEmpty(wsbdd.Cells(p, 1)): p = p + 1: Loop

J'ai pas testé mais j'crois qu'ça devrait marcher !

dhany

Merci Dhany,

Oui j utilise dans mon code les autres mais je les avais enlevé ici puisque pas utile dans ma question.

En tout cas merci pour la méthode, je vais aller plus vite avec ca, je ne connaissais pas!

Rechercher des sujets similaires à "etendre formules range variable"