我有一个子控件,它获取2个日期的用户输入,它们充当定义为DateRange
的范围的上下限。我想找到2个日期的单元格地址,将单元格引用增加1行,并用它来计算落入2个日期之下的所有相应值的总和。
我对对象变量操作没有太多经验。这是我遇到的问题:
L_Range
和R_Range
增加1行L_Found
和R_Found
Sub CustomIntervals()
'User Input
Dim L_Bound As Date
Dim R_Bound As Date
'Contains Found Cell Reference for L_Bound and R_Bound
Dim L_Found As Range
Dim R_Found As Range
'Defining Range that will be Searched
Dim DateRange As Range
Set DateRange = Range("H8", Range("H8").End(xlToRight))
L_Bound = InputBox("Enter Lower Bound Date (mm/dd/yyyy)", "Custom Date")
R_Bound = InputBox("Enter Upper Bound Date (mm/dd/yyyy)", "Custom Date")
Range("H8", Range("H8").End(xlToRight)).NumberFormat = "d-mmm"
Set L_Found = DateRange.Find(L_Bound)
Set R_Found = DateRange.Find(R_Bound)
Set L_Found = L_Found.Offset(1, 0)
Set R_Found = R_Found.Offset(1, 0)
Range("E9").Formula = "=sum(" & L_Found.Address & ":" & R_Found.Address & ")"
Range("E9").Select
ActiveCell.AutoFill Destination:=Range("E9:E15")
End Sub
答案 0 :(得分:0)
您需要一个看起来像单元格范围引用的字符串。这可以通过Address属性生成。
您还需要偏移生成该地址的范围。在获取地址之前,请使用偏移功能。
Range("E9").forumla = _
"=sum(" & Range(L_Found, R_Found).Offset(1, 0).Address(0, 0) & ")"