【问题标题】:Create Formula based on cells根据单元格创建公式
【发布时间】:2014-01-17 19:23:35
【问题描述】:

我需要创建一个基于单元格的匹配公式,如下例所示:

+------+--------+--------+
| Col1 | SumCol | Val    | 
+------+--------+--------+
|    1 |      8 | v1     | '=Match([otherWorkbook]MainSheet!C1,RC3,[otherWorkbook]MainSheet!C8)
|    2 |      5 | v44*   | '=Match([otherWorkbook]MainSheet!C2,RC3,[otherWorkbook]MainSheet!C5)
|    3 |      7 | ls*    | '=Match([otherWorkbook]MainSheet!C3,RC3,[otherWorkbook]MainSheet!C7)
+------+--------+--------+

要构建公式,我需要获取字符串“[otherWorkbook]MainSheet”并与列 Col1 中的单元格连接。然后我需要从列 Val 中获取值。最后,我需要再次将字符串“[otherWorkbook]MainSheet”与列 SumCol 中的单元格连接起来。

我期望像=Match([otherWorkbook]MainSheet!C1,RC3,[otherWorkbook]MainSheet!C8) 这样的公式。我可以使用两个临时列进行连接,然后构建 wole 公式。

PS:我正在使用 VBA 来制作公式,所以我可以用它来达到我的目标。

【问题讨论】:

  • 如果您在 vba 中只是将单元格的值合并到公式字符串中...
  • 但我有 5000 行要填写。如果我循环女巫线,宏将花费太长时间来完成任务。这就是为什么我想创建公式然后AutoFill() - 或者只是Range(...).FormulaR1C1 =
  • 没有别的办法。

标签: vba excel excel-formula


【解决方案1】:

也许你需要类似 this 的东西:

Sub AddFormulas()

    myOtherWorkbook = "[otherWorkbook]"

    myFormula1 = "=Match(" & myOtherWorkbook & "MainSheet!C"
    myFormula2 = ",RC3," & myOtherWorkbook & "MainSheet!C"

    lastRow = Cells(Rows.Count, 1).End(xlUp).Row
    For x = 2 To lastRow  'Loops from row 2 to the last of column "A"

        Cells(x, 4).Formula = myFormula1 & Cells(x, 1) & myFormula2 & Cells(x, 2) & ")"

    Next x


End Sub

【讨论】:

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