vba 如何在数组上使用 for each 循环?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4228137/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 10:57:08  来源:igfitidea点击:

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

excelvba

提问by l--''''''---------''''''''''''

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
  do_something(element)
next element

do_somethingtakes a string as a parameter

do_something接受一个字符串作为参数

I am getting an error passing the element as a String:

我收到一个错误,将元素作为字符串传递:

ByRef Argument Mismatch

ByRef 参数不匹配

Should I be converting the element to a String or something?

我应该将元素转换为字符串或其他东西吗?

回答by Bobsickle

Element needs to be a variant, so you can't declare it as a string. Your function should accept a variant if it is a string though as long as you pass it ByVal.

Element 需要是一个变体,所以你不能将它声明为一个字符串。你的函数应该接受一个变体,如果它是一个字符串,但只要你传递它 ByVal。

Public Sub example()
    Dim sArray(4) As string
    Dim element As variant

    For Each element In sArray
        do_something (element)
    Next element
End Sub


Sub do_something(ByVal e As String)

End Sub

The other option is to convert the variant to a string before passing it.

另一种选择是在传递变量之前将其转换为字符串。

  do_something CStr(element)

回答by Fink

A for each loop structure is more designed around the collection object. A For..Each loop requires a variant type or object. Since your "element" variable is being typed as a variant your "do_something" function will need to accept a variant type, or you can modify your loop to something like this:

一个 for each 循环结构更多是围绕集合对象设计的。For..Each 循环需要一个变体类型或对象。由于您的“元素”变量被输入为变体,因此您的“do_something”函数需要接受变体类型,或者您可以将循环修改为如下所示:

Public Sub Example()

    Dim sArray(4) As String
    Dim i As Long

    For i = LBound(sArray) To UBound(sArray)
        do_something sArray(i)
    Next i

End Sub

回答by Dick Kusleika

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

我使用 Fink 建议的计数器变量。如果您想要 For Each 并传递 ByRef(对于长字符串可能更有效),则必须使用 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

回答by Sebastian Viereck

what about this simple inArray function:

这个简单的 inArray 函数怎么样:

Function isInArray(ByRef stringToBeFound As String, ByRef arr As Variant) As Boolean
For Each element In arr
    If element = stringToBeFound Then
        isInArray = True
        Exit Function
    End If
Next element
End Function