【发布时间】:2015-08-29 14:53:32
【问题描述】:
我在excel中使用下面的公式
=AVERAGEIFS(B4:B440;A4:A440;"<"&A441;A4:A440;">"&EDATE(A441;-6))
根据相邻列中的值获取一系列值的平均值。但是,我需要将此公式应用于一千多个日期(A 列包含日期)。我有一个宏,它要求用户指定工作表名称和日期(使用对话框)。所以我想添加一些代码,它采用用户指定的日期并将上面公式中的单元格 A441 替换为它。然后复制平均值,以便我可以将其粘贴到需要的位置。这是我到目前为止尝试编码的内容,但没有成功:
Sub Find()
Dim FindString As Date
Dim Sumact As Range
Dim Analyst As Double
Dim shname As String
Do Until WorksheetExists(shname)
shname = InputBox("Enter sheet name")
If Not WorksheetExists(shname) Then MsgBox shname & " doesn't exist!", vbExclamation
Loop
Sheets(shname).Select
FindString = InputBox("Enter a Search value")
If Trim(FindString) <> "" Then
With Sheets(shname).Range("A:A")
Set Sumact = .Find(What:=FindString, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not Sumact Is Nothing Then
Application.Goto Sumact, True
Else
MsgBox "Nothing found"
End If
End With
End If
Set Analyst = Application.AverageIf(Range(("B:B"), ("A:A")), "<Sumact")
Selection.Copy
End Sub
【问题讨论】:
-
引用 A 列中的调用还不够吗?为什么要将日期硬编码到公式中?
标签: vba excel if-statement average