我正在编写一个宏,它将条件公式添加到多个工作表上,标题为Hazard 1 / Hazard 2 / Hazard 3 / Hazard 4.它设置为遍历工作表,每个工作表都有自己的命名范围只有前缀改变的地方(H1Ans1Rng,H2Ans1Rng,H3Ans1Rng,H4Ans1Rng等)。我需要代码在每个工作表的公式中写入正确的命名范围。我有一个代码可以做到这一点,但它很笨重,现在我得到一个“程序太大”的错误。客户给我申请的公式比下面的例子大得多且复杂得多。
If Wks.Name = "Hazard 1" Then Rng.FormulaR1C1 = "=SUM(H1Ans1Rng+H1Ans2Rng)"
If Wks.Name = "Hazard 2" Then Rng.FormulaR1C1 = "=SUM(H2Ans1Rng+H2Ans2Rng)"
If Wks.Name = "Hazard 3" Then Rng.FormulaR1C1 = "=SUM(H3Ans1Rng+H3Ans2Rng)"
If Wks.Name = "Hazard 4" Then Rng.FormulaR1C1 = "=SUM(H4Ans1Rng+H4Ans2Rng)"
答案 0 :(得分:0)
有些盲注我会像下面那样
Dim hazardNr As String
hazardNr = Replace(Wks.Name, "Hazard ", "")
rng.Formula = "=SUM(H" & hazardNr & "Ans1Rng,H" & hazardNr & "Ans2Rng)" & ")"