Excel VBA仅复制粘贴值(xlPasteValues)

我试图将sheetA中的整个列复制到SheetB中。sheetA列具有用formuls形成的值。 我只使用xlPasteValues复制SheetA列值。 但它不会将值粘贴到另一个sheetB。 sheetB中的列是空的。 我的VBA代码

Public Sub CopyrangeA() Dim firstrowDB As Long, lastrow As Long Dim arr1, arr2, i As Integer firstrowDB = 1 arr1 = Array("BJ", "BK") arr2 = Array("A", "B") For i = LBound(arr1) To UBound(arr1) With Sheets("SheetA") lastrow = Application.Max(3, .Cells(.Rows.Count, arr1(i)).End(xlUp).Row) .Range(.Cells(1, arr1(i)), .Cells(lastrow, arr1(i))).Copy Sheets("SheetB").Range(arr2(i) & firstrowDB).PasteSpecial xlPasteValues End With Next Application.CutCopyMode = False End Sub 

如果您只想复制整列,您可以通过执行如下操作简化代码:

 Sub CopyCol() Sheets("Sheet1").Columns(1).Copy Sheets("Sheet2").Columns(2).PasteSpecial xlPasteValues End Sub 

要么

 Sub CopyCol() Sheets("Sheet1").Columns("A").Copy Sheets("Sheet2").Columns("B").PasteSpecial xlPasteValues End Sub 

或者如果你想保持循环

 Public Sub CopyrangeA() Dim firstrowDB As Long, lastrow As Long Dim arr1, arr2, i As Integer firstrowDB = 1 arr1 = Array("BJ", "BK") arr2 = Array("A", "B") For i = LBound(arr1) To UBound(arr1) Sheets("Sheet1").Columns(arr1(i)).Copy Sheets("Sheet2").Columns(arr2(i)).PasteSpecial xlPasteValues Next Application.CutCopyMode = False End Sub 

我会去没有复制/粘贴

  Sheets("SheetB").Range(arr2(i) & firstrowDB).Resize(lastrow, 1).Value = .Range(.Cells(1, arr1(i)), .Cells(lastrow, arr1(i))).Value 

就个人而言,如果你所需要的只是列,我也会缩短一点:

 For i = LBound(arr1) To UBound(arr1) Sheets("SheetA").Columns(arr1(i)).Copy Sheets("SheetB").Columns(arr2(i)).PasteSpecial xlPasteValues Application.CutCopyMode = False Next 

从这个代码片断来看,在lastrowfirstrowDB没有太多的意义

你可以使用这个:

 Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ :=False, Transpose:=False 

你也可以使用它

 Sub CopyPaste() Sheet1.Range("A:A").Copy Sheet2.Activate col = 1 Do Until Sheet2.Cells(1, col) = "" col = col + 1 Loop Sheet2.Cells(1, col).PasteSpecial xlPasteValues End Sub