How to disable Excel's automatic cell reference change after copy/paste?

后端 未结 13 2251
温柔的废话
温柔的废话 2020-12-07 23:14

I\'ve got a massive Excel 2003 spreadsheet I\'m working on. There are a lot of very large formulas with a lot of cell references. Here\'s a simple example.

=         


        
13条回答
  •  借酒劲吻你
    2020-12-07 23:33

    It's common to use find/ replace to disable formulas whilst performing manipulations. For example, copy with transpose. The formula is disabled by placing some placeholder (e.g. "$=") in front of it. Find replace can get rid of these once the manipulation is complete.

    This vba just automates the find/ replace for the active sheet.

    ' toggle forumlas on active sheet as active/ inactive
    ' by use of "$=" prefix
    
    Sub toggle_active_formulas()
        Dim current_calc_method As String
        initial_calc_method = Application.Calculation
        Application.Calculation = xlCalculationManual
        Dim predominant As Integer
        Dim c As Range
        For Each c In ActiveSheet.UsedRange.Cells
            If c.HasFormula Then
                predominant = predominant + 1
            ElseIf "$=" = Left(c.Value, 2) Then
                predominant = predominant - 1
            End If
        Next c
        If predominant > 0 Then
            For Each c In ActiveSheet.UsedRange.Cells
                On Error Resume Next
                If c.HasFormula Then
                    c.Value = "$" & c.Formula
                End If
            Next c
        Else
            For Each c In ActiveSheet.UsedRange.Cells
                On Error Resume Next
                If "$=" = Left(c.Value, 2) Then
                    c.Formula = Right(c.Value, Len(c.Value) - 1)
                End If
            Next c
        End If
        Application.Calculation = initial_calc_method
    End Sub
    

提交回复
热议问题