Macro to Copy and paste values in a specified column to a column in another worksheet

LOPA

New Member
Joined
Oct 28, 2011
Messages
8
Hi,
i have a "payroll sheet" which have a cell (say D2)containing the week#. There is data in a certain column let say "C" which i want to copy to my "Consolidated sheet". I am looking for a macro which can copy the values in column C in payroll sheet to the appropriate column in the consolidated sheet e.g. if the value in Cell D2 is 5 then it should paste the values in column E of consolidated sheet. if it is 6 then paste the values in column F of concolidated sheet. Basically the payroll sheet will be updated every week with Cell D2 showing the week# and i want the consolidated sheet to show the data for all the weeks in seperate columns. hope i am clear. help will be greatly appreciated! Thanks.
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Welcome to the Board.
Does the data in column C begin in C1 or C2? Do you want the paste to begin in the first row of the consolidated sheet or the second row (i.e. does the sheet have a header)?
 
Upvote 0
Sorry forgot to mention.. data will start from C2 and the sheet here it will be pasted will also have header row.
 
Upvote 0
Try this. Note that you may have to adjust the sheet names as noted with comments in the code:
Code:
Sub ConsolidatePayrollData()
Const pSh = "Payroll"  'Adjust to your sheet name
Const cSh = "Consolidated"  'Adjust to your sheet name
Dim wRng As Range, cRng As Range, sSh As Worksheet, rSh As Worksheet, lRw As Long
Set sSh = Worksheets(pSh)
Set rSh = Worksheets(cSh)
Set wRng = sSh.Range("D2")   'adjust to your range
lRw = sSh.Range("C" & Rows.Count).End(xlUp).Row
Set cRng = sSh.Range("C2", "C" & lRw)
With Application
    .ScreenUpdating = False
End With
cRng.Copy
rSh.Cells(2, wRng.Value).PasteSpecial Paste:=xlValues
With Application
    .ScreenUpdating = True
    .CutCopyMode = False
End With

End Sub
 
Upvote 0
Thanks! it works like a charm. is it possible that if i put numbers in the header row of consolidated sheet (1-52) and macro checks the number in cell D2 and then puts the data in appropriate column. I really appreciate your help
 
Upvote 0
Thanks! it works like a charm. is it possible that if i put numbers in the header row of consolidated sheet (1-52) and macro checks the number in cell D2 and then puts the data in appropriate column. I really appreciate your help
It is possible. Try this (your header numbers are assumed to be in the first row of the consolidated sheet, they need not be in consecutive columns).
Code:
Sub ConsolidatePayrollData2()
Const pSh = "Payroll"  'Adjust to your sheet name
Const cSh = "Consolidated"  'Adjust to your sheet name
Dim wRng As Range, cRng As Range, sSh As Worksheet, rSh As Worksheet, lRw As Long
Set sSh = Worksheets(pSh)
Set rSh = Worksheets(cSh)
Set wRng = sSh.Range("D2")   'adjust to your range
lRw = sSh.Range("C" & Rows.Count).End(xlUp).Row
Set cRng = sSh.Range("C2", "C" & lRw)
With Application
    .ScreenUpdating = False
End With
cRng.Copy
rSh.Range("1:1").Find(wRng.Value, after:=rSh.Range("A1"), _
    LookIn:=xlValues, lookat:=xlWhole).Offset(1, 0).PasteSpecial Paste:=xlValues
With Application
    .ScreenUpdating = True
    .CutCopyMode = False
End With

End Sub
 
Upvote 0
Hope i am not asking for too much. is it possible that instead of copy and pasting only Column C it pastes the data in column C,D, E and F together in a bunch. the header row in consolidated sheet will have 4 columns with same week number . Also instead of D2, F2 will have the week number on payroll sheet. Thanks in advance for your help!
 
Upvote 0
If you include column F in the data that means that the week # in F2 is part of the data to be copied and pasted into the consolidated sheet. Is that what you want?
 
Upvote 0

Forum statistics

Threads
1,214,907
Messages
6,122,185
Members
449,071
Latest member
cdnMech

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