【发布时间】:2012-11-17 16:33:48
【问题描述】:
第一次在这里发帖,所以我希望我足够清楚。
我正在使用的工作表上有一个表格。我已将 listObject 传递给一个类,该类可以从中返回各种数据。我想通过过滤指定的列标题来检索唯一列表。
我的问题是这样的:
我能否在过滤后返回包含所有行的范围,而无需手动循环遍历整个未过滤的范围?
我当前的代码在(未过滤的)范围内循环,寻找唯一的条目,如下所示。在我的测试工作表上花费了相当多的时间,所以不要认为它对于操作示例是可行的。
Public Function returnUniqueList(col As String) As Collection
' get unqiue lists from the table. Useful for things like LCPs or ballast types
' returns as list of strings
Dim i As Integer
Dim r As Excel.Range
Dim reqCol As Integer
Dim tempString As String
' collection of strings with the unique values
Dim retString As New Collection
reqCol = returnColId(col)
On Error GoTo errorCatch
' collect the unique values
For Each r In pLO.Range.rows
If Not InCollection(retString, r.Cells(1, reqCol)) Then
' add to the collection, including the key
If r.Cells(1, reqCol) <> "" Then
retString.Add r.Cells(1, reqCol), r.Cells(1, reqCol)
End If
End If
Next r
Set returnUniqueList = retString
Exit Function
errorCatch:
MsgBox "Error returning unique list: " + Err.Description
End Function
【问题讨论】: