简体   繁体   English

如何将变量插入期望的字符串中,从而引起问题? Excel的VBA

[英]How do I insert a variable into an expected string, delimiting issue? Excel-VBA

I am trying to retrieve data from Yahoo Finance through the WEBSERVICE function and then populate the data through a loop. 我正在尝试通过WEBSERVICE函数从Yahoo Finance检索数据,然后通过循环填充数据。 I know the issue is with the apostrophes, since there are two variables I am not sure how to delimit them. 我知道问题在于撇号,因为有两个变量我不确定如何定界它们。 I've tried combinations of " & symbol & ", double apostrophes, nothing worked. 我试过了“&symbol&”,双撇号的组合,没有任何效果。

Any help is greatly appreciated, thank you! 任何帮助,不胜感激,谢谢!

The code is: 代码是:

Sub marketdata()

    Dim symbol As String
    Dim parameter As String
    Dim rows As Long
    Dim cols As Long

    For rows = 2 To 5700
        For cols = 3 To 20
            symbol = Cells(rows, 2).Value
            parameter = Cells(1, cols).Value
            Cells(rows, cols).Value = "=WEBSERVICE _
            (""https://download.finance.yahoo.com/d/quotes.csv?s=SYMBOL&f=PARAMETER"")"
        Next cols
    Next rows

End Sub

This is my first post, please let me know if I need to clarify anything. 这是我的第一篇文章,如果需要澄清,请告诉我。

I would use two Replace operations to put the cell values into the string. 我将使用两次替换操作将单元格值放入字符串中。

Dim sp As String
Dim rows As Long
Dim cols As Long

For rows = 2 To 5700
    For cols = 3 To 20
        sp = "=WEBSERVICE(""https://download.finance.yahoo.com/d/quotes.csv?s=%1%&f=%2%"")"
        sp = Replace(Replace(sp, "%1%", Cells(rows, 2).Value), "%2%", Cells(1, cols).Value)
        Debug.Print sp
        Cells(rows, cols).Value = sp    'should this be .Formula?
    Next cols
Next rows

You might also wish to start including parent worksheet references. 您可能还希望开始包括父级工作表引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM