我已经看过几次这个问题,但是找不到适合我情况的解决方案,所以在这里:
我有一个相当复杂的公式,我想插入一个单元格(复杂,因为它在接下来的痛苦中)。我在变量中设置了组件,运行子组件在locals窗口中给出了正确的最终变量名称,但是当我尝试将单元格设置为公式时,我得到了一个' 1004:应用程序定义的对象定义的错误'。输出应如下所示:
单元格A1:= BDS(" 0"," pg_segment"," dir = h"," number_of_periods = -3")
然而它什么也没有返回。
我尝试过以下操作:将最终变量(cmdstr0)设置为整数 - 这是有效的。作为一个字符串(" asdf")也可以。直接改变值(.value =" string")有效。唯一不起作用的是VBA构建公式本身以插入字符串。这是代码,谢谢你:
sub populate_revenues()
'field = bloomberg field to look up
'direction = output direction (horizontal/vertical)
'geoverride = override to display only geo or product segments
'periods = numbe of periods to display
'cmdstr = the string to be output that will download the data
Dim field As String
Dim direction As String
Dim geoverride As String
Dim periods As String
Dim cmdstr3 As String
Dim cmdstr2 As String
Dim cmdstr1 As String
Dim cmdstr0 As Variant
Let cmdstr2 = 0
Let field = Worksheets("output").Cells(1, 3).Value
Let direction = "Dir = " & Worksheets("output").Cells(1, 5).Value
Let geoverride = " product_geo_override = " & Worksheets("output").Cells(1, 7).Value
Let periods = " number_of_periods = " & Worksheets("output").Cells(2, 3).Value
Let cmdstr1 = "=BDS(" & Chr(34)
Let cmdstr3 = Chr(34) & "," & Chr(34) & field & ", " & Chr(34) _
& direction & Chr(34) & ", " & Chr(34) & geoverride & Chr(34) & ", " & Chr(34) & periods & Chr(34) & ")"
Let cmdstr0 = cmdstr1 & cmdstr2 & cmdstr3
'Let cmdstr0 = 1
Let Worksheets("Sheet2").Cells(10, 1).Value = cmdstr0
End Sub
此外,任何人都可以告诉我,如果有更快的方式格式化代码而不是每行四次击中空格键吗?
答案 0 :(得分:1)
如您所知,您必须加倍双引号。所以,如果你想:
=COUNTIF(A1:A10,"apples")
你必须使用:
Sub demo()
Range("B9").Formula = "=COUNTIF(A1:A10,""apples"")"
End Sub
我个人对此有很多麻烦。我要调试的是在公式的开头放置一个撇号:
Sub demo()
Range("B9").Formula = "'=COUNTIF(A1:A10,""apples"")"
End Sub
这让我能够看到"公式的文本并解决问题。
<强> B.T.W 强>
要创建代码块,请点亮代码并使用配对的大括号{}