[英]VBA macro and Excel advanced filter result on counting unique elements not match
My problem is that I have to write a macro what gives back the unique elements from a column. 我的问题是我必须编写一个宏,该宏可以返回列中的唯一元素。 I now that I can use advanced filter, but I have to do with macro for an assignment.
现在,我可以使用高级过滤器,但是必须与宏进行分配。 I can test the results of the macro with the filter, and sometimes the macro gives back good result, sometimes don't.
我可以使用过滤器测试宏的结果,有时宏可以返回良好的结果,有时却不能。 I know that the selection, selection part requires that I stay on the first row of the column.
我知道选择,选择部分要求我停留在该列的第一行。 The code is:
代码是:
Sub ElemSzámolás()
Dim lista() As String
Dim k As Integer
Dim oszlop As Range
k = 1
ReDim lista(0 to 1)
Set oszlop = ActiveSheet.Range(Selection, Selection)
Do While oszlop.Offset(k).Value <> ""
If UBound(Filter(lista, oszlop.Offset(k).Value)) = -1 Then
lista(Ubound(lista)) = oszlop.Offset(k).Value
ReDim Preserve lista(0 to UBound(lista) + 1)
End If
k = k + 1
Loop
MsgBox UBound(lista) & " db műsor van a listában", vbOKOnly, "Eredmény"
End Sub
Thanks in advance! 提前致谢!
My problem is that I have to write a macro what gives back the unique elements from a column.
我的问题是我必须编写一个宏,该宏可以返回列中的唯一元素。
Is this what you are trying? 这是您要尝试的吗? This will work on multiple columns as well.
这也将适用于多列。
Sub ElemSzámolás()
Dim oszlop As Range, aCell As Range
Dim col As New Collection, itm
'~~> Check if what the user selected is a valid range
If TypeName(Selection) <> "Range" Then
MsgBox "Select a range first."
Exit Sub
End If
Set oszlop = Selection
'~~> Create a unique collection
For Each aCell In oszlop
If Not aCell = "" Then
On Error Resume Next
col.Add aCell.Value, CStr(aCell.Value)
On Error GoTo 0
End If
Next
'~~> This will give you the unique count
MsgBox col.count & " unique items found"
'~~> This will give you each of those unique items
For Each itm In col
Debug.Print itm
Next
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.