Goodafternoon all,
目前面临VBA问题,“记录宏”功能确实有效,但当我尝试重新运行时,它会给我1004错误。 我记得我可以做三件事:
特别是第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,那么就这样做。)
提前致谢:)
答案 0 :(得分:3)
这个公式完全不可维护。永远不要再这样做了!
我个人从不使用VBA在单元格中设置公式。我总是在VBA中进行计算,只在单元格中显示结果。这样做的缺点是不能进行“实时”计算并需要触发器(如按下按钮),但这样可以更容易维护。
我建议您在VBA中移动所有计算。
x = Range("OrderDate").Value2
例如,使用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))"