复制并粘贴excel而不删除单元格当前内容

urd*_*boy 1 excel vba copy-paste cell excel-vba

我正在尝试创建一个单元格来构建excel中的预设响应电子邮件.我需要能够将文本粘贴到单元格中而不删除该单元格中已存在的信息(我需要能够重复执行此操作.)我基本上要求帮助创建具有类似"副本"的单个单元格并将'属性粘贴为word doc.即,当我将文本粘贴到单元格中时,它会将新文本插入到上一个文本下面,而不是用剪贴板替换现有文本.

我绝对可以在VBA中使用一些帮助编码.如果有任何需要澄清的话,请告诉我!谢谢

use*_*820 8

我写了代码来做你要求的,但我不确定它是否真的是你需要的.

Alt+F11

双击要使其工作的工作表(左侧)

粘贴代码

粘贴此代码.

请注意,此代码允许您通过单击"删除"来清除单元格.

否则,将很难删除它.

请注意,这实际上会附加您尝试使用编辑进行编辑的任何单元格.

Dim currentVal
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.CountLarge > 1 Then Exit Sub
If currentVal <> vbNullString And Target.Value <> vbNullString Then
    Application.EnableEvents = False
    Target.Value = currentVal & vbCrLf & Target.Value
    currentVal = Target.Value
    Application.EnableEvents = True
Else
    currentVal = Target.Value
End If
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.CountLarge = 1 Then currentVal = Target.Value
End Sub
Run Code Online (Sandbox Code Playgroud)

这个怎么运作:

工作

希望最终编辑以使用在移动设备上键入的合并单元格无法测试:

Dim currentVal
Private Sub Worksheet_Change(ByVal Target As Range)
If currentVal <> vbNullString And Target.Cells(1,1).Value <> vbNullString Then
    Application.EnableEvents = False
    'Use this to add an extra line instead
    Target.Cells(1,1).Value = currentVal & vbLf & vbLf & Target.Cells(1,1).Value
    'Target.Cells(1,1).Value = currentVal & vbLf & Target.Cells(1,1).Value
    currentVal = Target.Cells(1,1).Value
    Application.EnableEvents = True
Else
    currentVal = Target.Cells(1,1).Value
End If
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
currentVal = Target.Cells(1,1).Value
End Sub
Run Code Online (Sandbox Code Playgroud)

  • user1274820,我们投票给你的答案是未删除的,因为这是OP想要的解决方案.要求你不要删除它请:) (2认同)
  • @urdearboy - 看来你在很短的时间内走了很长一段路. (2认同)