macro to copy specific columns of selected rows to another sheet

acmehra

New Member
Joined
Jan 20, 2018
Messages
16
Hi All ,

I am a beginner in vb. I want to copy data of rows selected by user to another sheet . But data from column A, B and E of the source sheet should be copied from the selected rows.

I am able to write the code which can copy whole row to destination sheet but I need help to copy data from specific columns only. Please help.

Below is my current code :

HTML:
Sub btnS()
Selection.Copy Sheets("destination").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)Worksheets("destination").Activate
   End Sub
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Hi & welcome to the board
How about
Code:
Sub btnS()
   Union(Selection.Resize(, 2), Selection.Offset(, 4)).Copy Sheets("destination").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
   Worksheets("destination").Activate
End Sub
 
Upvote 0
Hi & welcome to the board
How about
Code:
Sub btnS()
   Union(Selection.Resize(, 2), Selection.Offset(, 4)).Copy Sheets("destination").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
   Worksheets("destination").Activate
End Sub


Its showing 1004 error. I Guess its because of
HTML:
Union(Selection.Resize(, 2),

what should be the first parameter value in union ?
 
Upvote 0
What is the error message?
 
Upvote 0
If you want to copy non contiguous columns and paste them as contiguous then
Code:
Dim rng As Range, c As Range
With ActiveSheet
    For Each c In .Range("A2", .Cells(Rows.Count, 1).End(xlUp))
        If c <> "" Then
            Set rng = Union(.Cells(c.Row, 1), .Cells(c.Row, 2), .Cells(c.Row, 5))
            rng.Copy Sheets(2).Cells(Rows.Count, 1).End(xlUp)
        End If
    Next
End With
Barring any typos, this would copy cells from columns A, B, E of a qualifying row and paste to the next available row of sheet 2.
This would copy and paste using the same columns in both sheets.
Code:
Dim rng As Variant, c As Range, i As Long
With ActiveSheet
    rng = Array(1, 2, 5)
    For Each c In .Range("A2", .Cells(Rows.Count, 1).End(xlUp))
        If c <> "" Then
            For i = LBound(rng) To UBound(rng)
                If i = LBound(rng) Then
                    .Cells(c.Row, rng(i)).Copy Sheets(2).Cells(Rows.Count, 1).End(xlUp)(2)
                Else
                    .Cells(c.Row, rng(i)).Copy Sheets(2).Cells(Rows.Count, 1).End(xlUp).Offset(, i - 1)
                End If
            Next
        End If
    Next
End With
 
Last edited:
Upvote 0
Hi Fluff ,

Thank you for you inputs but user may or may not select contiguous rows on source sheet.
 
Upvote 0
In that case, how about
Code:
Sub btnS()
   
   Dim Rng As Range
   
   For Each Rng In Selection.Columns(1).Areas
      Union(Rng.Resize(, 2), Rng.Offset(, 4)).Copy Sheets("destination").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
   Next Rng
   Worksheets("destination").Activate
End Sub
 
Upvote 0
In that case, how about
Code:
Sub btnS()
   
   Dim Rng As Range
   
   For Each Rng In Selection.Columns(1).Areas
      Union(Rng.Resize(, 2), Rng.Offset(, 4)).Copy Sheets("destination").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
   Next Rng
   Worksheets("destination").Activate
End Sub

Its copying only first row out of all the selected rows :(
 
Upvote 0
Ok, try
Code:
Sub btnS()
   
   Dim Rng As Range
   
   For Each Rng In Selection.Areas
      Union(Rng.Resize(, 2), Rng.Resize(, 1).Offset(, 4)).Copy Sheets("destination").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
   Next Rng
   Worksheets("destination").Activate
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,695
Members
448,979
Latest member
DET4492

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top