excel - 在 VB 问题中使用 Excel 函数

标签 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"& (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/

相关文章:

javascript - 科学计数法手机号码 - Excel Javascript

vba - 捕获/读取 Excel 应用程序状态栏中显示的消息

excel - 动态平均值前 9 个整数中最低的 4 个

excel - 从具有混合数据类型的范围中返回仅包含数字的数组

vba - 如何在 Excel 中打开特定版本的 Word 2007/2010

regex - 使用正则表达式将编号列表数组拆分为多行编号列表

excel - 如何将 Cells().Select 命令中的单元格的值设置为其中包含公式的单元格的值?

javascript - 在 Office 加载项中启用扩展错误日志记录(查看错误对象的完整说明)

excel - 如何计算 YYYYMMDDHHMMSSXXX 格式的文本时间差(包括毫秒)

vba - 使用 Instr 和单元格格式时出现问题