Combine data in two columns into one and sort the combined column
Sub mergecolumns()
LastrowA = Cells(Rows.Count, "A").End(xlUp).Row
LastrowB = Cells(Rows.Count, "B").End(xlUp).Row
If LastrowA LastrowB Then
LastRow = LastrowA
Else
LastRow = LastrowB
End If
Set SourceRange = Range(Cells(1, "A"), Cells(LastRow, "B"))
RowCount = 1
For Each cell In SourceRange
If Not IsEmpty(cell.Value) Then
If RowCount = 1 Then
Cells(1, "D") = cell.Value
RowCount = RowCount + 1
Else
Set DestRange = Range(Cells(1, "D"), _
Cells(RowCount - 1, "D"))
Set c = DestRange.Find(what:=cell, LookIn:=xlValues)
If c Is Nothing Then
Cells(RowCount, "D") = cell.Value
RowCount = RowCount + 1
End If
End If
End If
Next cell
Range(Cells(1, "D"), Cells(RowCount - 1, "D")).Sort _
Key1:=Range("D1"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
End Sub
"Tommy" wrote:
I have two columns, A and B. Each column contains a list of data in
text format e.g. 01TI518A.PV . It is possible that duplicates of this
text may appear within either column. Also, the number of data values
in each column is variable as they are imported from an external
source into the worksheet. I would like to be able to combine the text
from both columns into a single list in a single column, say column C.
I would also like this column to be alphanumerically sorted. How do I
go about doing this?
|