Excel - 文本输入后在字段上添加前缀/后缀文本

Excel - 文本输入后在字段上添加前缀/后缀文本

我想在一个字段中输入一串文本,一旦我点击,Enter就会在文本中添加一个常量前缀和后缀 - 例如,如果我输入购买Linux或者侮辱比尔进入单元格并点击Enter(或Tab)提交文本,然后该字段显示永远不要购买Linux或者永不侮辱比尔等等。我看过了CONCATREPLACESUBSTITUTE无法做到这一点(我得到了循环引用错误)。有什么线索吗,大师们?

答案1

Private Const prefix As String = "-=prefix=-"
Private Const postfix As String = "-=postfix=-"

Private Sub Worksheet_Change(ByVal Target As Range)
' the flag to avoid recursive calls
Static busy As Boolean
' If this call is recursive then do nothing
If busy Then Exit Sub
' Mark that the action is now performed
busy = True
' When some value on the worksheet is changed we check
' that only one cell is selected (not multicell range)
' and this cell is in column 1 (column A)
If Target.Column = 1 And Target.Cells.Count = 1 Then
    ' If one cell in column A is altered, update its value
    Target.Value = prefix & Target.Value & postfix
End If
' Drop execution mark
busy = False
End Sub

相关内容