找到数组中所有空元素的行索引如何?

huangapple go评论60阅读模式
英文:

How to find index of row that has all empty elements in array?

问题

我想调整多维数组的大小,其中有许多空元素。

对于一维数组,我想出了以下简短的代码来找到第一个空元素的索引:

arr = Array(4, 6, 7, "fjf", Empty, Empty, Empty)

i = 1
While arr(i) <> Empty
    i = i + 1
Wend

但对于像下面这样的二维数组,是否有一种更直接的方法来识别所有元素为空的“行”的索引呢?在这种情况下应该是4。

Dim myArray(1 To 5, 1 To 7) As Variant
myArray(1, 1) = 1
myArray(1, 2) = 2
myArray(1, 3) = 3
myArray(1, 4) = 4
myArray(1, 5) = 5
myArray(2, 1) = 6
myArray(2, 2) = 7
myArray(2, 3) = 8
myArray(2, 4) = 9
myArray(2, 5) = 10
myArray(3, 1) = 6
myArray(3, 2) = 7
myArray(3, 3) = 8
myArray(3, 4) = 9
myArray(3, 5) = 10

希望这可以帮助你找到解决方案。

英文:

I'd like to redim the size of multidimensional array that has many empty elements,

For one dimensional array I came up with this short code to find the index of first empty element.

arr = Array(4, 6, 7, &quot;fjf&quot;, Empty, Empty, Empty)

i = 1
While arr(i) &lt;&gt; Empty
    i = i + 1
Wend

But is there a more direct way for a 2 dimensional array like below, to identify index of "row" with all empty elements? in this case should be 4.

Dim myArray(1 To 5, 1 To 7) As Variant
    myArray(1, 1) = 1
    myArray(1, 2) = 2
    myArray(1, 3) = 3
    myArray(1, 4) = 4
    myArray(1, 5) = 5
    myArray(2, 1) = 6
    myArray(2, 2) = 7
    myArray(2, 3) = 8
    myArray(2, 4) = 9
    myArray(2, 5) = 10
    myArray(3, 1) = 6
    myArray(3, 2) = 7
    myArray(3, 3) = 8
    myArray(3, 4) = 9
    myArray(3, 5) = 10

答案1

得分: 3

Get Elements Before First Empty Row

  • 获取第一个空行之前的元素

The Function

  • 函数

The Test

  • 测试

The Result

  • 结果

The 1D Method (Redim Preserve)

  • 一维方法(Redim Preserve

The 1D Test

  • 一维测试

The 1D Result

  • 一维结果
英文:

Get Elements Before First Empty Row

The Function

<!-- language: lang-vb -->

Function GetBeforeEmptyRow(ByVal Data As Variant) As Variant

    Dim LB1 As Long: LB1 = LBound(Data, 1)
    Dim UB1 As Long: UB1 = UBound(Data, 1)
    Dim LB2 As Long: LB2 = LBound(Data, 2)
    Dim UB2 As Long: UB2 = UBound(Data, 2)
    
    Dim r As Long, c As Long
    
    For r = LB1 To UB1
        For c = LB2 To UB2
            If Not IsEmpty(Data(r, c)) Then &#39; the row is not empty
                Exit For
            End If
        Next c
        If c &gt; UB2 Then &#39; the row is empty
            Exit For
        End If
    Next r
    
    UB1 = r - 1
    
    Dim dData &#39; initially &#39;Empty&#39; 
    
    If UB1 &gt;= LB1 Then
        ReDim dData(LB1 To UB1, LB2 To UB2)
        For r = LB1 To UB1
            For c = LB2 To UB2
                dData(r, c) = Data(r, c)
            Next c
        Next r
    &#39;Else &#39; the 1st row is empty; do nothing i.e. let the result be &#39;Empty&#39; 
    End If

    GetBeforeEmptyRow = dData

End Function

The Test

<!-- language: lang-vb -->

Sub Test()

    Dim myArray(1 To 5, 1 To 7) As Variant
    myArray(1, 1) = 1
    myArray(1, 2) = 2
    myArray(1, 3) = 3
    myArray(1, 4) = 4
    myArray(1, 5) = 5
    myArray(2, 1) = 6
    myArray(2, 2) = 7
    myArray(2, 3) = 8
    myArray(2, 4) = 9
    myArray(2, 5) = 10
    myArray(3, 1) = 6
    myArray(3, 2) = 7
    myArray(3, 3) = 8
    myArray(3, 4) = 9
    myArray(3, 5) = 10
    
    Dim Data(): Data = GetBeforeEmptyRow(myArray)
    
    Debug.Print &quot;Initial Limits [&quot; &amp; LBound(myArray, 1) &amp; &quot;,&quot; _
        &amp; UBound(myArray, 1) &amp; &quot;][&quot; &amp; LBound(myArray, 2) &amp; &quot;,&quot; _
        &amp; UBound(myArray, 2) &amp; &quot;]&quot;
    
    If IsEmpty(Data) Then
        Debug.Print &quot;The first row was empty.&quot;
    Else
        Debug.Print &quot;Result Limits  [&quot; &amp; LBound(Data, 1) &amp; &quot;,&quot; _
            &amp; UBound(Data, 1) &amp; &quot;][&quot; &amp; LBound(Data, 2) &amp; &quot;,&quot; _
            &amp; UBound(Data, 2) &amp; &quot;]&quot;
    End If

End Sub

The Result

Initial Limits [1,5][1,7]
Result Limits  [1,3][1,7]

The 1D Method (Redim Preserve)

<!-- language: lang-vb -->

Sub RemoveAfterEmpty(ByRef Arr As Variant)
    
    Dim LB As Long: LB = LBound(Arr)
    Dim UB As Long: UB = UBound(Arr)
    
    Dim n As Long
    
    For n = LB To UB
        If IsEmpty(Arr(n)) Then
            Exit For
        End If
    Next n
    
    n = n - 1
    
    If n &gt;= LB Then &#39; the 1st element is not empty
        If n &lt; UB Then
            ReDim Preserve Arr(LB To n)
        &#39;Else &#39; no empty elements; do nothing
        End If
    Else &#39; the 1st element is empty
        Arr = Array()
    End If

End Sub

The 1D Test

<!-- language: lang-vb -->

Sub Test1D()
    
    Dim Arr(): Arr = Array(4, 6, 7, &quot;fjf&quot;, Empty, Empty, Empty)
    
    Debug.Print &quot;Initial [&quot; &amp; LBound(Arr) &amp; &quot;,&quot; &amp; UBound(Arr) &amp; &quot;]&quot;
    Debug.Print Join(Arr, &quot;, &quot;)
    
    RemoveAfterEmpty Arr
    
    If UBound(Arr) &lt; LBound(Arr) Then
        Debug.Print &quot;The first element was empty.&quot;
    Else
        Debug.Print &quot;Result  [&quot; &amp; LBound(Arr) &amp; &quot;,&quot; &amp; UBound(Arr) &amp; &quot;]&quot;
        Debug.Print Join(Arr, &quot;, &quot;)
    End If

End Sub

The 1D Result

Initial [0,6]
4, 6, 7, fjf, , , 
Result  [0,3]
4, 6, 7, fjf

huangapple
  • 本文由 发表于 2023年5月30日 08:44:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76360992.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定