[問題] Excel sumifs多條件 VBA陣列
軟體:excel
版本:2010
爬了一下文,發現之前so大的資料已經不在dropbox了QQ
因為用了函數發現嚴重影響計算效率
我原始資料(sheet1)只要一更新,其他工作頁上的函數就會重新計算
導致我原始資料每輸入一筆資料就耗費快一分鐘在計算函數上,函數如下
=IF(SUMIFS(sheet1!J:J,sheet1!B:B,A3,sheet1!C:C,B3)<H3,"未完成","完成")
因此想到用VBA設置按鈕讓需要計算的時候按下按鈕即可,程式碼如下
Set rngpo = Sheets(1).Range("b1:b" & lstrow)
Set rngno = Sheets(1).Range("c1:c" & lstrow)
Set rngout = Sheets(1).Range("j1:j" & lstrow)
With ActiveSheet
myrow = .Range("b3").End(xlDown).Row
For i = 3 To myrow
If Application.SumIfs(rngout, rngpo, Cells(i, 1).Value, rngno, Cells(i,
2).Value) < Cells(i, 8).Value Then
Cells(i, 13).Value = "未完成"
Else: Cells(i, 13).Value = "完成"
End If
Next i
後來發現按下按鈕後還是非常沒有效率,平均100rows的資料要25秒
自己在網上搜尋後,發現使用陣列會加速很多
但對VBA完全新手的我 array 的使用方式研究好久還是不太清楚
找到使用陣列的優化程式碼如下
Sub sumif()
Const n& = 50000
Dim d As Object, a, u&(), i As Long
Set d = CreateObject("scripting.dictionary")
a = Range("A1:B" & n)
ReDim u(1 To n, 1 To 1)
For i = 1 To n
d(a(i, 1)) = d(a(i, 1)) + a(i, 2)
Next i
For i = 1 To n
u(i, 1) = d(a(i, 1))
Next i
Range("E1:E" & n) = u
End Sub
原本函數的sample如下
=RANDBETWEEN(10,99) in A1:A50000
and
=RANDBETWEEN(50,500000) in B1:B50000
Then in C1
=SUMIF(A:A,A1,B:B)
我是完全不懂他在哪個地方有做加總的動作
不知道哪位大大可以看出這個外國人的邏輯
最後同場加映似乎更快的方法,這個我比較看得懂(因為沒有陣列)
但我找不到他的criteria他只合併了criteria range 成為另外一個range
但是他的criteria在哪?
還有他用排序的方式去加總,不是應該要在合併完AB欄位後就要先排序一次嗎?
太多疑問不知道有沒有大神可以教學陣列的邏輯(願意付學費)
Sub FasterThanSumifs()
'FasterThanSumifs Concatenates the criteria values from columns A and B -
'then uses simple IF formulas (plus 1 sort) to get the same result as a
sumifs formula
'Columns A & B contain the criteria ranges, column C is the range to sum
'NOTE: The data is already sorted on columns A AND B
'Concatenate the 2 values as 1 - can be used to concatenate any number of
values
With Range("D2:D25001")
.FormulaR1C1 = "=RC[-3]&RC[-2]"
.Value = .Value
End With
'If formula sums the range-to-sum where the values are the same
With Range("E2:E25001")
.FormulaR1C1 = "=IF(RC[-1]=R[-1]C[-1],RC[-2]+R[-1]C,RC[-2])"
.Value = .Value
End With
'Sort the range of returned values to place the largest values above the
lower ones
Range("A1:E25001").Sort Key1:=Range("D1"), Order1:=xlAscending, _
Key2:=Range("E1"), Order2:=xlDescending, Header:=xlYes
Sheet1.Sort.SortFields.Clear
'If formula returns the maximum value for each concatenated value match &
'is therefore the equivalent of using a Sumifs formula
With Range("F2:F25001")
.FormulaR1C1 = "=IF(RC[-2]=R[-1]C[-2],R[-1]C,RC[-1])"
.Value = .Value
End With
End Sub
第一次發文 如果排版有問題請告知
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 47.89.55.16
※ 文章網址: https://www.ptt.cc/bbs/Office/M.1490710537.A.C9F.html
※ 編輯: heavendemon (47.89.55.16), 03/28/2017 22:18:31
※ 編輯: heavendemon (47.89.55.16), 03/28/2017 22:20:37
→
03/29 00:02, , 1F
03/29 00:02, 1F
→
03/29 00:03, , 2F
03/29 00:03, 2F
→
03/29 00:23, , 3F
03/29 00:23, 3F
→
03/29 00:25, , 4F
03/29 00:25, 4F
→
03/29 00:29, , 5F
03/29 00:29, 5F
→
03/29 00:29, , 6F
03/29 00:29, 6F
→
03/29 00:32, , 7F
03/29 00:32, 7F
→
03/29 00:33, , 8F
03/29 00:33, 8F
→
03/29 00:34, , 9F
03/29 00:34, 9F
→
03/29 00:37, , 10F
03/29 00:37, 10F
→
03/29 00:38, , 11F
03/29 00:38, 11F
→
03/29 00:39, , 12F
03/29 00:39, 12F
→
03/29 00:39, , 13F
03/29 00:39, 13F
→
03/29 00:44, , 14F
03/29 00:44, 14F
→
03/29 00:44, , 15F
03/29 00:44, 15F
→
03/29 00:46, , 16F
03/29 00:46, 16F
→
03/29 00:46, , 17F
03/29 00:46, 17F
→
03/29 00:47, , 18F
03/29 00:47, 18F
→
03/29 00:49, , 19F
03/29 00:49, 19F
→
03/29 00:49, , 20F
03/29 00:49, 20F
→
03/29 00:58, , 21F
03/29 00:58, 21F
→
03/29 01:14, , 22F
03/29 01:14, 22F
→
03/29 01:15, , 23F
03/29 01:15, 23F
→
03/29 01:15, , 24F
03/29 01:15, 24F
→
03/29 01:16, , 25F
03/29 01:16, 25F
→
03/29 01:16, , 26F
03/29 01:16, 26F
→
03/29 01:17, , 27F
03/29 01:17, 27F
→
03/29 01:24, , 28F
03/29 01:24, 28F
→
03/29 01:28, , 29F
03/29 01:28, 29F
→
03/29 01:29, , 30F
03/29 01:29, 30F
→
03/29 01:37, , 31F
03/29 01:37, 31F
→
03/29 01:38, , 32F
03/29 01:38, 32F
→
03/29 01:38, , 33F
03/29 01:38, 33F
→
03/29 10:34, , 34F
03/29 10:34, 34F
→
03/29 10:37, , 35F
03/29 10:37, 35F
→
03/29 10:38, , 36F
03/29 10:38, 36F
→
03/29 10:51, , 37F
03/29 10:51, 37F
→
03/29 10:52, , 38F
03/29 10:52, 38F
→
03/29 11:38, , 39F
03/29 11:38, 39F
→
03/29 11:39, , 40F
03/29 11:39, 40F
→
03/29 11:40, , 41F
03/29 11:40, 41F
→
03/29 11:41, , 42F
03/29 11:41, 42F
→
03/29 12:03, , 43F
03/29 12:03, 43F
※ 編輯: heavendemon (47.89.55.16), 03/29/2017 15:14:04
→
03/29 16:01, , 44F
03/29 16:01, 44F
→
03/29 16:03, , 45F
03/29 16:03, 45F
→
03/29 16:03, , 46F
03/29 16:03, 46F
→
03/29 16:04, , 47F
03/29 16:04, 47F
→
03/29 16:05, , 48F
03/29 16:05, 48F
→
03/29 16:06, , 49F
03/29 16:06, 49F
→
03/29 16:16, , 50F
03/29 16:16, 50F
→
03/29 16:18, , 51F
03/29 16:18, 51F
→
03/29 16:19, , 52F
03/29 16:19, 52F
→
03/29 18:54, , 53F
03/29 18:54, 53F
→
03/29 18:55, , 54F
03/29 18:55, 54F
→
03/29 18:56, , 55F
03/29 18:56, 55F
→
03/29 19:24, , 56F
03/29 19:24, 56F
→
03/29 19:25, , 57F
03/29 19:25, 57F
→
03/29 19:28, , 58F
03/29 19:28, 58F
→
03/29 20:10, , 59F
03/29 20:10, 59F
→
03/29 20:51, , 60F
03/29 20:51, 60F
→
03/29 20:51, , 61F
03/29 20:51, 61F
非常感謝so 大不厭其煩解答
我最後用了錄製巨集的方式取得原本sumifs函數的formulaR1C1格式
直接將R1C1的函數丟到指定的range範圍
最後把函數取代成值
達到每100rows低於一秒的效率
花了很久的時間 才回頭發現最簡單的方法
希望能給有遇到函數公式太多導致原始資料更新耗時的朋友
一些參考和幫助
※ 編輯: heavendemon (47.89.55.16), 03/30/2017 18:10:19
Office 近期熱門文章
PTT數位生活區 即時熱門文章