VBA How to copy the content of a cell without .Select

前端 未结 3 1929
小蘑菇
小蘑菇 2021-02-07 18:56

I am writing a method that can take the Target and paste the cell exactly to another cell. The cell is a shipping label with some fancy formatting. Is there a way I

3条回答
  •  星月不相逢
    2021-02-07 19:00

    You can loop thru each cell of the range to copy and set each cell's value, comment, etc in the target destination. Here's an example.

    Sub CopySpecial(p_RangeFrom As String, p_OffsetRow As Integer, p_OffsetColumn As Integer)
    
        Dim l_Row As Integer
        Dim l_Column As Integer
        Dim thisCell As Range
        Dim l_TargetCell As Range
    
        l_Row = Range(p_RangeFrom).Row
        l_Column = Range(p_RangeFrom).Column
    
        For Each thisCell In Range(p_RangeFrom)
    
            Set l_TargetCell = Range(Cells(thisCell.Row + p_OffsetRow, thisCell.Column + p_OffsetColumn).Address)
    
            ' Copy the text
            l_TargetCell.Value = thisCell.Value
    
            ' Copy the comment only if we have a comment to copy
            If Not thisCell.Comment Is Nothing Then
    
                ' Delete any existing comment in the target cell, if any.
                ' If you don't to this the .AddComment Method below will fail.
                If Not l_TargetCell.Comment Is Nothing Then l_TargetCell.Comment.Delete
                Call l_TargetCell.AddComment(thisCell.Comment.Text)
    
            End If
    
            ' Add more items to copy here, such as color, etc.
    
        Next
    
    End Sub
    
    Sub TestCall()
        Call CopySpecial("A1:B2", 3, 3)
    End Sub
    

提交回复
热议问题