Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 返回唯一值并避免在未筛选的范围内循环_Excel_Vba_Filtering - Fatal编程技术网

Excel 返回唯一值并避免在未筛选的范围内循环

Excel 返回唯一值并避免在未筛选的范围内循环,excel,vba,filtering,Excel,Vba,Filtering,第一篇文章在这里,所以我希望我足够清楚 我在工作表上有一张桌子,我正在使用它。我已将listObject传递给一个类,该类可以从中返回各种数据位。我想通过过滤指定的列标题来检索唯一列表 我的问题是: 我是否可以返回一个包含所有行的范围(过滤后),而不手动循环整个未过滤的范围 我当前的代码在(未过滤的)范围内循环,查找唯一的条目,如下所示。在我的测试工作表上花费了大量的时间,所以不要认为它对于操作示例是可行的 Public Function returnUniqueList(col As Stri

第一篇文章在这里,所以我希望我足够清楚

我在工作表上有一张桌子,我正在使用它。我已将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
公共函数returnUniqueList(列作为字符串)作为集合
'从表中获取unqiue列表。适用于LCP或镇流器类型
'作为字符串列表返回
作为整数的Dim i
将r设置为Excel.Range
Dim reqCol为整数
将字符串设置为字符串
'具有唯一值的字符串集合
将字符串设置为新集合
reqCol=返回COLD(col)
关于错误转到错误捕获
'收集唯一值
对于pLO.Range.rows中的每个r
如果未收集(返回字符串,r.单元格(1,reqCol)),则
'添加到集合,包括密钥
如果r.单元格(1,reqCol)“,则
retString.Add r.Cells(1,reqCol),r.Cells(1,reqCol)
如果结束
如果结束
下一个r
设置returnUniqueList=retString
退出功能
错误捕获:
MsgBox“返回唯一列表时出错:”+错误说明
端函数

因此,在对各种内置excel/VBA功能进行了一些修改之后,我选择了高级过滤器。我遇到的一个问题是,当我对一列进行过滤时,我想将过滤后的表返回给调用的代码段。上述函数现在看起来如下所示:

Public Function returnUniqueList(col As String, searchTerm As String) As Excel.range
' get unique lists from the table.  Useful for things like LCPs or ballast types
' returns as excel.range

Dim reqCol As Integer

On Error GoTo errorCatch

reqCol = returnColId(col)
Dim critRange As String
Dim cr As Excel.range


critRange = "=""=" + searchTerm + "*"""

pWkSht.Cells(1, 1000) = col
pWkSht.Cells(2, 1000) = critRange

Set cr = pWkSht.range(pWkSht.Cells(1, 1000), pWkSht.Cells(2, 1000))
' filter for unique entries on this column
pLO.range.Columns(reqCol).Select
pLO.range.Columns(reqCol).AdvancedFilter Action:=xlFilterInPlace, Unique:=True, CriteriaRange:=cr


Set returnUniqueList = pLO.range.SpecialCells(xlCellTypeVisible).EntireRow
pWkSht.Cells(1, 1000) = Empty
pWkSht.Cells(2, 1000) = Empty
Exit Function

errorCatch:
MsgBox "Error returning unique list: " + Err.Description

End Function
我发现一件棘手的事情是在调用函数中处理范围。我发现excel范围可以包含“区域”。这是由于excel处理连续数据的方式。因此,在调用函数时,我必须遍历返回范围中的区域。这确实给我希望避免的原始调用函数增加了一定程度的开销(我希望返回一个范围,其中有一个可以轻松迭代的区域)

我发现迭代从上面返回的范围/区域的最可靠的方法是基于这个片段,我以这样或那样的方式在加载位置时使用它(从表中提取不同的列,等等:

Set devices = edh.returnUniqueList("DaliCct", lcp)
' filter by the requested LCP

'clear down the dali ccts box
daliCctsListBox.Clear

' cycle through the returned areas, retrieving the relvant info
For i = 1 To devices.Areas.Count
    For rowInd = 1 To devices.Areas(i).rows.Count
        Dim r As Excel.range
        For Each r In devices.Areas(i).rows(rowInd)

         If (r.Cells(daliCctColId) <> "") And (r.Cells(daliCctColId) <> "DaliCct") Then
             daliCctsListBox.AddItem r.Cells(daliCctColId)
             bAdded = True
         End If
        Next r
    Next rowInd
Next i
Set devices=edh.returnUniqueList(“DaliCct”,lcp)
'按请求的LCP进行筛选
“清理大理ccts箱
daliCctsListBox,清除
'在返回的区域中循环,检索相关信息
对于i=1到devices.Areas.Count
对于rowInd=1的设备。区域(i)。行数
将r设置为Excel.range
对于设备中的每个r。区域(i)。行(rowInd)
如果(r.Cells(dalictcolid)”和(r.Cells(dalictcolid)“DaliCct”),则
daliCctsListBox.AddItem r.CELL(daliCctColId)
加上记号=正确
如果结束
下一个r
下一个罗温德
接下来我