gpt4 book ai didi

arrays - 将字符串从数组传递给函数的类型不匹配错误(在 VBA 中)

转载 作者:行者123 更新时间:2023-12-04 22:03:33 25 4
gpt4 key购买 nike

我有这样的代码

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)))

但是将字符串转换为字符串似乎很奇怪......不是吗?
还有其他方法吗?

最佳答案

尝试将您的数组转换为变体,然后将其传递给函数。

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

关于arrays - 将字符串从数组传递给函数的类型不匹配错误(在 VBA 中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29534842/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com