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

时间:2016-08-11 18:45:47

标签: excel-vba cell copy-paste vba excel

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

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

1 个答案:

答案 0 :(得分:8)

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

Alt+F11

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

Paste Code

粘贴此代码。

请注意,此代码允许您通过点击“删除”来清除单元格。

否则,很难将其删除。

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

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

工作原理:

Working

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

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