簡體   English   中英

檢查 VBA 中的空數組索引(可以包括對象)

[英]Check for empty array indexes in VBA (that can include objects)

我需要對 Variant 數組進行is nothing檢查,以排除使用空索引。 我使用is nothing來捕獲保存(空)對象的空索引,它工作正常,但對於保存常規數據類型(不是對象)的索引,它會引發異常。

    Dim arrArray() as Variant
    '... fill array with values but leave some indexes out

    'Loop through the array
    For i = LBound(arrArray) To UBound(arrArray)

        'Check if the current array item is an empty object
        If arrArray(i) Is Nothing Then
            'don't debug.print
        
        'Debug if it's not an empty object 
            Else
            Debug.Print arrArray(i)
            End If

        Next

我可以on error resume next使用,但由於錯誤處理是動態完成的,它會改變錯誤處理狀態,所以我想避免這種情況。 如果無法避免,請查看我的其他問題

注意:目前我只使用空對象,在將來的某個時候我可能會得到一個實際的對象。 所以從長遠來看,我將不得不檢查索引是否包含現有對象(否則 - 我認為 - debug.print 將引發錯誤)。

請嘗試下一個功能。 它將為各種元素類型返回一個已清理的數組(沒有空元素):

Function elimEmptyArrayElements(arrX As Variant) As Variant
 Dim i As Long, arrNoEmpty, k As Long
 ReDim arrNoEmpty(UBound(arrX)): k = 0
 For i = LBound(arrX) To UBound(arrX)
    If Not IsMissing(arrX(i)) Then
       If Not IsObject(arrX(i)) Then
            If TypeName(arrX(i)) = "String" Then
                If arrX(i) <> "" Then
                    arrNoEmpty(k) = arrX(i): k = k + 1
                End If
            Else
                If Not IsEmpty(arrX(i)) Then
                    arrNoEmpty(k) = arrX(i): k = k + 1
                End If
            End If
       Else
            Set arrNoEmpty(k) = arrX(i): k = k + 1
       End If
    End If
 Next i
 ReDim Preserve arrNoEmpty(k - 1)
 elimEmptyArrayElements = arrNoEmpty
End Function

請使用下一個Sub對其進行測試。 它將在每對初始/清理的數組表示上停止。 如果可能,兩個數組都加入到Immediate Window中。

如果不可能,則僅返回其元素的數量 ( Ubound(arr) )。 您可以在每個數組元素之間進行迭代,並看到不存在空元素:

Sub testElimEmptyArrayElements()
   Dim arr
   arr = Split("1,7,9,,10,5,6,,2,8,3,4", ",")
   Debug.Print Join(arr, "|") 'just to visually see the initial array content
   
   arr = elimEmptyArrayElements(arr)
   Debug.Print Join(arr, "|"): Stop 'the cleaned array
   
   arr = Application.Transpose(Range("A2:A20").value) 'a 1D array extracted from a column range
   Debug.Print Join(arr, "|")

    arr = elimEmptyArrayElements(arr)
    Debug.Print Join(arr, "|"): Stop 'the cleaned array

    arr = Array(1, 2, 3, , 4, , 5): Debug.Print "Initial number of numeric elements: " & UBound(arr)
    arr = elimEmptyArrayElements(arr): Debug.Print "Cleaned array number of numeric elements: " & UBound(arr): Stop

    arr = Array(Range("A2"), Range("A3"), , Range("A6")): Debug.Print "Initial number of Range Object elements: " & UBound(arr)
    arr = elimEmptyArrayElements(arr): Debug.Print "Cleaned array number of Range elements: " & UBound(arr): Stop
    
    arr = Array(ActiveSheet, , ActiveSheet.Next): Debug.Print "Initial number of Sheet Object elements: " & UBound(arr)
    arr = elimEmptyArrayElements(arr): Debug.Print "Cleaned array number of Sheet Object elements: " & UBound(arr): Stop
    
    arr = Array("my string", 100, Range("A2"), , ActiveSheet, , ThisWorkbook, "test", 6): Debug.Print "Initial number of variate elements: " & UBound(arr)
    arr = elimEmptyArrayElements(arr): Debug.Print "Cleaned array number of variate types elements: " & UBound(arr)
      Debug.Print arr(2).value        'the cell value
      Debug.Print arr(3).name         'the activesheet name
      Debug.Print arr(4).Sheets.count 'activeworkbook number of sheets
End Sub

您可以使用if YourArray(i)<>"" then語法簡單地檢查和過濾數組中的空槽

除此之外,我在您的代碼的第一行中看到一些錯誤的聲明問題: 1-您不能使用Array作為數組的名稱 2-您應該在數組名稱后使用括號(例如 Dim myArray() 作為變體)3 - 變量類型不能有括號(據我所知)

我建議聲明您的數組,如下所示:

dim arr()

這樣,它會自動被視為一個變體數組。 所以我建議的代碼是這樣的:

Dim arr()
'... fill array with values but leave some indexes out
For i = LBound(arr) To UBound(arr)
    If arr(i)<>"" Then
        'do nothing
    Else
        'do something
    end if
Next i

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM