krishna334
Active Member
- Joined
- May 22, 2009
- Messages
- 391
Hi All,
I have 2 sheets in my workbook - A and B
In the first row of each sheet i have headers.
But only sheet A has data under each header.
What i need is :
Headers in sheet B's first row has to be searched in sheet A and if the same header exist in sheet A, copy all data under the header in sheet A and paste it under corresponding header in sheet B.
I used the below code but doesn't work if a header in B does not match with any of the headers in A. I want the code to be modified as if B header does not match skip it and continue with the next header in B.
Sub arrange()
Dim i, j As Integer, k As Long, x As String
k = Sheets("A").Range("A65536").End(xlUp).Row
i = 1
For i = 1 To 3
Sheets("A").Select
Cells.Find(What:=Sheets("B").Cells(1, i).Value, After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
Range(ActiveCell.Offset(1, 0), ActiveCell.Offset(k - 1, 0)).Copy Destination:=Sheets("B").Cells(1, i).Offset(1, 0)
Next i
End Sub
I have 2 sheets in my workbook - A and B
In the first row of each sheet i have headers.
But only sheet A has data under each header.
What i need is :
Headers in sheet B's first row has to be searched in sheet A and if the same header exist in sheet A, copy all data under the header in sheet A and paste it under corresponding header in sheet B.
I used the below code but doesn't work if a header in B does not match with any of the headers in A. I want the code to be modified as if B header does not match skip it and continue with the next header in B.
Sub arrange()
Dim i, j As Integer, k As Long, x As String
k = Sheets("A").Range("A65536").End(xlUp).Row
i = 1
For i = 1 To 3
Sheets("A").Select
Cells.Find(What:=Sheets("B").Cells(1, i).Value, After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
Range(ActiveCell.Offset(1, 0), ActiveCell.Offset(k - 1, 0)).Copy Destination:=Sheets("B").Cells(1, i).Offset(1, 0)
Next i
End Sub