|
取A列的不重复值(VBA正常、VSTO中不成功)——请教老师们指教,谢谢!
取A列的不重复值
1、Excel VBA中:正常取到A列的不重复值
Sub test()
Dim d, i, arr
Set d = CreateObject("Scripting.Dictionary")
arr = ActiveSheet.Range("A2:A" & ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row)
For i = 1 To UBound(arr) '数组数据最大行数
d(arr(i, 1)) = ""
Next i
ActiveSheet.Cells(2,2).Resize(d.Count) = WorksheetFunction.Transpose(d.keys) '取A列不重复值(竖向排)
Set d = Nothing
End Sub
2、VSTO中:没有实现把A列的重复值去掉 —— 失败:没有获取到不重复(唯一值),而是原来的数据(没有变化)
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'On Error Resume Next '防错(Error)——遇到错误代码,自动跳到下一步
Dim d
Dim i As Integer
Dim arr As Object
Dim Brr As Array
d = CreateObject("Scripting.Dictionary")
arr = Exapp.ActiveSheet.Range("A2:A" & Exapp.ActiveSheet.Cells(Exapp.Rows.Count, 1).End(-4162).Row)
For i = 1 To UBound(arr()) '数组数据最大行数
d(arr(i, 1)) = ""
Next i
Exapp.ActiveSheet.Cells(2, 2).Resize(d.Count) = Exapp.WorksheetFunction.Transpose(d.keys) '取A列不重复值
d = Nothing
End Sub
模块代码:
Module Module1
Public Exapp As Excel.Application = Globals.ThisAddIn.Application
End Module
|
|