我正在尝试在Excel VBA中进行计算,但是我收到了错误
应用程序定义或对象定义错误
这是我试图执行的代码。错误发生在.FormulaR1C1 = ...
行,但我不确定原因。
Function CalcNumDays()
Dim d1 As Date, d2 As Date, NoofDays As Variant
d1 = "01/01/2017"
d2 = "03/01/2017"
NoofDays = Application.WorksheetFunction.NetworkDays(d1, d2)
With Sheets("ALL")
With .Range("K2:K" & .Cells(.Rows.Count, "A").End(xlUp).Row)
.FormulaR1C1 = "=IFERROR(SUM((RC[-8]+RC[-7]/RC[-4])*NoofDays,0)"
.Value = .Value
End With
End With
End Function
答案 0 :(得分:2)
试试这个
.FormulaR1C1 = "=IFERROR(SUM(RC[-8]+RC[-7]/RC[-4])*" & NoofDays & ",0)"
答案 1 :(得分:1)
您的问题是这一行:
.FormulaR1C1 = "=IFERROR(SUM((RC[-8]+RC[-7]/RC[-4])*NoofDays,0)"
你想要包含变量NoofDays
,所以这必须落在字符串之外,使用&符号连接字符串&
.FormulaR1C1 = "=IFERROR(SUM((RC[-8]+RC[-7]/RC[-4])*" & NoofDays & ",0)"
您还缺少一个括号来结束SUM
,因此最终更正
.FormulaR1C1 = "=IFERROR(SUM((RC[-8]+RC[-7]/RC[-4])*" & NoofDays & "),0)"