VBA 字符串变量不会在字符串连接中放置常量字符串值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16888863/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 21:24:48  来源:igfitidea点击:

VBA string variables does not put constant string value in string concatenating

stringvba

提问by Jim Xie

all. My code like this:

全部。我的代码是这样的:

Sub mySub

Public Const strVarA As String = "AA"
Public Const strVarB As String = "BB"
Public Const strVarC As String = "CC"

Dim myStrArray() As String
Dim myString As String
myStrArray = ["strVarA", "strVarB", "strVarC"]

For i = 0 To 2
    myURL = "http://www.yahoo.com/news/" & myStrArray(i) & ".html" <--- Here, wrong
    '//do something.
Next i

End Sub

My purpose is in the For loop, myStrArray(0) should put its constant value "AA" in the URL, like this:

我的目的是在 For 循环中, myStrArray(0) 应该将其常量值“AA”放在 URL 中,如下所示:

"http://www.yahoo.com/news/AA.html"

because the URL use "AA" in its link. But the code always put "strVarA", like this:

因为 URL 在其链接中使用“AA”。但是代码总是放“strVarA”,像这样:

"http://www.yahoo.com/news/strVarA.html"

Similar as when loop to myStrArray(1), it should put "BB" in URL, myStrArray(2) should put "CC" in URL. I tried many ways, no luck. Does somebody have experience and suggestions about this? Thanks!

与循环到 myStrArray(1) 时类似,它应该在 URL 中放置“BB”,myStrArray(2) 应该在 URL 中放置“CC”。我尝试了很多方法,没有运气。有人有这方面的经验和建议吗?谢谢!

采纳答案by Santosh

Try below code :

试试下面的代码:

Sub mySub()

 Const strVarA As String = "AA"
 Const strVarB As String = "BB"
 Const strVarC As String = "CC"

'Dim myStrArray() As String
Dim myString As String
myStrArray = Array("strVarA", "strVarB", "strVarC")

For i = LBound(myStrArray) To UBound(myStrArray)
    myURL = "http://www.yahoo.com/news/" & myStrArray(i) & ".html"
    '//do something.
Next i

End Sub

enter image description here

在此处输入图片说明

Updated after comments

评论后更新

Sub mySub()

    Dim myStrArray As Variant
    myStrArray = Array("AA", "BB", "CC")

    For i = LBound(myStrArray) To UBound(myStrArray)
        myurl = "http://www.yahoo.com/news/" & myStrArray(i) & ".html"
        MsgBox myurl
    Next

End Sub