【问题标题】:Using VBA to assign a criterion for average.ifs()使用 VBA 为 average.ifs() 分配标准
【发布时间】: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


【解决方案1】:

除非您正在设置像Range.Find method 返回的单元格这样的对象,否则您不要Set 变量。将 double 分配给 var 应该简单地相等(例如 =)。

Sub Make_AVERAGEIFS()

    Dim FindString As Date
    Dim Sumact As Range
    Dim Analyst As Double
    Dim shname As String
    Dim d As Integer, m As Integer, y As Integer

    Do Until WorksheetExists(shname)
                  shname = InputBox("Enter sheet name")
                  If Not WorksheetExists(shname) Then MsgBox shname & " doesn't exist!", vbExclamation
    Loop

    With worksSheets(shname)
        .Activate
        FindString = InputBox("Enter a Search value")
        If Trim(FindString) <> "" Then
            With .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

        If IsDate(Sumact) Then
            d = Day(Sumact): m = Month(Sumact): y = Year(Sumact)
            Analyst = Application.AverageIfs(.Columns(2), _
                                             .Columns(1), "<" & DateSerial(y, m, d), _
                                             .Columns(1), ">" & DateSerial(y, m - 6, d))
        End If
    End With


    Selection.Copy
End Sub

我认为在 A 列中搜索日期是检查用户是否输入了有效日期的一种方法,但必须有其他不太复杂的方法。我上面用的IsDate Function就是一个。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-05
    • 1970-01-01
    • 1970-01-01
    • 2015-01-03
    • 2019-01-02
    • 2011-05-11
    相关资源
    最近更新 更多