VBA设置为“= RC ”

我得到了列T填补了一个公式,返回一个双。 例如,T8的公式= S8 / K8(但这不是太重要)。

你可以通过查看来理解代码,但是我的问题来自years = "=RC[14]" ,它返回一个types不匹配的错误。

 Dim years As Double For Each Cell In Range("$F$8:$F$" & lastrow) years = "=RC[14]" If years = 0 Then ActiveCell.FormulaR1C1 = "No inventory" End If If years < 1 And years <> 0 Then ActiveCell.FormulaR1C1 = "Less than a year's worth of inventory" End If If years > 1 Then If Int(years) = 1 Then ActiveCell.FormulaR1C1 = "No Activity Required - OH qty exceeds EAU 1 year" Else: ActiveCell.FormulaR1C1 = "No Activity Required - OH qty exceeds EAU " & Int(years) & "years" End If End If Next 

此外,我知道我的If-Else嵌套可能更清洁,但我对VBA不是很有经验,这对我来说是一个非常简单的方法。

谢谢您的帮助!

编辑:

我现在必须select我想编辑的单元格的范围:

 Range("$F$8:$F$" & lastrow).Select For Each wCell In Range("$F$8:$F$" & lastrow) years = wCell.Offset(0, 14).Value If years = 0 Then ActiveCell.FormulaR1C1 = "No inventory" End If If years < 1 And years <> 0 Then ActiveCell.FormulaR1C1 = "Less than a year's worth of inventory" End If If years > 1 Then If Int(years) = 1 Then ActiveCell.FormulaR1C1 = "No Activity Required - OH qty exceeds EAU 1 year" Else: ActiveCell.FormulaR1C1 = "No Activity Required - OH qty exceeds EAU " & Int(years) & "years" End If End If Next 

你正在设置一个string值的double。 您需要使用Range对象引用您的范围。 要进行相对参考,请使用“偏移”function。 请不要使用Cell作为variables名称,因为这是Excel中的保留字。

相应地修改:

 Dim years As Double Dim wCell As Range For Each wCell In Range("$F$8:$F$" & lastrow) years = wCell.Offset(0,14).Value