
假設我有以下一組數據
A B
AnthonyMorgan EvelynThomas
JoePatterson RussellRobinson
JohnAnderson ChristopherMiller
CarolynMorris RyanStewart
DorisKing SarahPrice
AlanJohnson MarilynHall
JonathanHall EricCooper
AndreaPowell GregorySimmons
PaulaCooper JackRussell
AndrewPeterson SeanEvans
JohnWilliams JudyBell
LillianLewis JohnWilliams
SteveRoberts LillianLewis
MatthewGray SteveRoberts
ChristinaClark MatthewGray
PeterAllen
SharonHill
MaryTurner
DorisGonzales
VictorWhite
JoanFoster
ChristinaClark
RubyBryant
RogerColeman
JosephLong
AndrewPeterson
我想按列進行排序,A
以便將不同列中的相同值保留在一起。最終結果將是:
A B
AlanJohnson
AndreaPowell
AndrewPeterson AndrewPeterson
AnthonyMorgan
CarolynMorris
ChristinaClark ChristinaClark
ChristopherMiller
DorisGonzales
DorisKing
EricCooper
EvelynThomas
GregorySimmons
JackRussell
JoanFoster
JoePatterson
JohnAnderson
JohnWilliams JohnWilliams
JonathanHall
JosephLong
JudyBell
LillianLewis LillianLewis
MarilynHall
MaryTurner
MatthewGray MatthewGray
PaulaCooper
PeterAllen
RogerColeman
RubyBryant
RussellRobinson
RyanStewart
SarahPrice
SeanEvans
SharonHill
SteveRoberts SteveRoberts
VictorWhite
任何想法?
答案1
試試這個宏:
Sub Interleaver()
Dim nA As Long, nB As Long
Dim rc As Long, i As Long, j As Long
rc = Rows.Count
nA = Cells(rc, "A").End(xlUp).Row
nB = Cells(rc, "B").End(xlUp).Row
Range("A1:A" & nA).Copy Range("C1")
Range("B1:B" & nB).Copy Range("C" & nA + 1)
For i = 1 To nA + nB
If i <= nA Then
Cells(i, "D") = "A"
Else
Cells(i, "D") = "B"
End If
Next i
Range("C1:D" & nA + nB).Sort Key1:=Range("C1"), Order1:=xlAscending, Header:=xlNo, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
Range("A1:A" & nA).Clear
Range("B1:B" & nB).Clear
j = 2
If Range("D1").Value = "A" Then
Cells(1, "A") = Cells(1, "C")
Else
Cells(1, "B") = Cells(1, "C")
End If
For i = 2 To nA + nB
If Cells(i, "C") = Cells(i - 1, "C") Then
j = j - 1
Range("A" & j & ":B" & j) = Cells(i, "C")
j = j + 1
Else
If Cells(i, "D").Value = "A" Then
Cells(j, "A") = Cells(i, "C")
Else
Cells(j, "B") = Cells(i, "C")
End If
j = j + 1
End If
Next i
End Sub
它將產生:
答案2
我認為沒有一種簡單的方法可以做到這一點。嘗試以下操作:
- 將資料合併到一個範圍內,記住原始位置(例如,將第一列複製到新工作表並在下一列中填寫“1”,複製第一列下方的第二列並在第二列填寫「10」)這組資料)。
- 插入一個資料透視表,列標籤應該是您的第一列,並要求第二列的總和(現在您的唯一值按字母順序排序,如果名稱僅出現在一列中,則在第二列1 或10 中,如果名稱僅出現在一列中,則在第二列1 或11 中,如果它同時出現在兩列中) )。
- 假設資料透視表中的資料從 A5 開始,請在 C5 中輸入此資料:=if(mod(b5,2)=1,a5,"")
在 d5 中輸入此資料:=if(b5>9,a5,"")
答案3
另一個工作流程如下。請注意,它是手動執行的,因為我不知道有任何 Excel 函數可以執行您正在尋找的操作。另一種選擇是使用巨集或 VBA 來自動化下面建議的工作流程。
圖 1 顯示了初始範例資料:
首先對 wach 列進行單獨排序。保持排序不會影響其他列。
=INDEX($C$1:$C$10,MATCH(A2,$C$1:$C$10,0),1)
圖 2 顯示了此階段的結果:
現在剪下並貼上 B 列以避免與 NA 值相鄰。在範例中,這是向下兩個單元格。然後剪下並貼上 A 列和輔助列,以將條目與 B 列相符。見下圖:
迭代此階段直至完成。請記住從 B 列開始,然後匹配 A 列和 INDEX 列(輔助列)。最後刪除 INDEX 列就完成了。
答案4
您真正要求的是一個完整的外部聯接,將兩列視為單獨的表。
據我所知,即使在 Power Query 插件的幫助下,這在本機 Excel 中也是不可能的。
你能取得成果如果較長的表包含較短表中的所有條目,但您的範例表明情況並非如此。
您可以做的就是附加兩列,然後進行重複資料刪除,這很簡單。您也可以新增計數列,以便可以查看原始資料中存在多少個同名副本。如果您想這樣做,請告訴我。
更新:正如我預期的那樣,您可以使用 Microsoft Access 來完成此操作。為此,您首先需要將這兩個欄位作為單獨的表。然後,您必須在表之間建立兩個聯接,一個左外聯接和一個右外聯接。建立這些關係後,您可以執行簡單的查詢來從每個表中選擇單一列。就是這樣。
如果您願意,您可以將原始資料保留在 Excel 中,只要將兩列拆分為單獨的表格即可連結到它。