To copy selected columns to another sheet

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Code:
    Selection.EntireColumn.Copy _
    Destination:=Sheets("Sheet2").Range("A1")
Full column(s) must be selected and Sheet2 must exist.
 
Upvote 0
hi,
I have 7 columns in sheet1 and 5 columns in sheet2.

Sheet1
ColA ColB ColC ColD ColE ColF ColG
1 aa bb cc dd ee Y


Sheet2
ColA ColB ColC ColD ColE
1 aa cc dd ee

I have to copy the selected values of sheet1 if the value in ColG is "Y" .
ex. the values in colA of sheet1 to be copied to ColA of sheet2, the values in colB of sheet1 to be copied to ColB of sheet2, the values in colD of sheet1 to be copied to ColC of sheet2, the values in colE of sheet1 to be copied to ColD of sheet2,

How to do this with exce vba.

Regards
 
Upvote 0
The below code assumes Sheets "Sheet1" and "Sheet2" exist and Sheet1 is active when the code runs.
Code:
Sub CopyBetweenSheets()
    'Assign Range where "Y" values exist
    Set Rng1 = Range("G1:G" & Sheets("Sheet1").Cells(Rows.Count, "G").End(xlUp).Row)
    For Each c In Rng1
        If c.Value = "Y" Then
            'Assign LastRow variable
            LR = Sheets("Sheet2").Cells(Sheets("Sheet2").Rows.Count, 1).End(xlUp).Row + 1
            'Assign cell values between sheets
            Sheets("Sheet2").Range("A" & LR).Value = Cells(c.Row, "A").Value
            Sheets("Sheet2").Range("B" & LR).Value = Cells(c.Row, "B").Value
            Sheets("Sheet2").Range("C" & LR).Value = Cells(c.Row, "D").Value
            Sheets("Sheet2").Range("D" & LR).Value = Cells(c.Row, "E").Value
        End If
    Next c
End Sub
Since the code "assigns" values rather than copying, the cell formating will not be transferred.
 
Upvote 0
Hi,
First row contains column Heading, datas start from the second row,
Sheet1 contains blank rows in between.I have to copy only the rows that have entry in column G as "OK".

Regards


The below code assumes Sheets "Sheet1" and "Sheet2" exist and Sheet1 is active when the code runs.
Code:
Sub CopyBetweenSheets()
    'Assign Range where "Y" values exist
    Set Rng1 = Range("G1:G" & Sheets("Sheet1").Cells(Rows.Count, "G").End(xlUp).Row)
    For Each c In Rng1
        If c.Value = "Y" Then
            'Assign LastRow variable
            LR = Sheets("Sheet2").Cells(Sheets("Sheet2").Rows.Count, 1).End(xlUp).Row + 1
            'Assign cell values between sheets
            Sheets("Sheet2").Range("A" & LR).Value = Cells(c.Row, "A").Value
            Sheets("Sheet2").Range("B" & LR).Value = Cells(c.Row, "B").Value
            Sheets("Sheet2").Range("C" & LR).Value = Cells(c.Row, "D").Value
            Sheets("Sheet2").Range("D" & LR).Value = Cells(c.Row, "E").Value
        End If
    Next c
End Sub
Since the code "assigns" values rather than copying, the cell formating will not be transferred.
 
Upvote 0
In your first post you said you wanted to copy rows if Column G contained "Y", now you say you want to copy rows that have entry in column G as "OK".

Edit the following code line as needed:
Code:
If c.Value = "Y" Then
Did you try the code?
Does it do what you want?
 
Upvote 0
hi,
I have 7 columns in sheet1 and 5 columns in sheet2.

Sheet1
ColA ColB ColC ColD ColE ColF ColG
1 aa bb cc dd ee Y


Sheet2
ColA ColB ColC ColD ColE
1 aa cc dd ee

I have to copy the selected values of sheet1 if the value in ColG is "Y" .
ex. the values in colA of sheet1 to be copied to ColA of sheet2, the values in colB of sheet1 to be copied to ColB of sheet2, the values in colD of sheet1 to be copied to ColC of sheet2, the values in colE of sheet1 to be copied to ColD of sheet2,
If colG is "N" then the copy the rows to sheet3

How to do this with exce vba.

Regards
<!-- / message -->
 
Upvote 0

Forum statistics

Threads
1,224,551
Messages
6,179,473
Members
452,915
Latest member
hannnahheileen

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