Looking for a solution to combine to columns of data into one in Excel. The data is not adjacent, so I wish to combine data in Column A with data in Column C, placing the results into Column E.
An example:
Row A B C D E (Desired result)
1 1
2 1 2 2
3 3 4 3
4 5 6 4
5 1 2 5
6 6
7 7 8 7
8 8
It's important that the data is in order of first appearance by row. As an aside it would also be good to avoid duplicates (see the second 1 & 2 not duplicated in example col E), but that is easily dealt with afterwards.
I also need it to ignore blank cells.
Would prefer to achieve this via VBA.
Loop through all the cells in columns A and C, if they don't already exist in Column E, list them sequentially:
Sub Test()
With ActiveSheet
For i = 1 To .Cells(.Rows.Count, 1).End(xlUp).Row
If Cells(i, 1).Value <> "" Then
If WorksheetFunction.CountIf(.Range("E:E"), Cells(i, 1).Value) = 0 Then
Cells(.Cells(.Rows.Count, 5).End(xlUp).Row + 1, 5).Value = Cells(i, 1).Value
End If
End If
If Cells(i, 3).Value <> "" Then
If WorksheetFunction.CountIf(.Range("E:E"), Cells(i, 3).Value) = 0 Then
Cells(.Cells(.Rows.Count, 5).End(xlUp).Row + 1, 5).Value = Cells(i, 3).Value
End If
End If
Next i
End With
End Sub
Here is my attempt:
Sub Test()
Dim MyArray() As Variant
Dim X As Long, Y As Long, Z As Long
Dim RNG As Range, CEL As Range
Z = 1
MyArray = Range(Cells(1, 1), Cells(9, 3)).Value
For X = 1 To UBound(MyArray, 1)
For Y = 1 To UBound(MyArray, 2)
'Debug.Print MyArray(X, Y)
Set RNG = Range(Cells(1, 5), Cells(Z, 5))
Set CEL = RNG.Find(MyArray(X, Y), lookat:=xlWhole)
If CEL Is Nothing Then
Cells(Z, 5).Value = MyArray(X, Y)
Z = Z + 1
End If
Next Y
Next X
End Sub
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.