klnp_mysore
Board Regular
- Joined
- Mar 2, 2011
- Messages
- 64
Hi
How to copy the selected columns from one worksheet to another worksheet.
How to copy the selected columns from one worksheet to another worksheet.
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
The below code assumes Sheets "Sheet1" and "Sheet2" exist and Sheet1 is active when the code runs.Since the code "assigns" values rather than copying, the cell formating will not be transferred.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
If c.Value = "Y" Then