3

情况很简单,如果我将 cell 命名A1MY_CELL,然后CUT AND PASTE从 cellA2到 cell MY_CELL,那么MY_CELL将失去该名称并A1再次命名。

但是,当我从to复制和粘贴时,不会发生这种情况。A2MY_CELL

如何防止 Excel 重置CUT AND PASTE上的单元格名称?

4

1 回答 1

2

逻辑A1:使用事件捕获对 Cell 的更改,Worksheet_Change并在丢失时重新创建名称。

代码

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim DoesRngNameExists As Boolean
    Dim sName As String

    On Error GoTo Whoa

    Application.EnableEvents = False

    '~~> You named range
    sName = "MY_CELL"

    If Not Intersect(Target, Range("A1")) Is Nothing Then
        '~~> Check if the Range exists
        On Error Resume Next
        DoesRngNameExists = Len(Names(sName).Name) <> 0
        On Error GoTo 0

        '~~> If not then recreate it
        If DoesRngNameExists = False Then _
        ThisWorkbook.Names.Add Name:=sName, RefersToR1C1:="=Sheet1!R1C1"
    End If

Letscontinue:
    Application.EnableEvents = True
    Exit Sub
Whoa:
    MsgBox Err.Description
    Resume Letscontinue
End Sub

注意:相关工作表的工作表代码区域中的代码。请参阅下面的屏幕截图。

在此处输入图像描述

于 2013-04-11T15:24:29.787 回答