简体   繁体   中英

How to copy two non adjacent columns in range

I need to copy just columns D & H and paste to a table. This is what i have so far. I have tried a few different modifications searching this site with no luck. Thanks

Sub Zulily_DS()
On Error Resume Next

Dim lastrowB As Long
Dim lastrowB1 As Long
Dim myLastCell As Range

Application.ScreenUpdating = True

lastrowB = Sheets("Source").Cells(Rows.Count, "B").End(xlUp).Row + 1
lastrowB1 = Sheets("Source").Cells(Rows.Count, "B").End(xlUp).Row

Sheets("Source").Select
With Sheets("Zulily_DS")

If Sheets("Source").Range("C2").Value = vbNullString Then
    .Range("D2:H2", .Range("D" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeConstants).Copy _
                                                                            Sheets("Source").Cells(lastrowB1, 2)

ElseIf Sheets("Source").Range("C2").Value > "0" Then
    .Range("D2:H2", .Range("D" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeConstants).Copy _
                                                                            Sheets("Source").Cells(lastrowB, 2)
End If
End With

Application.ScreenUpdating = False
End Sub

If instead of separating your ranges like:

Range("A1:A2","D1:D2")

Change to:

Range("A1:A2,D1:D2")

Notice the quotes are not separating the ranges from each other. They will paste in a table format like you are expecting like this:

Instead of this:

Range("A1:A2","D1:D2").Copy Range("B6:C7")

Use This:

Range("A1:A2,D1:D2").Copy Range("B6:C7")

The first is copying different ranges, while the second is combining the ranges.

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