如何获得定义的范围地址以在公式中显示为相对引用?
How do you get a defined range address to show up as a relative reference in formula?
如何让 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
都会降低代码速度。请参阅 THIS POST 了解避免使用它的方法。它将加快代码速度。
如何让 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
都会降低代码速度。请参阅 THIS POST 了解避免使用它的方法。它将加快代码速度。