使用VBA剪切单元格文本和格式,然后双击,然后粘贴下一次单击

时间:2018-01-31 19:05:58

标签: excel vba excel-vba

尝试在单元格中设置双击以剪切文本和格式,然后在另一个单元格中单击一下即可剪切剪切文本和格式。当前代码仅将未格式化的文本粘贴到新单元格中。

这是我现在拥有的

    Option Explicit

    Dim CutValue
    Dim CutCell As Range

    Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)

        Cancel = True
        Target.Cut 
        CutValue = Target.Text
        Set CutCell = Target

    End Sub

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)

        If Not IsEmpty(CutValue) Then
            Target.FormulaR1C1 = CutValue
            CutValue = Empty
            CutCell.Clear
        End If

    End Sub

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

您可以延迟剪切,直到用户选择另一个单元格:

Option Explicit

Dim CutCell As Range

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)

    Cancel = True
    Set CutCell = Target

End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    If Not CutCell Is Nothing Then
        CutCell.Cut Target.Cells(1) '<<<in case of multi-cell selection...
        Set CutCell = Nothing
    End If

End Sub