VBA 复制目标但作为值

VBA Copy Destination but as values

我正在尝试使用 "Copy Destination" 将数据从一个文档传输到另一个文档,因为我想避免使用剪贴板,但我希望它停止使用它进行格式化...

Dim Sheet As Worksheet
Dim FoundLocationSheet As Boolean
Dim n As Long
Dim AllSheet As Worksheet
Set AllSheet = Sheets("Main")

'Transfer data
For n = 1 To AllSheet.Cells(Rows.Count, 1).End(xlUp).Row
    If AllSheet.Cells(n, 1) = "TiTle" Then
        With Sheets(AllSheet.Cells(n - 1, 1).Value)
            AllSheet.Cells(n, 1).CurrentRegion.Copy Destination:=.Cells(Rows.Count, 1).End(xlUp).Offset(2, 0)
        End With
    End If
Next n

宏可能会从 A20:L40 中提取数据并将其放入 A15:L35...

我一直在用 AllSheet.Cells(n, 1).CurrentRegion.Copy Destination:=.Cells(Rows.Count, 1).End(xlUp).Offset(2, 0) 尝试很多不同的东西,但不知道如何让它发挥作用...

sheet 的大小意味着清除格式花费的时间太长了:/

有什么想法吗?

因为你想避免剪贴板并且只复制值,你可以使用赋值给Value 属性而不是Range.Copy

像这样

Sub Demo()
    Dim Sheet As Worksheet
    Dim FoundLocationSheet As Boolean
    Dim n As Long
    Dim rSource As Range
    Dim rDest As Range
    Dim AllSheet As Worksheet

    Set AllSheet = Sheets("Main")

    'Transfer data
    For n = 1 To AllSheet.Cells(Rows.Count, 1).End(xlUp).Row
        If AllSheet.Cells(n, 1) = "TiTle" Then
            With Worksheets(AllSheet.Cells(n - 1, 1).Value)
                ' Reference the range to be copied
                Set rSource = AllSheet.Cells(n, 1).CurrentRegion
                ' Reference the Top Left cell of the destination range
                ' and resize to match source range
                Set rDest = _
                  .Cells(.Rows.Count, 1).End(xlUp).Offset(2, 0) _
                  .Resize(rSource.Rows.Count, rSource.Columns.Count)
                ' Copy values
                rDest.Value = rSource.Value
            End With
        End If
    Next n
End Sub

您可以将数据复制到任何数组,然后从数组复制到目标。执行此操作的代码很短而且效率惊人。注意:源必须有多个单元格。

' Create dynamic array
Dim arr() As Variant
Dim rg As Range


Set rg = AllSheet.Cells(n, 1).CurrentRegion
' Read values to array
arr = rg.Value

' Write the values back sheet
.Cells(Rows.Count, 1).End(xlUp).Offset(2, 0).Value = arr