如果M列中的单元格不是空白,我试图在N列中添加一个excel公式,目前我有下面的代码,它将从N2添加到N7302但是我希望这个公式基于我所拥有的标准范围提到了,请帮忙。
Range("N2").Select
ActiveCell.FormulaR1C1 = _
"=IF(IFERROR(VLOOKUP(RC[-11],'Service ID Master List'!C[-11],1,0),""Fail"")=""Fail"",""Check SESE_ID"",IF(IFERROR(VLOOKUP(RC[-9],Rules!C[-13],1,0),""Fail"")=""Fail"",""Check SESE_RULE"",IF(AND(RC[-5]<>"" "",IFERROR(VLOOKUP(RC[-5],Rules!C[-13],1,0),""Fail"")=""Fail""),""Check SESE_RULE_ALT"",IF(IFERROR(VLOOKUP(RC[-11],'Service ID Master List'!C3:C6,4,0),""Fail"")=""Fail"",""Check SEPY_ACCT_CAT"",IF(RC[-7]=""TBD"",""Check SEPY_ACCT_CAT"",""Pass"")))))"
Range("N2").Select
Selection.AutoFill Destination:=Range("N2:N7302")
答案 0 :(得分:0)
您可以在一个步骤中将公式添加到多个单元格,从而避免循环。要识别非空单元格,可以使用SpecialCells
,再次避免需要循环。
像这样的东西
Sub Demo()
Dim sFormula As String
Dim rng As Range
Dim ws As Worksheet
sFormula = "YourFormulaString"
Set ws = ActiveSheet
With ws
Set rng = .Range("M2")
Set rng = .Range(rng, .Cells(.Rows.Count, rng.Column).End(xlUp))
End With
rng.SpecialCells(xlCellTypeConstants).Offset(0, 1).FormulaR1C1 = sFormula
' If needed, uncomment next line
'rng.SpecialCells(xlCellTypeFormulas).Offset(0, 1).FormulaR1C1 = sFormula
End Sub
上面的代码假设M列中的单元格包含常量。如果需要,您可以使用xlCellTypeFormulas