简体   繁体   English

SpecialCells(xlCellTypeVisible)不会将所有值返回到数组(Excel 2013)

[英]SpecialCells(xlCellTypeVisible) not returning all values to array (Excel 2013)

I have a range with 170000 rows in it. 我有一个包含170000行的范围。 I'm filtering column A for a single value and returning the corresponding values in column B. 我为单个值过滤A列,并在B列中返回相应的值。

I want these values to dump into an array so I can quickly toss them into a dictionary (with the key being the value I filtered column A with). 我希望将这些值转储到数组中,以便快速将它们放入字典中(键是我过滤A列的值)。

The problem is that SpecialCells(xlCellTypeVisible) is acting inconsistent. 问题在于SpecialCells(xlCellTypeVisible)的行为不一致。

If I do the same test on a smaller range, the values dump into the array just fine. 如果我在较小的范围内进行相同的测试,则值会正确转储到数组中。 But with a range as large as mine, it only returns the first value in the range. 但是,如果范围与我的一样大,它只会返回该范围内的第一个值。 Also, I can use the same line to copy to another sheet. 另外,我可以使用同一行复制到另一张纸上。 I just can't get it to populate the array. 我只是无法获取它来填充数组。

foo = ws1.Range(tbl1Name & "[ID]").SpecialCells(xlCellTypeVisible)

Works with small ranges, but returns only the first result in a range as large as mine (less than 50 results.) foo becomes an array containing all the variables. 可在较小范围内使用,但仅返回与我的范围一样大的范围内的第一个结果(小于50个结果。)foo成为包含所有变量的数组。

ws1.Range(tbl1Name & "[ID]").SpecialCells(xlCellTypeVisible).Copy ws2.Range("A1")

Works with the large range and copies all the relevant data successfully. 可广泛使用,并成功复制所有相关数据。

So my question: How do I populate the array without the extra step of copying to a blank worksheet when autofiltering a large table range? 所以我的问题是:在自动过滤较大的表范围时,如何在不执行复制到空白工作表的额外步骤的情况下填充数组? (Excel 2013) (Excel 2013年)

EDIT : requires a reference to "Microsoft Forms 2.0 Object Library" (should be near the top of the list of available references). 编辑 :需要对“ Microsoft Forms 2.0对象库”的引用(应位于可用引用列表的顶部附近)。 Or add a userform to your project and that will auto-add the reference (you can then remove the form...) 或者将用户窗体添加到您的项目中,这将自动添加引用(然后您可以删除窗体...)

This should work for a single column: 这应该适用于单个列:

Sub Tester()

    Dim rng, txt As String, cb As New DataObject, arr

    Set rng = ActiveSheet.Range("A2:A28").SpecialCells(xlCellTypeVisible)

    rng.Copy
    DoEvents
    cb.GetFromClipboard
    txt = cb.GetText
    arr = Split(txt, vbCrLf)
    Debug.Print LBound(arr), UBound(arr)

End Sub

If you had multiple columns you'd need to loop over each element of arr (splitting its value on tab) and transfer the values to a 2-d array. 如果您有多列,则需要遍历arr每个元素(在选项卡上拆分其值),然后将值传输到二维数组中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM