EXCEL中怎样用VBA去掉一列数据中的重复数据?
展开全部
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Count > 1 Then Exit Sub
If Target.Address(0, 0) <> "B1" Then Exit Sub
Dim dic As Object
arr = Range([a1], [a65536].End(xlUp))
Set dic = CreateObject("scripting.dictionary")
For i = 1 To UBound(arr, 1)
dic(arr(i, 1)) = ""
Next
[a65536].End(xlUp).Offset(-dic.Count + 1, 1).Resize(dic.Count, 1) = Application.Transpose(dic.keys)
End Sub
右键工作表标签》粘贴代码
追问
我试了下还可以,有个问题请修改下。就是有重复的数保留下面的数。我看了现在是去掉了下面的数,保留了上面的数。如:A10,253。A16,253。现在运行VBA是保留了A10,我想保留A16的。
追答
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Count > 1 Then Exit Sub
If Target.Address(0, 0) <> "B1" Then Exit Sub
Dim dic As Object
arr = Range([a1], [a65536].End(xlUp))
Set dic = CreateObject("scripting.dictionary")
For i = 1 To UBound(arr, 1)
If dic.exists(arr(i, 1)) Then dic.Remove (arr(i, 1))'加一行,先删除,再添加
dic(arr(i, 1)) = ""
Next
[a65536].End(xlUp).Offset(-dic.Count + 1, 1).Resize(dic.Count, 1) = Application.Transpose(dic.keys)
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询