你如何得到一个定义的范围地址作为公式中的相对参考?

有人知道我怎样才能得到rng3的范围地址作为相对参考(例如B2:B4)而不是作为范围名称出现在我的总和公式中?

我试图使用它的总和公式给出的值为0。

我正在试图将该公式复制到D列的其余部分。

基于电子表格中最后一列的范围每周都在变化。

请忽略我的代码中的任何注释。 感谢您的阅读

Sub needhelp () Dim rng1 As Range Dim rng2 As Range Dim rng3 As Range Dim rng4 As Range Dim fillrange As Range Dim cell1 As Range Dim cell2 As Range Dim cellAddress As Range Range("G1").Select Range("G1").End(xlToRight).Select ActiveCell.Offset(1, 0).Select ActiveCell.Offset(0, -2).Select Set cell1 = ActiveCell Set rng1 = Cells.Find("*", [cell1], , , xlByColumns, xlNext) ActiveCell.Offset(0, 2).Select Set cell2 = ActiveCell Set rng2 = Cells.Find("*", [cell2], , , xlByColumns, xlPrevious) If Not rng2 Is Nothing Then Set rng3 = Range([cell1], [cell2]) MsgBox "Range is " & rng3.Address(0, 0) Application.Goto rng3 MsgBox "Range is " & rng3.Address(0, 0) Application.Goto rng3 Range("D2").Select ' *** Here is when I need help. Worksheets("Confidential").Range("D2").Formula = "=SUM(rng3) / 3" ActiveCell.Offset(0, -1).Select Selection.End(xlDown).Select ActiveCell.Offset(0, 1).Select Range(Selection, Selection.End(xlUp)).Select Cells.Select Cells.EntireColumn.AutoFit End If End Sub 

我也试过以下(没有工作):

 ' Debug.Print Range("rng4").Address(External:=True) ' Try to use this to fill average range with formula using loop (some other time): ' ActiveWorkbook.Names.Add Name:="fillrange", RefersTo:=Selection ' Range("D2").Select ' ActiveWorkbook.Names.Add Name:="", RefersTo:=Selection 

再次感谢您的阅读。

你的公式应该是:

 "=SUM(" & rng3.Address(0,0) & ") / 3" 

你需要引用vba。

另外,所有的。select减慢代码。 请参阅这篇文章 ,了解避免使用它的方法。 它会加快代码。