How to copy down formula in Excel to last cell wit

2019-08-22 12:19发布

I have a code that will perform a formula and then place the value in Column G and then sort column G... The code works fine but I will be having a dynamically changed amount of row I need the formula to be ran for so I am trying to figure out how to change the range from just certain cells to all cells that have a value. Any help is appreciated. The code I have written is below.

Sub Sorting()
Worksheets.Select ("FedEx Air Ops Workbench Report")
Range("G4").Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-2],BUTTONS!R2C11:R6C12,2,FALSE)"
Selection.AutoFill Destination:=Range("G4:G58"), Type:=xlFillDefault
ActiveWorkbook.Worksheets("FedEx Air Ops Workbench Report").Sort.SortFields. _
    Add Key:=Range("G4:G58"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("FedEx Air Ops Workbench Report").Sort
    .SetRange Range("A4:G58")
    .Header = xlGuess
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With

End Sub

1条回答
虎瘦雄心在
2楼-- · 2019-08-22 13:18
Dim ws As Worksheet
Set ws = WorkSheets("FedEx Air Ops Workbench Report")
lastRow = ws.Cells(ws.Rows.Count, "G").End(xlUp).Row
Debug.Print lastRow
ws.Range("G4").FormulaR1C1 = "=VLOOKUP(RC[-2],BUTTONS!R2C11:R6C12,2,FALSE)"
ws.Range("G4").AutoFill Destination:=Range("G4:G" & lastRow), Type:=xlFillDefault

Does the above work?

查看更多
登录 后发表回答