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

后端 未结 13 2268
温柔的废话
温柔的废话 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:42

    This macro does the whole job.

    Sub Absolute_Reference_Copy_Paste()
    'By changing "=" in formulas to "#" the content is no longer seen as a formula.
    ' C+S+e (my keyboard shortcut)
    
    Dim Dummy As Range
    Dim FirstSelection As Range
    Dim SecondSelection As Range
    Dim SheetFirst As Worksheet
    Dim SheetSecond As Worksheet
    
    On Error GoTo Whoa
    
    Application.EnableEvents = False
    
    ' Set starting selection variable.
    Set FirstSelection = Selection
    Set SheetFirst = FirstSelection.Worksheet
    
    ' Reset the Find function so the scope of the search area is the current worksheet.
    Set Dummy = Worksheets(1).Range("A1:A1").Find("Dummy", LookIn:=xlValues)
    
    ' Change "=" to "#" in selection.
    Selection.Replace What:="=", Replacement:="#", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
    
    ' Select the area you want to paste the formulas; must be same size as original
      selection and outside of the original selection.
    Set SecondSelection = Application.InputBox("Select a range", "Obtain Range Object", Type:=8)
    Set SheetSecond = SecondSelection.Worksheet
    
    ' Copy the original selection and paste it into the newly selected area. The active
      selection remains FirstSelection.
    FirstSelection.Copy SecondSelection
    
    ' Restore "=" in FirstSelection.
    Selection.Replace What:="#", Replacement:="=", LookAt:=xlPart, _
    SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
    
    ' Select SecondSelection.
    SheetSecond.Activate
    SecondSelection.Select
    
    ' Restore "=" in SecondSelection.
    Selection.Replace What:="#", Replacement:="=", LookAt:=xlPart, _
    SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
    
    ' Return active selection to the original area: FirstSelection.
    SheetFirst.Activate
    FirstSelection.Select
    
    Application.EnableEvents = True
    
    Exit Sub
    
    Whoa:
    ' If something goes wrong after "=" has been changed in FirstSelection, restore "=".
    FirstSelection.Select
    Selection.Replace What:="#", Replacement:="=", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
    
    End Sub
    

    Note that you must match the size and shape of the original selection when you make your new selection.

提交回复
热议问题