types不匹配错误将string传递给数组中的函数(在VBA中)

我有这样的代码

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)”上得到一个错误“types不匹配”

我尝试了不同的方式,但唯一的工作似乎是:

  myResult = myFunction(T, CStr(ArrayStrings(1))) 

但似乎很奇怪将string转换为string…不是吗? 有没有其他的方法?

事实上,将string转换为string并不富有魅力。

你有没有尝试过使用ByVal

Function myFunction(Tx1 As String, ByVal Tx2 As String) As String

这应该够了

尝试将你的数组作为一个变体,然后传递给函数。

 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