Try this code and let me know -
Option Explicit
Sub compare_cols()
Dim i As Long, lrow As Long, lrow1 As Long
Worksheets.Add(after:=Worksheets(Worksheets.Count)).Name = "Temp"
With Worksheets("Sheet1")
.Columns("A:A").Copy Worksheets("Temp").Columns("A:A")
lrow = Worksheets("Temp").Range("A" & Rows.Count).End(xlUp).Row
Worksheets("Temp").Range("B1:B" & lrow).Value = "A"
lrow = .Range("C" & .Rows.Count).End(xlUp).Row
.Range("C1:C" & lrow).Copy Worksheets("Temp").Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
End With
With Worksheets("Temp")
lrow = .Range("A" & .Rows.Count).End(xlUp).Row
lrow1 = .Range("B" & .Rows.Count).End(xlUp).Row
.Range("B" & lrow1 + 1 & ":B" & lrow).Value = "B"
.Sort.SortFields.Clear
.Sort.SortFields.Add Key:=Range("A:A") _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.Sort.SortFields.Add Key:=Range("B:B") _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With .Sort
.SetRange Range("A:B")
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
For i = 2 To lrow
If .Range("A" & i).Value = .Range("A" & i + 1).Value And .Range("B" & i).Value <> .Range("B" & i + 1).Value Then
.Range("A" & i).Value = ""
End If
Next i
.Rows(1).Insert
.Rows(1).AutoFilter field:=2, Criteria1:="A"
End With
With Worksheets("Sheet1")
.Columns("A:A").ClearContents
Worksheets("Temp").UsedRange.Columns(1).SpecialCells(xlCellTypeVisible).Copy Worksheets("Sheet1").Range("A1")
.Sort.SortFields.Add Key:=Range("A:A") _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With .Sort
.SetRange Range("A:A")
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End With
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
Bookmarks