【问题标题】:Remove blank entries from an array loaded by a range从范围加载的数组中删除空白条目
【发布时间】:2014-09-24 20:55:35
【问题描述】:

我正在尝试从 Excel 数据表中名为 TY[L3 Name](1 列,X 行长)的字段加载的数组中删除空白条目。下面的代码旨在从数组中删除所有空白值(一旦加载了范围),并返回一个新数组,其中的行中只有数据。我稍后会想将此数组传递到一个集合中以删除重复项,但我想弄清楚为什么我不能先摆脱空白(现在我正处于我只想了解如何做到这一点无论我是否将其传递给其他东西)。

代码在 ReDim Preserve 行出错。我首先将 NewArr 调整为 MyArr 表,但最后返回了空白行。然后我尝试调整它的大小,因此我只有包含数据的行,但我似乎无法让 NewArr() 数组在没有错误的情况下执行此操作。

我正在使用即时窗口来验证没有空白条目(目前 TY[L3 Name] 范围末尾有 8 行)。

Sub BuildArray()

'   Load array
Dim MyArr()
Dim j As Long

'   Size array
MyArr() = Range("TY[L3 Number]")
ReDim NewArr(LBound(MyArr) To UBound(MyArr), 1)

'   For Loop to search for Blanks and remove from Array
'   The Lbound and UBound parameters will be defined by the size of the TY[L3 Number] field in the TY Table
For i = LBound(MyArr) To UBound(MyArr)
   If MyArr(i, 1) <> "" Then
        j = j + 1
        NewArr(j, 1) = MyArr(i, 1)
   End If
   Next i
ReDim Preserve NewArr(1 To j, 1) 'Error out here; "Subscript out of range." Can't seem to get this Array to new size without blank entries.

'   Debug Window to show results of revised array.
Dim c As Long
For c = LBound(NewArr) To UBound(NewArr)
   Debug.Print NewArr(c, 1)
Next
   Debug.Print "End of List"

End Sub

【问题讨论】:

    标签: arrays excel vba for-loop subscript


    【解决方案1】:

    在 VBA 中处理数组可能很棘手,但我认为下面的示例将向您展示填充“无空白”Array 的不同策略可能是如何工作的:

    假设我们从单个Worksheet 开始,CoolRange 的命名如下所示:

    可以像这样生成一个没有空格的数组:

    Option Explicit
    Sub BuildArrayWithoutBlanks()
    
    Dim AryFromRange() As Variant, AryNoBlanks() As Variant
    Dim Counter As Long, NoBlankSize As Long
    
    'set references and initialize up-front
    ReDim AryNoBlanks(0 To 0)
    NoBlankSize = 0
    
    'load the range into array
    AryFromRange = ThisWorkbook.Names("CoolRange").RefersToRange
    
    'loop through the array from the range, adding
    'to the no-blank array as we go
    For Counter = LBound(AryFromRange) To UBound(AryFromRange)
        If AryFromRange(Counter, 1) <> "" Then
            NoBlankSize = NoBlankSize + 1
            AryNoBlanks(UBound(AryNoBlanks)) = AryFromRange(Counter, 1)
            ReDim Preserve AryNoBlanks(0 To UBound(AryNoBlanks) + 1)
        End If
    Next Counter
    
    'remove that pesky empty array field at the end
    If UBound(AryNoBlanks) > 0 Then
        ReDim Preserve AryNoBlanks(0 To UBound(AryNoBlanks) - 1)
    End If
    
    'debug for reference
    For Counter = LBound(AryNoBlanks) To UBound(AryNoBlanks)
        Debug.Print (AryNoBlanks(Counter))
    Next Counter
    Debug.Print "End of List"
    
    End Sub
    

    所以,总而言之,我们:

    1. 为我们的最终数组创建一个一维数组,去掉空格
    2. 遍历我们的原始数组(带空格)
    3. 除非数组字段为空,否则我们增加非空计数器,然后将值添加到非空数组中,然后扩展非空数组
    4. 吹走我们非空数组中最后一个讨厌的空字段

    根据您的问题描述,听起来您最终会用Collection 删除重复项——喜欢它。出于好奇,您将使用 non-blank-but-with-duplicates 数组做什么?

    【讨论】:

    • 哇,谢谢。非常好的回应!在这一点上,我只是要把它从这里传递到集合中。我想我可以在收藏中完成这项任务,但我花了很多时间试图弄清楚如何删除空白(字面意思是 7 小时),以至于我不得不学习如何去做。现在开始学习如何使用我的新数组和集合删除欺骗!再次感谢您的好评。
    • 很高兴我能帮上忙——我想你会发现 Collection 对象对于像你这样的情况非常方便
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-29
    • 1970-01-01
    • 2018-08-09
    • 2017-02-15
    • 2020-10-01
    • 1970-01-01
    • 2012-12-24
    相关资源
    最近更新 更多