macro to copy past based on header

Rajkiran557

New Member
Joined
Jun 21, 2014
Messages
10
Hello i have put together a macro with help of google which works fine .

i have a request that i am able to find on google, with the below code i am able to copy paste from sheet to another , only thing is the range is fixed like A, C , B , E and I ..

i would like the range be based on the header . Example instead of A and B , range need to be selected by header like "Action taken" and "Location" which would be common in both sheets hope my question is clear the below code might give you more clarity. please help

Sub Copy()


Dim i As Long, j As Long, lastrow1 As Long, lastrow2 As Long
Dim myname As String
lastrow1 = Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row


For i = 2 To lastrow1
myname = Sheets("Sheet1").Cells(i, "A").Value


Sheets("Sheet2").Activate
lastrow2 = Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Row


For j = 2 To lastrow2


If Sheets("Sheet2").Cells(j, "B").Value = myname Then
Sheets("Sheet1").Activate
Sheets("Sheet1").Range(Cells(i, "C"), Cells(i, "E")).Copy
Sheets("Sheet2").Activate
Sheets("Sheet2").Range(Cells(j, "R"), Cells(j, "T")).Select
ActiveSheet.Paste
End If


Next j
Application.CutCopyMode = False
Next i
Sheets("Sheet1").Activate
Sheets("Sheet1").Range("A1").Select


End Sub
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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