Type mismatch when passing string variable into Variant parameter, and assigning result of Array function to parameter

后端 未结 2 1477
囚心锁ツ
囚心锁ツ 2020-12-11 20:27

I have the following two procedures:

Sub OuterSub()
    Dim s As String
    s = \"Lorem ipsum dolor sit amet\"
    InnerSub s
End Sub

Sub InnerSub(prm As Va         


        
2条回答
  •  执念已碎
    2020-12-11 21:02

    I don't know the reason, but it appears that when passing byref the expection is throw. String is passed by reference, but if you put a parenthesis around it, it is passed by value:

    Sub OuterSub()
        Dim s As String
        s = "Lorem ipsum dolor sit amet"
        InnerSub (s)
    End Sub
    

    Another option, if you don't want to change the outer function, is to define the InnerSub prm by value:

    Sub InnerSub(ByVal prm As Variant)
        prm = Array(prm)
    End Sub
    

提交回复
热议问题