gpt4 book ai didi

vba - 编译错误: Constant expression required

转载 作者:行者123 更新时间:2023-12-04 21:38:14 25 4
gpt4 key购买 nike

我偶然发现了一个编译错误,但不知道可能是什么问题。当试图将符号更改为输入变量( TickerID )时,我得到了错误,否则在输入例如“yhoo”作为雅虎股票名称时工作得很好。

代码

Private Sub CmdBtn_Add_Click()
'---------------------------------------------------------------------------------------'
' Checks that inputted ticker name is correct and calls import class after confirmation
'---------------------------------------------------------------------------------------'

' General Variables---------'
Dim TickerID As String: TickerID = UCase(Add_Instrument.TxtBox_Instrument.Value)
'--------------------------'

'Check if input field is not empty
If TickerID = "" Or Application.WorksheetFunction.IsText(TickerID) = False Then
MsgBox "Please provide a valid ticker ID"
Exit Sub
End If

Debug.Print TickerID

'Check Ticker name exists through YQLBuilder class
Dim YQLBuilder As YQLBuilder: Set YQLBuilder = New YQLBuilder
Call YQLBuilder.TickerCheck(TickerID)


' Call ImportData(TickerID)

' MsgBox "Please check the ticker name. It is in the wrong format"

End Sub
Public Sub TickerCheck(TickerID As String)
'---------------------------------------------------------------------------------------'
' Built 2014-11-05 Allows parsing of XML data through YAHOO API YQL
' 2014-12-21: Not fully built yet, see where it can be of use
'---------------------------------------------------------------------------------------'

' General Variables---------'
Const ConnStringStart As String = "http://query.yahooapis.com/v1/public/yql?q="
Const ConnStringLast As String = "&diagnostics=true&env=store://datatables.org/alltableswithkeys"
'---------------------------'


Const ConnStringInput As String = "select * from yahoo.finance.stocks where symbol='" _
& TickerID & "'" **<----- Error here!**

Debug.Print ConnStringStart & ConnStringInput & ConnStringLast

Dim YQLNodes As MSXML2.IXMLDOMNodeList
Dim YQLReq As MSXML2.DOMDocument60

Set YQLReq = New MSXML2.DOMDocument60

YQLReq.async = False
YQLReq.Load ConnStringStart & ConnStringInput & ConnStringLast

YQLReq.setProperty "SelectionNamespaces", "xmlns:f='http://www.yahooapis.com/v1/base.rng'"
Set YQLNodes = YQLReq.SelectNodes("//CompanyName")

Dim xNode As MSXML2.IXMLDOMNode

For Each xNode In YQLNodes

Debug.Print xNode.Text

Next xNode

Debug.Print YQLNodes.Length

End Sub

最佳答案

信息很明确。当你声明一个常量时,你给它的值也必须是常量。在这种情况下,它的一部分是参数 TickerId,它是可变的。不能用变量值声明常量。

要解决这个问题,我认为您可以使用 Dim而不是 Const并且根本不使 ConnStringInput 成为常量。

关于vba - 编译错误: Constant expression required,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27590606/

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