FormulaR1C1 including variable - Application-Defined Error: Excel VBA
FormulaR1C1 including variable - Application-Defined Error: Excel VBA
我正在尝试在 Excel VBA 中进行计算,但出现错误
Application-Defined or Object-Defined Error
这是我要执行的代码。错误发生在 .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
试试这个
.FormulaR1C1 = "=IFERROR(SUM(RC[-8]+RC[-7]/RC[-4])*" & NoofDays & ",0)"
你的问题是这一行:
.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)"
我正在尝试在 Excel VBA 中进行计算,但出现错误
Application-Defined or Object-Defined Error
这是我要执行的代码。错误发生在 .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
试试这个
.FormulaR1C1 = "=IFERROR(SUM(RC[-8]+RC[-7]/RC[-4])*" & NoofDays & ",0)"
你的问题是这一行:
.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)"