用多个单元格中的变量编写公式的宏

时间:2015-09-14 20:52:06

标签: excel vba excel-vba

我正在尝试创建一个用条件格式更新图形的甘特图,但为了做到这一点,我需要编写一个在每个单元格中都有变化的公式,我已经写了一个宏来做它并且它有效,但是在写完之后,每个单元都需要双击才能使它工作,双击后它会自动更新,所以启用了自动计算,如果我使用Text to Columns,我可以使它们工作,但是它们大约有650列,自动执行的宏不起作用。

建议将不胜感激。

这是我正在使用的代码:

Sub formula_writer()
Row = 2
col = 9
i = 0
row2 = ActiveCell.Row
x = 0
    While i <= 100
        If i >= 99 Then
            x = x + 1
            ActiveSheet.Range("i9").Offset(x, 0).Select
            i = 0
            col = 9
            row2 = row2 + 1
        End If
        If x = 20 Then
            Exit Sub
        Else
            aCell = Cells(Row, col).Address(RowAbsolute:=True, ColumnAbsolute:=True)
            Selection.Formula = "" & "=SI" & "($A$" & row2 & " <> " & Chr(34) & Chr(34) & " , " & aCell & " , " & Chr(34) & Chr(34) & ")"
            ActiveCell = ActiveCell.Offset(0, 1).Select
            i = i + 1
            col = col + 1
        End If
    Wend
i = 0
x = 0
End Sub

1 个答案:

答案 0 :(得分:0)

您可以简化代码,但主要问题是代码中的公式(无论是R1C1还是A1)应使用公式名称的英文版本。试试这个更新:

Sub formula_writer()

Application.ScreenUpdating = False

Range("I10:DD28").numberformat="General"
Range("I10:DD28").FormulaR1C1 = "=IF(R[10]C1<>"""",R2C,"""")"

Application.Calculate
Application.ScreenUpdating = True
End Sub