在VB问题中使用Excel函数

所以今天收到一个超过20,000个单元格的Excel电子表格。 我不得不通过名称来分开它们(有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”&(count – 15)&“:H”&(count – 1)&“)”

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

编辑:我也忘了提及,在#NAME错误,公式显示正确,但它增加了“的function,如下所示:= SUM('H __':'H__')

尝试ActiveCell.Formula而不是ActiveCell.FormulaR1C1

.FormulaR1C1是针对不同风格的单元格

 ActiveCell.Formula = "=A1" 

 ActiveCell.FormulaR1C1 = "=R1C1" 

指的是同一个单元格。