If your set A is in sheet1 starting with row 2 and set B is in sheet2 starting with row 2, then try this macro -
Option Explicit
Sub cons_data()
Dim i As Long, lrow As Long, lcol As Long
Application.ScreenUpdating = False
lrow = Worksheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Row
Worksheets("Sheet2").Range("A2:B" & lrow).Copy Worksheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
With Worksheets("Sheet1")
.Sort.SortFields.Clear
.Sort.SortFields.Add Key:=Range("A:A") _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With .Sort
.SetRange Range("A:B")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
lrow = .Range("A" & .Rows.Count).End(xlUp).Row
For i = lrow To 2 Step -1
If .Range("A" & i).Value = .Range("A" & i - 1).Value Then
lcol = .Range("IV" & i - 1).End(xlToLeft).Column
.Cells(i - 1, lcol + 1).Value = .Range("B" & i).Value
.Rows(i).Delete
End If
Next i
End With
Application.ScreenUpdating = True
End Sub
Copy the Excel VBA code
Select the workbook in which you want to store the Excel VBA code
Hold the Alt key, and press the F11 key, to open the Visual Basic Editor
Choose Insert | Module
Where the cursor is flashing, choose Edit | Paste
To run the Excel VBA code:
Choose View | Macros
Select a macro in the list, and click the Run button
The output will appear in sheet1.
Bookmarks