简体   繁体   中英

Copy multiple Range from worksheet to another worksheet

I'm trying to copy multiple cells from one worksheet to another. I'm getting error message wrong number of arguments or invalid property assignment.

Range("D10:D12,D15,D22,D25,D32:D33,D38:D42,D47:D50,D53,D55,D57,D63").Select
Range("G3").Select
Selection.Copy
Sheets("Sheet3").Select
'Range("I4").End(xlUp).Select
lMaxRows = Cells(Rows.Count, "I", "AD").End(xlUp).Row
Range("I", "AD" & lMaxRows + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=True
Sheets("Sheet1").Select
Range("I4", "AD").Select

Hoping for your help.


i tried using union but cannot come up with a solution. Here is the codes I have now

Dim r1 As Range, r2 As Range, multiRange As Range

Set r1 = Sheets("Sheet1").Range("D10:D12,D15,D22,D25,D32:D33,D38:D42,D47:D50,D53,D55,D57,D63")
Set r2 = Sheets("Sheet1").Range("G3")
Set multiRange = Union(r1, r2)
Application.Union(r1, r2).Select
Selection.Copy
Sheets("Sheet3").Select
'Range("I4").End(xlUp).Select
lMaxRows = Cells(Rows.Count, "I").End(xlUp).Row
Range("I" & lMaxRows + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=True
Sheets("Sheet1").Select
Range("I4").Select

THE error message I'm know getting is That command cannot be used on multiple selections. The Highlighted code is SELECTION.COPY

The code below assumes that Sheets("Sheet1") has sheet CodeName "Sheet1", and similar for Sheet3. (In general you should use sheet CodeName in your code).

Dim SourceArea As Range
Dim TargetArea As Range
Dim CopyRange As Range

Set CopyRange = Sheet1.Range("D10:D12,D15,D22,D25,D32:D33,D38:D42,D47:D50,D53,D55,D57,D63")

For Each SourceArea In CopyRange.Areas
    Set TargetArea = Sheet3.Range(SourceArea.Address)
    TargetArea.Value = SourceArea.Value
Next

Edit: The above will paste in Sheet3 in exactly the same locations as the ranges in Sheet1. If you want to paste to a different location, use Offset. For example if you want the top left cell in the target to be I20, then:

Set TargetArea = Sheet3.Range(SourceArea.Address).Offset(10,5)

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