VBA替代Application.Quotient包含小数

时间:2018-01-03 16:47:22

标签: excel vba excel-vba

我正在尝试使用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

1 个答案:

答案 0 :(得分:1)

import numpy as np import matplotlib.pyplot as plt from matplotlib import dates, ticker import matplotlib as mpl from mpl_finance import candlestick_ohlc mpl.style.use('default') data = [('2017-01-02 02:00:00', '1.05155', '1.05197', '1.05155', '1.0519'), ('2017-01-02 02:01:00', '1.05209', '1.05209', '1.05177', '1.05179'), ('2017-01-02 02:02:00', '1.05177', '1.05198', '1.05177', '1.05178'), ('2017-01-02 02:03:00', '1.05188', '1.052', '1.05188', '1.052'), ('2017-01-02 02:04:00', '1.05196', '1.05204', '1.05196', '1.05203'), ('2017-01-02 02:06:00', '1.05196', '1.05204', '1.05196', '1.05204'), ('2017-01-02 02:07:00', '1.05205', '1.0521', '1.05205', '1.05209'), ('2017-01-02 02:08:00', '1.0521', '1.0521', '1.05209', '1.05209'), ('2017-01-02 02:09:00', '1.05208', '1.05209', '1.05208', '1.05209'), ('2017-01-02 02:10:00', '1.05208', '1.05211', '1.05207', '1.05209')] ohlc_data = [] for line in data: ohlc_data.append((dates.datestr2num(line[0]), np.float64(line[1]), np.float64(line[2]), np.float64(line[3]), np.float64(line[4]))) fig, ax1 = plt.subplots() candlestick_ohlc(ax1, ohlc_data, width = 0.5, colorup = 'g', colordown = 'r', alpha = 0.8) ax1.xaxis.set_major_formatter(dates.DateFormatter('%d/%m/%Y %H:%M')) ax1.xaxis.set_major_locator(ticker.MaxNLocator(10)) plt.xticks(rotation = 30) plt.grid() plt.xlabel('Date') plt.ylabel('Price') plt.title('Historical Data EURUSD') plt.tight_layout() plt.show() 关闭"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 函数传递参数的顺序的问题。

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

Ifs