VBA替代Application.Quotient包含小数

问题描述 投票:1回答:1

我正在尝试使用Application.AverageIfs,我需要将答案除以3.我试过这样:

Range("C1:C676") = (Application.IfError(Application.AverageIfs(Sheets(modelName).Range("R:R"....
                    Sheets(modelName).Range("U:U"), "OGV"), "0") / 3)

并且没有括号围绕第一个application和最终的3但是这给出了类型不匹配错误。

将它嵌入Application.Quotient工作,但它只给出答案的整数部分,我也需要小数。有十进制友好的替代方案吗?我宁愿继续使用application语法,而不是如果可能的话将range().formula = "=averageifs(放在一边。

编辑:在J.Fox的建议之后,我将公式部分分解为变量。问题似乎是criZFromcriZTo变量,它们使用单​​独工作表中的范围作为标准。如果我分别用"1""2"替换这些变量,该公式可以正常工作。代码现在是:

Set rng = Sheets(wsName).Range("C1:C676")
    Set avgCol = Sheets(modelName).Range("M:M")
    Set colZFrom = Sheets(modelName).Range("G:G")
    Set criZFrom = Sheets(wsName).Range("A1:A676")
    Set colZTo = Sheets(modelName).Range("H:H")
    Set criZTo = Sheets(wsName).Range("B1:B676")
    Set colTime = Sheets(modelName).Range("V:V")
    Set colVType = Sheets(modelName).Range("U:U")

    criVType = "OGV"
    criAM = "AM"

        Range("A1:A676").Formula = "=roundup(row()/26,0)"

        Range("B1:B676").Formula = "=if(mod(row(),26)=0,26,mod(row(),26))"

            rng = Application.AverageIfs(avgCol, colZFrom, criZFrom, colZTo, criZTo, colTime, criAM, colVType, criVType) / 3

以下是一些示例数据:

来自sheets(modelName),这有我想要平均的数据和大多数标准范围:enter image description here

sheets(wsName),这有问题变量的标准,是我希望结果出现的地方(在C列中):enter image description here

excel vba excel-vba
1个回答
1
投票

看起来你在"OGV")之后错过了一个右括号来关闭AverageIfs函数,即:

Range("C1:C676") = Application.IfError(Application.AverageIfs(Sheets(modelName).Range("R:R", Sheets(modelName).Range("U:U"), "OGV")), 0) / 3

此外,不确定....是仅用于此处还是代码,但您想要使用_,如:

Range("C1:C676") = _ 
Application.IfError(Application.AverageIfs(Sheets(modelName).Range("R:R", _
Sheets(modelName).Range("U:U"), "OGV")), 0) / 3

编辑:如果您仍然遇到错误,我建议将您的公式分解为组件部分,并将每个部分分配给变量,以便您可以准确地解决问题的位置,如下所示:

Sub test()
Dim rng As Range, col1 As Range, col2 As Range, str As String, modelName As String

modelName = "Sheet1"

Set rng = Range("C1:C676")
Set col1 = Sheets(modelName).Columns(18)
Set col2 = Sheets(modelName).Columns(21)
str = "OGV"

rng = Application.IfError(Application.AverageIfs(col1, col2, str), 0) / 3

End Sub

我们能看到一些样本数据吗?这可能是AverageIfs函数传递参数的顺序问题。

编辑2:我想我可能会看到问题所在。您正在使用AverageIfs函数,目的是根据每行的特定条件分别验证每一行,方法是使用Arg3和Arg5的范围而不是AverageIfs不喜欢的单个值。 Ifs函数的标准总是需要是单个值而不是一系列值。相反,我认为你需要使用循环分别迭代每一行,如下所示:

Set avgCol = Sheets(modelName).Range("M:M")
Set colZFrom = Sheets(modelName).Range("G:G")
Set colZTo = Sheets(modelName).Range("H:H")
Set colTime = Sheets(modelName).Range("V:V")
Set colVType = Sheets(modelName).Range("U:U")

criVType = "OGV"
criAM = "AM"

Range("A1:A676").Formula = "=roundup(row()/26,0)"
Range("B1:B676").Formula = "=if(mod(row(),26)=0,26,mod(row(),26))"

Dim x as Long
Dim t as Variant

For x = 1 To 676
    Set criZFrom = Sheets(wsName).Range("A" & x)
    Set criZTo = Sheets(wsName).Range("B" & x)
    Set Rng = Sheets(wsName).Range("C" & x)

    t = Application.WorksheetFunction.AverageIfs(avgCol, colZFrom, criZFrom.Value, colZTo, criZTo.Value, colTime, criAM, colVType, criVType)
    t = CDbl(t / 3)
    Rng.Value = t
Next x
© www.soinside.com 2019 - 2024. All rights reserved.