VBA复制目标但作为值

我试图从一个文件传输数据到不同的文件使用“复制目的地”,因为我想避免使用剪贴板,但我希望它停止格式化与它…

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 

macros可能会从A20:L40提取数据,并将其放入A15:L35

我一直在尝试与AllSheet.Cells(n, 1).CurrentRegion.Copy Destination:=.Cells(Rows.Count, 1).End(xlUp).Offset(2, 0)研究如何使其工作…

表单的大小意味着清除格式太长了:/

有任何想法吗?

既然你想避免剪贴板并只复制值,你可以使用赋值给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