所以今天收到了一个 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"& (count - 15) & ":H"& (count - 1) & ")"
那里的一切对我来说都很好,老实说,我不确定我必须改变什么。
编辑:我也忘了提到,在#NAME 错误中,公式显示正确,但是它在函数中添加了 ',如下所示: =SUM('H__':'H__')
最佳答案
试试 ActiveCell.Formula
而不是 ActiveCell.FormulaR1C1
....FormulaR1C1
适用于不同风格的寻址单元
ActiveCell.Formula = "=A1"
和
ActiveCell.FormulaR1C1 = "=R1C1"
指的是同一个单元格。
关于excel - 在 VB 问题中使用 Excel 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3735383/