VBA公式太长,缩短它的方法

时间:2016-11-17 13:50:22

标签: excel vba excel-vba

Goodafternoon all,

目前面临VBA问题,“记录宏”功能确实有效,但当我尝试重新运行时,它会给我1004错误。 我记得我可以做三件事:

  1. 将公式分成几部分,然后参考公式中的下一部分(即第2部分= abc + def + ghi,因此公式为123 + 3456 + ____,然后将____替换为第2部分)
  2. 将公式作为文本放在不同的单元格中,然后参考公式中的那些单元格(公式为单元格(“a1”)。value(?)+ cell(“b2”)。value(?)
  3. 为代码的某些部分指定一个特定的名称,以便缩短公式(即abc ='INPUT - MOTC!'然后在公式中引用abc
  4. 特别是第2部分是我可以使用的一些帮助,因为我不知道确切的参考,我总是可以回顾excel中的公式,而不是去VBA。

    公式本身是:

    ActiveCell.FormulaR1C1 = _
        "=IF(WEEKDAY(RC2)=6,COUNTIFS('INPUT - MOTC'!C90,""confirmed"",'INPUT - MOTC'!C101,""1"",'INPUT - MOTC'!C74,""500"",'INPUT - MOTC'!C86,""<>C"",'INPUT - MOTC'!C47,""<""&R2C11,'INPUT - MOTC'!C45,""<""&R2C12,'INPUT - MOTC'!C93,""No"",'INPUT - MOTC'!C105,""No"",'INPUT - MOTC'!C99,""No"",'INPUT - MOTC'!C47,RC2)" & _
        "+COUNTIFS('INPUT - MOTC'!C90,""confirmed"",'INPUT - MOTC'!C101,""1"",'INPUT - MOTC'!C74,""500"",'INPUT - MOTC'!C86,""<>C"",'INPUT - MOTC'!C47,""<""&R2C11,'INPUT - MOTC'!C45,""<""&R2C12,'INPUT - MOTC'!C93,""No"",'INPUT - MOTC'!C105,""No"",'INPUT - MOTC'!C99,""No"",'INPUT - MOTC'!C47,RC2+1)" & _
        "+COUNTIFS('INPUT - MOTC'!C90,""confirmed"",'INPUT - MOTC'!C101,""1"",'INPUT - MOTC'!C74,""500"",'INPUT - MOTC'!C86,""<>C"",'INPUT - MOTC'!C47,""<""&R2C11,'INPUT - MOTC'!C45,""<""&R2C12,'INPUT - MOTC'!C93,""No"",'INPUT - MOTC'!C105,""No"",'INPUT - MOTC'!C99,""No"",'INPUT - MOTC'!C47,RC2+2)" & _
        ",COUNTIFS('INPUT - MOTC'!C90,""confirmed"",'INPUT - MOTC'!C101,""1"",'INPUT - MOTC'!C74,""500"",'INPUT - MOTC'!C86,""<>C"",'INPUT - MOTC'!C47,""<""&R2C11,'INPUT - MOTC'!C45,""<""&R2C12,'INPUT - MOTC'!C93,""No"",'INPUT - MOTC'!C105,""No"",'INPUT - MOTC'!C99,""No"",'INPUT - MOTC'!C47,RC2)"
    

    我做错了什么,或者公式太长了?

    (小背景,我想将星期六和周日的价值添加到周五的价值,所以如果工作日是6,那么就这样做。)

    提前致谢:)

2 个答案:

答案 0 :(得分:3)

这个公式完全不可维护。永远不要再这样做了!

我个人从不使用VBA在单元格中设置公式。我总是在VBA中进行计算,只在单元格中显示结果。这样做的缺点是不能进行“实时”计算并需要触发器(如按下按钮),但这样可以更容易维护。

我建议您在VBA中移动所有计算。

  • 单个输入单元格需要命名为范围

name

  • 使用x = Range("OrderDate").Value2
  • 将值拉入VBA
  • 如果您有一个比您知道大小的表,则可以将值拉入VBA数组。

例如,使用100×7表:

Dim table_values() as Variant
table_values = Range("top_left_cell").Resize(100,7).Value2
' to loop through values
For i=1 to 100
    table_values(i,7) = ...
Next i
' Replace the values with new calculated values
Range("top_left_cell").Resize(100,7).Value2 = table_values
  • 让您按步骤分享复杂的计算

例如:

Set r = Range("table_name")
For i = 1 To n
    'Find the column with the date
    d = r.Offset(i - 1, 2).Value2
    If WorksheetFunction.Weekday(d) Then
        ' If weekday count items with ...
        n_confirmed = WorksheetFunction.CountIfs( '...
        n_shipped = WorksheetFunction.CountIfs( '...
    Else
        ' otherwise count  ...
        n_confirmed = WorksheetFunction.CountIfs( '...
        n_queue = WorksheetFunction.CountIfs( '...
    End If
    ' Fill the result
    r.Offset(i - 1, 3).Value2 = n_confirmed + n_shipped + n_queue
Next i

答案 1 :(得分:1)

您可以缩短这样的公式:

ActiveCell.FormulaR1C1 = _
    "=IF(WEEKDAY(RC2)=6,SUM(COUNTIFS('INPUT - MOTC'!C90,""confirmed"",'INPUT - MOTC'!C101,""1"",'INPUT - MOTC'!C74,""500"",'INPUT - MOTC'!C86,""<>C"",'INPUT - MOTC'!C47,""<""&R2C11,'INPUT - MOTC'!C45,""<""&R2C12,'INPUT - MOTC'!C93,""No"",'INPUT - MOTC'!C105,""No"",'INPUT - MOTC'!C99,""No"",'INPUT - MOTC'!C47,RC2+{0,1,2}))" & _
    ",COUNTIFS('INPUT - MOTC'!C90,""confirmed"",'INPUT - MOTC'!C101,""1"",'INPUT - MOTC'!C74,""500"",'INPUT - MOTC'!C86,""<>C"",'INPUT - MOTC'!C47,""<""&R2C11,'INPUT - MOTC'!C45,""<""&R2C12,'INPUT - MOTC'!C93,""No"",'INPUT - MOTC'!C105,""No"",'INPUT - MOTC'!C99,""No"",'INPUT - MOTC'!C47,RC2))"