簡體   English   中英

有什么辦法可以將 append 字符串轉換為數組?

[英]Any way to append strings to Array?

我如何將這些字符串 append 放入一個數組中?
我想附加的字符串

我正在嘗試將 go 從水果列表中刪除,並將當前不在數組中的任何水果添加到其中。 (看到 apple 和 orange 不在數組中,因此添加它。然后看到以下蘋果在數組中,因此忽略它並繼續添加檸檬。)

Public Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean
    Dim i
    For i = LBound(arr) To UBound(arr)
        If arr(i) = stringToBeFound Then
            IsInArray = True
            Exit Function
        End If
    Next i
    IsInArray = False

End Function


Public Sub Create_INDIVIDUAL_TABLES()

Dim tArray As String
Dim t1 As Integer
Dim t2 As Integer
Dim i As Integer

tArray = Array()
t1 = 3
t2 = 3
i = 0

Do While Cells(t1, "A").Value <> ""

    If IsInArray(Cells(t1, "B"), tArray) Then
        t1 = t1 + 1
    Else
        tArray(i) = Cells(t1, "B")
        i = i + 1
        t1 = t1 + 1
    End If
Loop

您可以通過使用 Redim Preserve 並更改其他一些內容來調整您的原始代碼來執行此操作。

Public Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean
Dim idx As Long

    For idx = LBound(arr) To UBound(arr)
        If arr(idx) = stringToBeFound Then
            IsInArray = True
            Exit Function
        End If
    Next idx

End Function


Public Sub Create_INDIVIDUAL_TABLES()
Dim tArray() As String
Dim cnt As Long
Dim t1 As Long

    ReDim tArray(1 To 1)
    t1 = 3
    cnt = 1
    tArray(cnt) = Cells(t1, "B")

    Do
        
        If Not IsInArray(Cells(t1, "B"), tArray) Then
            cnt = cnt + 1
            ReDim Preserve tArray(1 To cnt)
            tArray(cnt) = Cells(t1, "B")
        End If
        t1 = t1 + 1
    Loop Until Cells(t1, "A").Value = ""
    
End Sub

但是,如果您嘗試從列表中獲取唯一項目,您應該真正考慮使用collectionsdictionaries

暫無
暫無

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

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