我有这样的代码
Sub Test()
Dim ArrayStrings()
Dim T As String, myResult As String
ArrayStrings = Array("String1", "String2", "String3", "String4", "String5", "StringN")
T = "myString"
myResult = myFunction(T, ArrayStrings(1))
End Sub
Function myFunction(Tx1 As String, Tx2 As String) As String
'myCode
End Function
这会在“ArrayStrings(1)”
上出现错误“类型不匹配”我尝试了不同的方式,但唯一的工作似乎是:
myResult = myFunction(T, CStr(ArrayStrings(1)))
但将字符串转换为字符串似乎很奇怪......不是吗? 还有其他办法吗?
答案 0 :(得分:2)
尝试将数组转换为变量,然后将其传递给函数。
Option Explicit
Sub Test()
Dim arr() As Variant
Dim str As String
arr = Array("1", "2", "3")
str = "Hello, world!"
MsgBox myFunction(str, arr)
End Sub
Function myFunction(str As String, arr As Variant) As String
Dim var As Variant
Dim strOutput As String
For Each var In arr
strOutput = strOutput & " " & var
Next var
myFunction = strOutput
End Function
答案 1 :(得分:1)
确实,将字符串转换为字符串并不富有魅力...
您是否尝试过使用ByVal
?
Function myFunction(Tx1 As String, ByVal Tx2 As String) As String
那应该够了