【问题标题】:Dynamic Range with Calculation带计算的动态范围
【发布时间】:2018-09-08 19:57:31
【问题描述】:

我想让我的范围动态化,同时仍然包含公式。有任何想法吗?它需要找到最后一行,并包括对两列所有行的计算。

 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

【问题讨论】:

  • 使用工作表中的偏移量创建一个动态命名范围,然后引用该 Range(“namedrange”).Formula = 或该主题的变体 .FormulaR1C1 ......请原谅有趣的“由于移动

标签: vba excel dynamic range


【解决方案1】:

尝试下面的代码将公式动态放置到最后一行:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-01
    • 1970-01-01
    相关资源
    最近更新 更多