【问题标题】:Adding to a formula in cell without deleting formula添加到单元格中的公式而不删除公式
【发布时间】:2015-03-27 13:33:48
【问题描述】:

假设单元格 A5 中有以下公式:=SUM(A1:A5)

我想用 VBA 给这个公式加 1 使它成为=SUM(A1:A5) + 1

我尝试过Range("A5") = Range("A5") + 1,但这只是将其更改为硬编码数字而不是公式+ 1。我该如何解决这个问题?

感谢您的帮助!

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    类似这样的东西,包括对公式存在的测试

    If Range("A5").HasFormula Then Range("A5").Formula = Range("A5").Formula & "+1"
    

    更新:要在大量单元格上快速执行此操作,自动更新您应该使用的公式variant arrays

    更改 strAdd = "+1" 为您的后缀附加

    'Press Alt + F11 to open the Visual Basic Editor (VBE)
    'From the Menu, choose Insert-Module.
    'Paste the code into the right-hand code window.
    'Press Alt + F11 to close the VBE
    'In Xl2003 Goto Tools … Macro … Macros and double-click UpdateFormulae
    
    Sub UpdateFormulae()
        Dim rng1 As Range
        Dim rngArea As Range
        Dim lngRow As Long
        Dim lngCol As Long
        Dim lngCalc As Long
        Dim X()
        Dim strAdd As String      
    
    
        On Error Resume Next
        Set rng1 = Application.InputBox("Select range for the replacement of forumlae", "User select", Selection.Address, , , , , 8)
        Set rng1 = rng1.SpecialCells(xlCellTypeFormulas)
        If rng1 Is Nothing Then Exit Sub
        On Error GoTo 0
    
        strAdd = "+1"
    
       'Speed up the code by turning off screenupdating and setting calculation to manual
       'Disable any code events that may occur when writing to cells
        With Application
            lngCalc = .Calculation
            .ScreenUpdating = False
            .Calculation = xlCalculationManual
            .EnableEvents = False
        End With
    
        'Test each area in the user selected range
    
        'Non contiguous range areas are common when using SpecialCells to define specific cell types to work on
        For Each rngArea In rng1.Areas
            'The most common outcome is used for the True outcome to optimise code speed
            If rngArea.Cells.Count > 1 Then
               'If there is more than once cell then set the variant array to the dimensions of the range area
               'Using Value2 provides a useful speed improvement over Value. On my testing it was 2% on blank cells, up to 10% on non-blanks
                X = rngArea.Formula
                For lngRow = 1 To rngArea.Rows.Count
                    For lngCol = 1 To rngArea.Columns.Count
                        'replace the leading zeroes
                        X(lngRow, lngCol) = X(lngRow, lngCol) & strAdd
                    Next lngCol
                Next lngRow
                'Dump the updated array with new formulae back over the initial range
                rngArea.Value2 = X
            Else
                'caters for a single cell range area. No variant array required
                rngArea.Value2 = rngArea.Formula & strAdd
            End If
        Next rngArea
    
        'cleanup the Application settings
        With Application
            .ScreenUpdating = True
            .Calculation = lngCalc
            .EnableEvents = True
        End With
    End Sub
    

    【讨论】:

    • 很难与回答如此之快的 Excel MVP 和 VBA 专家竞争 :) - +1 btw
    • 一个问题:为什么是“rngArea.Value2 = X”,而不是“rngArea.Formula = X”?两者似乎工作方式相同,但我原以为 Value2 会将单元格内容转换为 ... 值。
    • @DougGlancy (和 JMax),谢谢你的赞美,非常感谢 :) Doug,关于你的问题,虽然我发现使用 Value2 比使用数组的公式稍微快一点(也许 1% 左右) 这在我上面的代码中是无意的。我改编了一篇文章(在超链接中)的代码,对其进行了测试,然后发布了,但我只将单个单元格范围行从 value2 更改为公式 rngArea.Formula = rngArea.Formula & strAdd。我将更改此行以避免不必要的外观差异
    • 我认为关于前导零的 cmets 也是退化的。一如既往地感谢您增加我的知识。
    • @DougGlancy - 是的,仅在 10 分钟前编辑了那个。你当然对细节非常敏锐!
    【解决方案2】:
    Range("A5").Formula = Range("A5").Formula & "+1"
    

    【讨论】:

      猜你喜欢
      • 2015-10-09
      • 1970-01-01
      • 1970-01-01
      • 2014-11-28
      • 2015-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多