Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 尝试将单元格值设置为字符串值时出现错误1004_Vba_Excel - Fatal编程技术网

Vba 尝试将单元格值设置为字符串值时出现错误1004

Vba 尝试将单元格值设置为字符串值时出现错误1004,vba,excel,Vba,Excel,我已经见过这个问题好几次了,但还没有找到适合我的情况的解决方案,所以这里是: 我有一个相当复杂的公式,我想把它插入一个细胞中(复杂得像屁股上的痛一样)。我在变量中设置了组件,并在“局部变量”窗口中运行sub给我正确的最终变量名,但是当我尝试将单元格设置为公式时,我会得到一个“1004:应用程序定义或对象定义错误”。输出应如下所示: 单元格A1:=BDS(“0”,“pg\U段”,“dir=h”,“周期数=-3”) 然而,它什么也不返回 我尝试了以下方法:将最后一个变量(cmdstr0)设置为整数—

我已经见过这个问题好几次了,但还没有找到适合我的情况的解决方案,所以这里是:

我有一个相当复杂的公式,我想把它插入一个细胞中(复杂得像屁股上的痛一样)。我在变量中设置了组件,并在“局部变量”窗口中运行sub给我正确的最终变量名,但是当我尝试将单元格设置为公式时,我会得到一个“1004:应用程序定义或对象定义错误”。输出应如下所示:

单元格A1:=BDS(“0”,“pg\U段”,“dir=h”,“周期数=-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

另外,有谁能告诉我,是否有比每行按空格键四次更快的方式格式化为代码?

正如您所知,您必须在双引号上加上两倍。因此,如果你想:

=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


要创建代码块,请亮起代码并使用成对的大括号{}

谢谢!我完全忘记了双引号的事。还有一个问题:为什么Chr(34)(“”)不起作用?我希望我能给你一个好的答案。我发现嵌入双引号的整个过程非常令人沮丧。@smpat04可能是你这边的语法错误。我还没有测试过你的代码,但我注意到类似这样的简单操作可以使用Chr(34):
ActiveCell.Value“=”&Chr(34)和“hello”“&Chr(34)