VBA 中关于 Bloomberg 函数的问题

Problems in VBA with Bloomberg function on a LOOP

我正在尝试使用此代码以两个单元格的步骤将公式从 A2 引入到我的数据库的末尾。

Sub addbdh()

Dim i As Integer
Dim n As Integer

Range("A3").Select
Range(Selection, Selection.End(xlToRight)).Select

n = Selection.Count

For i = 1 To n

Cells(3, i * 2 - 1).Formula = "BDH(""A"" & "i * 2 - 1", "A1", "B1, "hoy()")"

Next i

End Sub

问题是我在指令上遇到错误:

Cells(3, i * 2 - 1).Formula = "BDH(""A"" & "i * 2 - 1", ""A1"", ""B1"", "Today()")"

特别是在 "i * 2 - 1" 部分,我得到一个 Excel MsgBox,上面写着 "Expected end of statement" (西班牙语:"se esperaba: fin de la instrucción".

¿谁能帮我找出代码中的错误?

非常感谢你。

一般来说,.Formula显示的是真正的英文公式。无论你的语言是什么。因此,Hoy() 应更改为 Today()

一般来说,这样试试:

Cells(3, i * 2 - 1).Formula = "=BDH(A" & i * 2 - 1 & ", A1, B1, Today())"

一般来说,当您遇到类似 "How to translate a working Excel formula to VBA" 的问题时,请执行以下操作:

  1. Select Excel.
  2. 中的工作公式
  3. 运行下面的代码。
  4. 从立即数 window 中取出公式并稍微修正一下以放入变量 i * 2 - 1.

Public Sub PrintMeUsefulFormula()

    Dim strFormula  As String
    Dim strParenth  As String

    strParenth = """"

    strFormula = Selection.Formula
    strFormula = Replace(strFormula, """", """""")

    strFormula = strParenth & strFormula & strParenth
    Debug.Print strFormula

End Sub