How can I use a for each loop on an array?

前端 未结 4 1537
南旧
南旧 2020-12-07 15:19

I have an array of Strings:

Dim sArray(4) as String

I am going through each String in the array:

for each element in sarray         


        
4条回答
  •  独厮守ぢ
    2020-12-07 16:21

    I use the counter variable like Fink suggests. If you want For Each and to pass ByRef (which can be more efficient for long strings) you have to cast your element as a string using CStr

    Sub Example()
    
        Dim vItm As Variant
        Dim aStrings(1 To 4) As String
    
        aStrings(1) = "one": aStrings(2) = "two": aStrings(3) = "three": aStrings(4) = "four"
    
        For Each vItm In aStrings
            do_something CStr(vItm)
        Next vItm
    
    End Sub
    
    Function do_something(ByRef sInput As String)
    
        Debug.Print sInput
    
    End Function
    

提交回复
热议问题