Run a macro on a variable range

Solved
```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
```

3 answers

  1. Hello
    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
    0
    1. Hello,

      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!
      0
      1. 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 :)
        0
    2. Contributor
      Hello,

      If your question is: finding the last row of column A, this article will give you the solution

      http://www.excelabo.net/excel/derniere_cellule_vba

      --
      See you, Le Pivert
      0
      1. Yes, I had started experimenting with this solution, but, as you will see in my response to Gyrus, I cannot directly apply this solution.

        Thank you :)
        0
      2. Contributor
        And like this:

         ActiveCell.Range("A1:A" & [A65536].End(xlUp).Row - 2).Select
        1
      3. I finally succeeded!

        Sub recherchev()
        '
        ' recherchev Macro
        ActiveCell.FormulaR1C1 = "=VLOOKUP(RC1,Classeur1!Tableau1#Data,12,FALSE)"

        ActiveCell.AutoFill Destination:=ActiveCell.Range("A1:A" & ActiveCell.End(xlDown).Row - 8), Type:=xlFillValues
        ActiveCell.Range("A1:A" & ActiveCell.End(xlDown).Row - 6).Select
        Selection.Copy
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
        End Sub


        I 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!
        0