【问题标题】:VBA How to copy the content of a cell without .SelectVBA如何在没有.Select的情况下复制单元格的内容
【发布时间】:2013-05-22 18:00:55
【问题描述】:

我正在编写一个方法,它可以采用Target 并将单元格准确地粘贴到另一个单元格。该单元格是一个带有一些花哨格式的运输标签。有什么办法可以吗?

原来我有这个:

Worksheets("Label").Range("A1").Value = Worksheets("Get Address").Range("A28").Value

它适用于纯文本。但是我失去了我创建的样式,它们是不同的,因为在第一行之后,样式是不同的:

我也尝试使用宏记录器,我得到了一个使用.Select 的解决方案,我read this question 尽可能不使用它。我能做什么?

' Created by the Macro Recorder
Range("A28:A33").Select
Range("A33").Activate
Selection.Copy
Sheets("Label").Select
Range("A1").Select
ActiveSheet.Paste

【问题讨论】:

  • 您的代码可以在一行中重写Range("A33").Copy Sheets("Label").Range("A1")
  • +1 用于很棒的地址示例

标签: excel vba excel-2003


【解决方案1】:
Worksheets("Get Address").Range("A33").Copy _
       Destination := Worksheets("Label").Range("A1")

【讨论】:

    【解决方案2】:

    复制和粘贴值,然后使用以下

    Worksheets("Label").Range("A1").value = _
       Worksheets("Get Address").Range("A33").value
    

    此语句不会使用剪贴板

    【讨论】:

      【解决方案3】:

      您可以循环遍历范围的每个单元格,以在目标目标中复制和设置每个单元格的值、注释等。 这是一个例子。

      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
      

      【讨论】:

        猜你喜欢
        • 2018-12-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-26
        相关资源
        最近更新 更多