简体   繁体   中英

VBA paste special

I am trying to write a script to copy entries from one worksheets cells into another one with a template. I am having issues setting merged cells to paste in non-merged cells. I have the following code, I know I can use Special Paste functions, but can I use more than one at a time? Would Paste:=xlColumnWidth help?

Sheets("Sheet3").Select
    Range("N6:O6").Select
    Selection.Copy
    Sheets("Reports").Select
    Range("O4").Select
    Selection.PasteSpecial Paste:=xlPasteValues

I agree with David about avoiding Selection but the macro recorder produced the following:

Sub Macro2()
    Sheets("Sheet3").Select
    Range("N6:O6").Select
    Selection.Copy
    Sheets("Reports").Select
    Range("O4").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
End Sub

Which appears to work with N6:O6 being merged..

For a trivial amount of data like this no need to do something like this but good to be aware that select isn't needed much in excel-vba and also that the clipboard can be avoided:

Sub Macro2()

Dim vArr As Variant
vArr = Sheets("Reports").Range("N6:O6")

Dim Destination As Range
Set Destination = Sheets("Sheet3").Range("O4")
Destination.Resize(UBound(vArr, 1), UBound(vArr, 2)).Value = vArr

End Sub

猜猜我将其作为答案

Worksheets("Reports").Range("O4") = Worksheets("Sheet3").Range("N6").MergeArea.Cells(1, 1).Value

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM