在VB问题中使用Excel函数

时间:2010-09-17 12:43:35

标签: excel vba

所以今天收到了一份excel电子表格,上面有超过20,000个单元格。我不得不按名称将它们分开(永远14行开始一个新客户),并总计价格列。所以,我写道:

Sub NewSpace()
'
' NewSpace Macro
'
' Keyboard Shortcut: Ctrl+p
'
        'create the count variable, initilize it to 17
        Dim count As Integer
        count = 17

    While count <= 30003

            'add first row
            Rows(count & ":" & count).Select
            Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove

            'add second row
            Rows(count & ":" & count).Select
            Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove

            'Select cell A in the first created row, write "Total", and format properly
            Range("A" & count).Select
            ActiveCell.FormulaR1C1 = "Total"
            With ActiveCell.Characters(Start:=1, Length:=5).Font
                    .Name = "Calibri"
                    .FontStyle = "Bold"
                    .Size = 11
                    .Strikethrough = False
                    .Superscript = False
                    .Subscript = False
                    .OutlineFont = False
                    .Shadow = False
                    .Underline = xlUnderlineStyleNone
                    .ThemeColor = xlThemeColorLight1
                    .TintAndShade = 0
                    .ThemeFont = xlThemeFontMinor
            End With

            'Select cell H in the first created row, sum up the values
            Range("H" & count).Select
            ActiveCell.FormulaR1C1 = "=sum(H" & (count - 15) & ":H" & (count - 1) & ")"
            With ActiveCell.Characters(Start:=1, Length:=5).Font
                    .Name = "Calibri"
                    .FontStyle = "Bold"
                    .Size = 11
                    .Strikethrough = False
                    .Superscript = False
                    .Subscript = False
                    .OutlineFont = False
                    .Shadow = False
                    .Underline = xlUnderlineStyleNone
                    .ThemeColor = xlThemeColorLight1
                    .TintAndShade = 0
                    .ThemeFont = xlThemeFontMinor
            End With


    'increment the counter by 16, start again
    count = count + 16

    Wend
End Sub

但是,我在此行上一直收到#NAME错误: ActiveCell.FormulaR1C1 =“= sum(H”&amp;(count-15)&amp;“:H”&amp;(count-1)&amp;“)”

那里的一切看起来都很好,说实话,我不确定我要改变什么。

编辑:我也忘了提及,在#NAME错误中,公式显示正确,但它添加'到函数,如下:= SUM('H __':'H __')

1 个答案:

答案 0 :(得分:3)

尝试使用ActiveCell.Formula代替ActiveCell.FormulaR1C1 ...

.FormulaR1C1适用于不同类型的寻址单元

ActiveCell.Formula = "=A1"

ActiveCell.FormulaR1C1 = "=R1C1"

指的是同一个细胞。