【问题标题】:replacing {MAX(IF())} array formula with vba用 vba 替换 {MAX(IF())} 数组公式
【发布时间】:2022-01-24 05:01:39
【问题描述】:

尝试在 VBA 中复制以下内容以提高运行时间(有成千上万行)

我基本上想获得每个唯一符号的最大值 已经拥有作为命名范围的唯一符号、符号和值

当前数组公式是

{=MAX(IF(symbols=D2, values, 0))}

每个单元格

【问题讨论】:

  • 我不会说它不会表现得更好,但公式和 Excel 引擎的性能通常比 VBA 高得多。
  • @Skin 视情况而定。
  • @ch2831 为什么不与数据共享您的工作簿?当前的运行时间是什么,您的目标是什么?你有多需要它?这是您需要定期在不同数据集上使用的东西吗?如果这个任务是一次性的,你最好让它慢慢计算,然后用结果值替换公式。

标签: arrays excel vba excel-formula


【解决方案1】:

一个 VBA MaxIf(公式)

  • 调整常量部分中的值。
Option Explicit

Sub VBAMaxIf()
    
    Const sAddress As String = "symbols"
    Const vAddress As String = "values"
    Const fRow As Long = 2
    Const uCol As String = "D"
    Const dCol As String = "E" ' adjust the destination (resulting) column!
    
    Dim ws As Worksheet: Set ws = ActiveSheet
    
    Dim lRow As Long: lRow = ws.Cells(ws.Rows.Count, uCol).End(xlUp).Row
    If lRow < fRow Then Exit Sub ' no data
    
    Dim urg As Range
    Set urg = ws.Range(ws.Cells(fRow, uCol), ws.Cells(lRow, uCol))

    Dim drg As Range: Set drg = urg.EntireRow.Columns(dCol)
    Dim dFormula As String: dFormula = "=MAX(IF(" _
        & ws.Range(sAddress).Address & "=" _
        & ws.Cells(fRow, uCol).Address(0, 0) & "," _
        & ws.Range(vAddress).Address & ",0))"
    
    Application.Calculation = xlCalculationManual
    
    drg.Formula = dFormula
    drg.FormulaArray = drg.FormulaR1C1
    drg.Value = drg.Value
 
    Application.Calculation = xlCalculationAutomatic

End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-13
    • 2019-06-23
    • 1970-01-01
    相关资源
    最近更新 更多