vba:对于每个数组 [英] vba: for each on an array
本文介绍了vba:对于每个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个字符串数组
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_something
将一个字符串作为参数
do_something
takes a string as a parameter
我收到将字符串作为字符串传递的错误。我应该将元素转换为字符串还是某些东西?
i am getting an error passing the element as a string. should i be converting the element to a string or something?
推荐答案
元素需要是一个变体,所以你不能声明它作为一个字符串。您的函数应该接受一个变体,如果它是一个字符串,只要你传递ByVal。
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.
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)
这篇关于vba:对于每个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文