我想让我的范围动态,同时仍然包括公式。有任何想法吗?它需要找到最后一行,并包括两列所有行的计算。
Sheets("Opérations").Activate
Range("AS3").Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-6],Time!R1C1:R16C2,2,TRUE)"
Selection.AutoFill Destination:=Range("AS3:AS8655")
Range("AS3:AS8655").Select
Range("AT3").Select
ActiveCell.FormulaR1C1 = _
"=IF(Time!R[-2]C[-35]<>Maturities!R3C17,VLOOKUP(RC[-7],Time!R1C4:R2585C5,2,FALSE),VLOOKUP(RC[-7],Time!R1C7:R2585C8,2,FALSE))"
Range("AT3").Select
Selection.AutoFill Destination:=Range("AT3:AT8655")
Range("AT3:AT8655").Select
答案 0 :(得分:0)
尝试下面的代码动态地将公式放到最后一行:
Option Explicit
Sub PopulateRangeWithFormulaString()
Dim Sht As Worksheet
Dim VlookupRng As Range
Dim FormulaStr As String
Dim LastRow As Long
Dim LastCell As Range
' set the Vlookup Range
Set VlookupRng = ThisWorkbook.Sheets("Time").Range("A1:B16")
' set the worksheet object
Set Sht = ThisWorkbook.Sheets("Op")
With Sht
FormulaStr = "=VLOOKUP(" & .Range("AS3").Offset(, -6).Address(False, False, xlA1) & "," & VlookupRng.Address(True, True, xlA1, xlExternal) & ",2,TRUE)"
Debug.Print FormulaStr ' <-- helps to debug the Formula String
' Using Find to find the last row in a worksheet
Set LastCell = .Cells.Find(What:="*", After:=.Cells(1), Lookat:=xlPart, LookIn:=xlFormulas, _
SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False)
If Not LastCell Is Nothing Then
LastRow = LastCell.Row
Else
MsgBox "Error! worksheet is empty", vbCritical
End
End If
' populate the entire range with the formula
.Range("AS3:AS" & LastRow).Formula = FormulaStr
End With
End Sub