AVERAGEIFS循环

时间:2018-07-30 09:04:04

标签: excel vba excel-vba

我是vba的新手,我为使用日常工作制作了一个宏,我需要AVERAGEIFS的帮助。

如何给averageifs公式提供变量,

我想要的是,在循环中,我想根据excel更改“ All!I:I”部分。

它将变成G:G然后M:M然后Q:Q(每次相同数量的小丑(例如ex:4)都会跳过...)

有什么想法吗?

非常感谢您。

Dim yilne As Long

yilne = MsgBox(ActiveSheet.Name & " Analiz Yili 2018 mi?", vbYesNo)

If yilne = vbYes Then
    yilne = 2018
Else
    yilne = Application.InputBox(prompt:=ActiveSheet.Name & " Analiz Yili Nedir?", Type:=1)
End If

rt = 3
hc = 2
sonay = 7
kacay = cnt
'kacay = Application.InputBox(prompt:=ActiveSheet.Name & " KaçAy var?", Type:=1)

For ilk = 1 To kacay

    Dim lay As Long
    Dim ilktarih As Date
    Dim sontarih As Date

    lay = Cells(rt - 1, hc)
    'lay = Application.InputBox("Enter Month Number 1 to 12")

    ilktarih = DateSerial(yilne, lay, 1)
    sontarih = DateSerial(yilne, lay + 1, 0)

    Cells(rt, hc).Select
    Cells(rt, hc).Formula = "=IFERROR(AVERAGEIFS(All!g:g,All!$B:$B,"">=" & ilktarih & """,All!$B:$B, ""<=" & sontarih & """,All!$A:$A,$A3),"""")"
    hc = hc + 1
Next ilk

1 个答案:

答案 0 :(得分:1)

我通过索引公式解决了它,我的代码变成了

op = 1
basla = 2

Do Until Cells(1, basla) = ""

  For ilk = 1 To kacay

   Dim lay As Long
   Dim ilktarih As Date
   Dim sontarih As Date

lay = Cells(rt - 1, hc)

ilktarih = DateSerial(yilne, lay, 1)
sontarih = DateSerial(yilne, lay + 1, 0)

Cells(rt, hc).Select


Cells(rt, hc).Formula = "=IFERROR(AVERAGEIFS(INDEX(All!G:AA,0," & op & "),All!$B:$B,"">=" & ilktarih & """,All!$B:$B, ""<=" & sontarih & """,All!$A:$A,$A3),"""")"




hc = hc + 1

Next ilk
basla = basla + kacay
op = op + 4
Loop