VBA传递变量以引用命名范围 - 对象定义的错误

时间:2015-03-31 20:44:48

标签: excel vba

我正在尝试定义名称范围,然后在公式中引用它们

工作表公式是这样的  =IFERROR(AVERAGEIFS('CAR Dashboard.xlsm'!Q2_15_Video_Penetration,'CAR Dashboard.xlsm'!Q2_15_Module,B5),"")

我在公式中传递变量并返回对象定义的错误

Sheets("QoQ Summary").Cells(5, 11).Formula = "=IFERROR(AVERAGEIFS('CAR Dashboard.xlsm'!" & VPenName & ",'CAR Dashboard.xlsm'!" & ModuleName & ",B5,"")"""

我不确定它有什么问题。有人可以帮忙吗?谢谢!

以下是测试代码。请忽略未定义的变量。

Sub test()
Dim ModuleName, VPenName, HPenName, CPenName, XPenName As String
   'Define  
   ModuleName = Sheets("Settings and Instruction").range("G1").Value
   VPenName = Sheets("Settings and Instruction").range("G2").Value

   ActiveWorkbook.Names.Add Name:=ModuleName, RefersToR1C1:= _
                "=OFFSET('" & rp & "'!R5C" & Module.Column & ",,,COUNTA('" & rp & "'!C" & Module.Column & "),)"

   ActiveWorkbook.Names.Add Name:=VPenName, RefersToR1C1:= _
                "=OFFSET('" & rp & "'!R5C" & VideoPen.Column & ",,,COUNTA('" & rp & "'!C" & VideoPen.Column & "),)"

Dim rng As range
'Set rng = range("'CAR Dashboard.xlsm'!" & VPenName)
'Application.Goto rng

 Sheets("QoQ Summary").Cells(5, 11).Formula = _
                 "=IFERROR(AVERAGEIFS('CAR Dashboard.xlsm'!" & VPenName & ",'CAR Dashboard.xlsm'!" & ModuleName & ",B5,"")"""

End Sub

1 个答案:

答案 0 :(得分:0)

您的右括号位于错误的位置,请尝试此操作:

"=IFERROR(AVERAGEIFS('CAR Dashboard.xlsm'!" & VPenName & ",'CAR Dashboard.xlsm'!" & ModuleName & ",B5,"""")"