寻找一种在Excel中将数据列合并为一个的解决方案。数据不相邻,因此我希望将A列中的数据与C列中的数据合并,然后将结果放入E列。
一个例子:
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
重要的是,数据应按行顺序出现。顺便说一句,最好避免重复(请参见示例E中的第二个1和2不重复),但是以后很容易处理。
我还需要它来忽略空白单元格。
宁愿通过VBA实现这一目标。
答案 0 :(得分:0)
遍历A和C列中的所有单元格(如果在E列中尚不存在),请按顺序列出它们:
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
答案 1 :(得分:0)
这是我的尝试:
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