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
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