如何通过匹配值对 Excel 中的多列进行排序
How to sort multiple columns in Excel by matching value
我正在清理数据。我从多个 excel 电子表格中收集数据。
我正在尝试在完全匹配的基础上实现排序,即
**Original dataset**
FirstName_Store1 FirstName_Store2 FirstName_Store3
Aat Cat Dat
Cat Bat Zat
Cot
Eat
**Result intended**
FirstName_Store1 FirstName_Store2 FirstName_Store3
Aat
Bat
Cat Cat
Cot
Dat
Eat
Zat
我之所以想要结果是因为我想找出要删除的记录。我想从 Store1 中删除不在 store2 和 store3 中的值。
原始数据集包含大约 4000 条记录。正常字母表排序不显示 Store1 中与 Store2 和 Store3 并排的准确列值。
到目前为止我尝试过的事情:
匹配函数 Match (Store1,Store2:Store3,0) 结果出错。
我也尝试过 Vlookup,但是,Vlookup 不进行排序。
就我个人而言,我认为这是 XY Problem 您试图解决的问题与您的实际问题几乎没有任何关系。
The reason I intended result because I want to find out which record to delete. I want to delete the value from Store1 which is not in store2 and store3.
这种情况应该很容易解决,方法是从下到上遍历 FirstName_Store1 列中的每个单元格,并对其余列执行 application.countif
.
话虽如此,这里是您实际提出的问题的一种解决方案。
Option Explicit
Sub sortAndSift()
Dim i As Long, j As Long, m As Variant, n As Variant
With Worksheets("sheet6")
With .Cells(1, 1).CurrentRegion
With .Cells.Resize(.Rows.Count - 1, .Columns.Count).Offset(1, 0)
'store the original values
m = .Value2
End With
'put all values into column A
For i = 2 To .Columns.Count
With .Range(.Cells(2, i), .Cells(Rows.Count, i).End(xlUp))
.Parent.Cells(.Parent.Rows.Count, 1).End(xlUp).Resize(.Rows.Count, .Columns.Count).Offset(1, 0) = .Value
End With
Next i
End With
'reassert CurrentRegion since it probably changed
With .Cells(1, 1).CurrentRegion
With .Columns(1).Cells
'remove duplicates from column A
.RemoveDuplicates Columns:=1, Header:=xlYes
'sort column A
.Sort Key1:=.Columns(1), Order1:=xlAscending, _
Orientation:=xlTopToBottom, Header:=xlYes
End With
'put a copy of the expanded, de-duplicated and sorted column A
' in all other columns and make a copy of the values
With .Cells.Resize(.Rows.Count - 1, .Columns.Count).Offset(1, 0)
.FillRight
n = .Value2
End With
'remove values from each 'column' in the array that were not in the original
For i = LBound(n, 1) To UBound(n, 1)
For j = LBound(n, 2) To UBound(n, 2)
If IsError(Application.Match(n(i, j), Application.Index(m, 0, j), 0)) Then
n(i, j) = vbNullString
End If
Next j
Next i
End With
'put values back on the worksheet
.Cells(2, 1).Resize(UBound(n, 1), UBound(n, 2)) = n
End With
End Sub
可以通过将数据放入数据透视表轻松创建唯一列表(如何 虽然对于 SO ref 来说似乎偏离了主题)。例如,结果可能是 Copy/Paste Special/Values、应用删除重复项、按字母顺序排序然后放置在 ColumnE 中。
然后在 F2 中向下复制到 H8:
=IFERROR(IF(MATCH($E2,A:A,0),$E2),"")
复制标题。
假设 Aat
在 A2 中。
我正在清理数据。我从多个 excel 电子表格中收集数据。 我正在尝试在完全匹配的基础上实现排序,即
**Original dataset**
FirstName_Store1 FirstName_Store2 FirstName_Store3
Aat Cat Dat
Cat Bat Zat
Cot
Eat
**Result intended**
FirstName_Store1 FirstName_Store2 FirstName_Store3
Aat
Bat
Cat Cat
Cot
Dat
Eat
Zat
我之所以想要结果是因为我想找出要删除的记录。我想从 Store1 中删除不在 store2 和 store3 中的值。
原始数据集包含大约 4000 条记录。正常字母表排序不显示 Store1 中与 Store2 和 Store3 并排的准确列值。
到目前为止我尝试过的事情: 匹配函数 Match (Store1,Store2:Store3,0) 结果出错。 我也尝试过 Vlookup,但是,Vlookup 不进行排序。
就我个人而言,我认为这是 XY Problem 您试图解决的问题与您的实际问题几乎没有任何关系。
The reason I intended result because I want to find out which record to delete. I want to delete the value from Store1 which is not in store2 and store3.
这种情况应该很容易解决,方法是从下到上遍历 FirstName_Store1 列中的每个单元格,并对其余列执行 application.countif
.
话虽如此,这里是您实际提出的问题的一种解决方案。
Option Explicit
Sub sortAndSift()
Dim i As Long, j As Long, m As Variant, n As Variant
With Worksheets("sheet6")
With .Cells(1, 1).CurrentRegion
With .Cells.Resize(.Rows.Count - 1, .Columns.Count).Offset(1, 0)
'store the original values
m = .Value2
End With
'put all values into column A
For i = 2 To .Columns.Count
With .Range(.Cells(2, i), .Cells(Rows.Count, i).End(xlUp))
.Parent.Cells(.Parent.Rows.Count, 1).End(xlUp).Resize(.Rows.Count, .Columns.Count).Offset(1, 0) = .Value
End With
Next i
End With
'reassert CurrentRegion since it probably changed
With .Cells(1, 1).CurrentRegion
With .Columns(1).Cells
'remove duplicates from column A
.RemoveDuplicates Columns:=1, Header:=xlYes
'sort column A
.Sort Key1:=.Columns(1), Order1:=xlAscending, _
Orientation:=xlTopToBottom, Header:=xlYes
End With
'put a copy of the expanded, de-duplicated and sorted column A
' in all other columns and make a copy of the values
With .Cells.Resize(.Rows.Count - 1, .Columns.Count).Offset(1, 0)
.FillRight
n = .Value2
End With
'remove values from each 'column' in the array that were not in the original
For i = LBound(n, 1) To UBound(n, 1)
For j = LBound(n, 2) To UBound(n, 2)
If IsError(Application.Match(n(i, j), Application.Index(m, 0, j), 0)) Then
n(i, j) = vbNullString
End If
Next j
Next i
End With
'put values back on the worksheet
.Cells(2, 1).Resize(UBound(n, 1), UBound(n, 2)) = n
End With
End Sub
可以通过将数据放入数据透视表轻松创建唯一列表(如何 虽然对于 SO ref 来说似乎偏离了主题)。例如,结果可能是 Copy/Paste Special/Values、应用删除重复项、按字母顺序排序然后放置在 ColumnE 中。
然后在 F2 中向下复制到 H8:
=IFERROR(IF(MATCH($E2,A:A,0),$E2),"")
复制标题。
假设 Aat
在 A2 中。