首页 > 解决方案 > 如何将动态/VBA 生成的范围放入单元格的公式中?

问题描述

假设我们有这个代码

Dim runningTotal As Range

reference = Range("A4").Value

ThisWorkbook.Sheets(7).Range("A:JL").FormatConditions.Delete ' Delete current scales

For Each c In Worksheets("PVP Calculator").Range("A7:JL1506").Cells
 '   If c.Interior.colorIndex <> 0 Then
  '      c.Interior.colorIndex = 0
   ' End If
    If c.Value = reference Then
'       c.Interior.colorIndex = 6
        If runningTotal Is Nothing Then
            Set runningTotal = c
        Else
            Set runningTotal = Union(runningTotal, c)
        End If
    End If
Next

如何使单元格 B4 显示 runningTotal 范围内的最大值?做类似的事情是Range("B4").Value = MAX(runningTotal)行不通的。

(起初单元格是相等的,但后来我更改了另一个单元格以使工作表的其余部分具有不同的值。此外,我打算更改c.Value = reference Then为容差,我想这样吗?c.Value >= reference-5 And c.Value <= reference+5 Then

提前致谢。我花了半天时间没有使用正确的关键字搜索和浏览 MSDN 来弄清楚如何做我想做的事。

标签: excel

解决方案


我得到了答案,因为我在提交问题后继续搜索。它是Range("B4").Formula = "=MAX(" & runningTotal.Address & ")"。我一开始在 & 连接中失败了,直到现在都忽略了 Address 属性


推荐阅读