使用VBA获取在VBA中使用的唯一值? [英] Use VBA to get Unique values for use within VBA?

查看:238
本文介绍了使用VBA获取在VBA中使用的唯一值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前将会使用像Range,Cells这样的东西,或许有许多不一样的基本原则。

I currently would use something like this either with Range, Cells or the like many different ways same basic principle.

Range("A1", Range("A1").End(xlDown)).AdvancedFilter Action:=xlFilterCopy, _
    CopyToRange:=Range("IV1"), Unique:=True

Dim myArr as Variant 
myArr = Range("IV1", Range("IV1").End(xlDown))
Columns("IV").Delete

有没有办法直接将这些唯一值加载到VBA中的任何类型的对象中,而不需要复制到另一个位置?

Is there a way to directly load those unique values into any type of object in VBA without the need to copy to another location?

推荐答案

您可以使用 Collection Object 来创建唯一的条目。例如

You can use a Collection Object to create unique entries. For example

Sub Sample()
    Dim Col As New Collection
    Dim itm
    Dim i As Long
    Dim CellVal As Variant

    '~~> Lets say looping through Row 1 to 22 For 
    '~~> Range("A1:A22") as mentioned in your recent comment
    For i = 1 To 22
        CellVal = Sheets("Sheet1").Range("A" & i).Value
        On Error Resume Next
        Col.Add CellVal, Chr(34) & CellVal & Chr(34)
        On Error GoTo 0
    Next i

    For Each itm In Col
        Debug.Print itm
    Next
End Sub

ScreenShot

这篇关于使用VBA获取在VBA中使用的唯一值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆