vba - 循环中的 AVERAGEIFS
问题描述
我是 vba 的新手,我制作了一个宏来使用我的日常生活,我需要 AVERAGEIFS 的帮助。
我怎样才能给averageifs公式的变量,
我想要什么,在我的循环中,我想根据 excel 更改“All!I:I”部分。
它将变为 G:G 然后 M:M 然后 Q:Q (每次相同数量(例如:4)的 colown 将跳过...)
有任何想法吗?
太感谢了。
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
解决方案
我通过索引公式解决了它,我的代码变成了;
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