【问题标题】:Assigning a Cell Number Using A Variable (Not Cell Value)使用变量分配单元格编号(不是单元格值)
【发布时间】:2019-07-20 10:02:12
【问题描述】:

我需要根据几个包含可变数据的工作簿上的相关单元格的值来定位给定的单元格编号(例如 C100)。一旦我定义了两个这样的单元格编号——一个范围——我将需要这些单元格中包含的值的平均值。

这里是设置,可以正常工作。


'   Perform "FIND" function for the first case of "LOCAL PEAK". Note: Since column D is composed of the above formula, the
'   find function only recognizes the VALUES in column E
    Set found = ActiveSheet.Cells.Find(What:="LOCAL PEAK", After:=[e1], LookIn:=xlValues, _
        LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=True)

' Assign the row number associated with the first instance of "LOCAL PEAK" to the variable "StaticRow"
    If Not found Is Nothing Then StaticRow = found.Row

' Assign the value in the cell of the force column associated with the first instance of "LOCAL PEAK" to the variable "SForce"
    SForce = Cells(StaticRow, 3).Value

' Assign the value in the cell of the position column associated with the first instance of "LOCAL PEAK" to the variable "InitialPos"
    InitialPos = Cells(StaticRow, 2).Value

' Assign the rows for Kinetic Friction calculation
    KineticRowA = StaticRow + 60
    KineticRowB = StaticRow + 300

继续我尝试但不起作用的方法:

    CellA = Cells(KineticRowA, 3)
    CellB = Cells(KineticRowB, 3)

KForce = WorksheetFunction.Average(Range(CellA, CellB))

上述方法将 VALUES 分配给变量 CellA 和 CellB,而不是单元格位置。

下一个完全不起作用:)

KForce = WorksheetFunction.Average(Range("CKineticRowA:CKineticRowB"))

谢谢!

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    如果您为 Range 变量赋值,则缺少 Set

    Dim CellA As Range, CellB As Range, ws As Worksheet
    
    Set ws = ActiveSheet
    Set CellA = ws.Cells(KineticRowA, 3)
    Set CellB = ws.Cells(KineticRowB, 3)
    
    KForce = WorksheetFunction.Average(ws.Range(CellA, CellB))
    

    【讨论】:

      猜你喜欢
      • 2014-05-07
      • 2019-09-11
      • 1970-01-01
      • 2019-01-23
      • 1970-01-01
      • 2022-06-26
      • 2020-11-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多