Run a macro on a variable range
Solved
RachelMartel
Posted messages
460
Status
Member
-
RachelMartel Posted messages 460 Status Member -
RachelMartel Posted messages 460 Status Member -
```vba
Sub recherchev()
' recherchev Macro
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC1,Classeur1!Tableau1#Data,12,FALSE)"
ActiveCell.Select
Dim lastRow As Long
lastRow = Cells(Rows.Count, 1).End(xlUp).Row
Selection.AutoFill Destination:=ActiveCell.Range("A1:A" & lastRow), Type:= _
xlFillValues
ActiveCell.Range("A1:A" & lastRow).Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
End Sub
```
Sub recherchev()
' recherchev Macro
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC1,Classeur1!Tableau1#Data,12,FALSE)"
ActiveCell.Select
Dim lastRow As Long
lastRow = Cells(Rows.Count, 1).End(xlUp).Row
Selection.AutoFill Destination:=ActiveCell.Range("A1:A" & lastRow), Type:= _
xlFillValues
ActiveCell.Range("A1:A" & lastRow).Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
End Sub
```
3 answers
Hello,
To test:
See you!
To test:
Sub recherchev()
Dim MaPlage As Range
Set MaPlage = Range(ActiveCell, ActiveCell.End(xlDown))
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC1,Classeur1!Tableau1#Data,12,FALSE)"
ActiveCell.AutoFill Destination:=MaPlage, Type:=xlFillValues
MaPlage.Copy
MaPlage.PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
End Sub
See you!
Sub recherchev()
Dim MaPlage As Range
Set MaPlage = Range(ActiveCell, ActiveCell.End(xlDown).Offset(-2, 0))
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC1,Classeur1!Tableau1#Data,12,FALSE)"
ActiveCell.AutoFill Destination:=MaPlage, Type:=xlFillValues
MaPlage.Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub
J'ai modifié un peu parce qu'il est important que toutes les cellules soient sélectionnées pour faire copier puis coller les valeurs. Pour ça, ça fonctionne.
Mon problème c'est qu'il faudrait que "MaPlage" s'arrête à la dernière cellule vide d'une autre colonne (la colonne A). Parce que en fait, l'endroit ou j'applique ma formule, ce sont toutes des cellules vides sauf le total en bas. Présentement, la macro s'arrête à mon total qui se fait écrasé. Est-ce qu'on pourrait l'arrêter 2 cellules au-dessus? J'ai essayé en ajoutant "- 2" après ActiveCell.End(xlDown) mais ça ne fonctionne pas.
Merci pour votre aide :)
Hello
maybe like this
A+
Maurice
maybe like this
Sub recherchev() ' recherchev Macro Nlig = Range("A" & Rows.Count).End(xlUp).Row ActiveCell.FormulaR1C1 = "=VLOOKUP(RC1,Classeur1!Tableau1#Data,12,FALSE)" ActiveCell.Select Selection.AutoFill Destination:=ActiveCell.Range("A1:A" & Nlig), Type:= _ xlFillValues ActiveCell.Range("A1:A" & Nlig).Select Selection.Copy Selection.PasteSpecial xlPasteValues Application.CutCopyMode = False End Sub A+
Maurice
Thank you :)
ActiveCell.Range("A1:A" & [A65536].End(xlUp).Row - 2).SelectI must admit I'm puzzled as to why I have to do -6 in one case and -8 in the other, considering it’s the same range... but at least it works! :)
Thank you!