Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.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

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
Excel 在VBA中将.Cells(i,j)调用为.formula_Excel_Vba - Fatal编程技术网

Excel 在VBA中将.Cells(i,j)调用为.formula

Excel 在VBA中将.Cells(i,j)调用为.formula,excel,vba,Excel,Vba,您好,我想使用.cells(I,j)调用单元格,将其转换为.formula 此时,我的宏将值/公式打印到单元格数组中。 它对值很有效,但我很难插入一个不返回错误的公式 这是相关的代码行 Sheets("Stats").Cells(j, i).Formula = SUM(Sheets("Stats").Cells(1, i),Sheets("Stats").Cells(1, i), A3) 这是一个简单的替代行,基本上我希望能够在同一个公式中使用.cells调用和字母数字调用 非常感谢在使用.F

您好,我想使用.cells(I,j)调用单元格,将其转换为.formula

此时,我的宏将值/公式打印到单元格数组中。 它对值很有效,但我很难插入一个不返回错误的公式

这是相关的代码行

Sheets("Stats").Cells(j, i).Formula = SUM(Sheets("Stats").Cells(1, i),Sheets("Stats").Cells(1, i), A3)
这是一个简单的替代行,基本上我希望能够在同一个公式中使用.cells调用和字母数字调用


非常感谢

在使用.Formula时,它需要一个字符串,并且它的格式必须与您在不使用VBA的情况下将其输入单元格本身的格式相同(包括开头的“=”)。试一试


您可以在案例中使用
With
语句,这可以简化很多:

With Sheets("Stats")    
    .Cells(j, i).Formula = "=Sum(" & .Cells(1, i).Address & "," & .Cells(1, i).Address & ", A3)"
End With
注意:每当尝试调试公式是否正确时,请使用
debug.Print

在您的情况下,如果您将
i=2
,并在该代码下面:

With Sheets("Stats")
    Debug.Print "=Sum(" & .Cells(1, i).Address & "," & .Cells(1, i).Address & ", A3)"
您将在即时窗口中使用以下公式:

=Sum($B$1,$B$1, A3)
=Sum($B$1,$B$1, A3)