Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.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/3/sql-server-2005/2.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 FormulaR1C1语法_Vba_Variables_Syntax - Fatal编程技术网

带变量的VBA FormulaR1C1语法

带变量的VBA FormulaR1C1语法,vba,variables,syntax,Vba,Variables,Syntax,我正试着把两个变量的公式写到单元格里。 单元格中的公式应为: =(SUM('C:\Users\[Excel.xlsm]Sheet1'!H:H)-SUM('C:\Users\[Sheet1.xlsm]Sheet1'!I:I)) 我想使用路径文件作为变量,以及工作表名称 path = C:\Users\Excel.xlsm 'from msofiledialog sheetname = Sheet1 我错过了什么 Cells(1, 1).FormulaR1C1 = "=(SUM('[" &

我正试着把两个变量的公式写到单元格里。 单元格中的公式应为:

=(SUM('C:\Users\[Excel.xlsm]Sheet1'!H:H)-SUM('C:\Users\[Sheet1.xlsm]Sheet1'!I:I))
我想使用路径文件作为变量,以及工作表名称

path = C:\Users\Excel.xlsm 'from msofiledialog
sheetname = Sheet1
我错过了什么

Cells(1, 1).FormulaR1C1 = "=(SUM('[" & Path & "] " & sheetname & " '!C8) _
                        -SUM('[" & Path & "] " & sheetname & " '!C9))

谢谢,这对我很有用:

Sub main
Dim LastRow as String
LastRow = Cells(Rows.Count, 1).End(xlUp).Row
Dim path as String
Path = "C:\users\username\Desktop\"
Dim filename as String
Filename = "Excel.xlsm"
Dim sheetname as String
sheetnameCR = "CR_" & supname
Dim myrangeH as String
Dim myrangeI as String
myrangeH = ("H5:H" & LastRow)
myrangeI = ("I5:I" & LastRow)

Cells(1, 1).Formula = "=SUM('" & Path & "[" & Filename & "]" & sheetnameCR & "'!" & myrangeH & ")" & "-SUM('" & Path & "[" & Filename & "]" & sheetnameCR & "'!" & myrangeI & ")"
End Sub
我必须添加“RangeH”变量,因为否则excel将使用单元格C8,而不是我想要的H列。
这很好,但即使我给了他想要的单元格的完整路径,excel仍然要求我在FileDialogOpen的情况下提供excel的路径。你知道为什么吗?

当我这样键入时:单元格(1,1)。公式1c1=“=(SUM('C:/Users/“&Environ$('Username”)&”/Desktop/[Excel].xlsm]“&sheetnameCR&'!C8)-SUM('C:/Users/“&Environ$('Username”)&”/Desktop/[Excel].xlsm]“&sheetnameCR&'!C9”)“它不是问我路径或任何问题,我只是想让它短一点。