gpt4 book ai didi

excel - Countif on sheet with 700k rows freezes program

转载 作者:行者123 更新时间:2023-12-04 22:30:14 24 4
gpt4 key购买 nike

我目前有两个列表。 A 列中的“授予者”列表和 B 列中删除重复项的相同列表。我试图使用 countif 计算给定授予者在 A 列中的次数,但是我在 A 列中的列表超过 700k 行.我使用的是 64 位 excel,但每次我运行代码来执行此操作时,excel 都会卡住和崩溃。

有没有办法在 excel 中做到这一点,还是我需要采取另一种方法,比如使用数据透视表或在 Access 中创建表?

我已经写了一些子程序,但这是最新的,来自这个论坛的另一个帖子。

Sub Countif()

Dim lastrow As Long
Dim rRange As Range
Dim B As Long '< dummy variable to represent column B

B = 2

With Application
.ScreenUpdating = False 'speed up processing by turning off screen updating
.DisplayAlerts = False
End With

'set up a range to have formulas applied
With Sheets(2)
lastrow = Cells(Rows.Count, "A").End(xlUp).Row
Set rRange = .Range(.Cells(2, B), .Cells(lastrow, B))
End With

'apply the formula to the range
rRange.Formula = "=COUNTIF($A$2:$A$777363,C2)"
'write back just the value to the range
rRange.Value = rRange.Value

With Application
.ScreenUpdating = True
.DisplayAlerts = True
End With

End Sub

最佳答案

像这样的东西:

Sub Countif()

Dim allVals, uniqueVals, i As Long, dict, v, dOut(), r As Long

''creating dummy data
' With Sheet2.Range("A2:A700000")
' .Formula = "=""VAL_"" & round(RAND()*340000,0)"
' .Value = .Value
' End With
'

'get the raw data and unique values
With Sheet2
allVals = .Range("A2:A" & .Cells(.Rows.Count, "A").End(xlUp).Row).Value
uniqueVals = .Range("B2:B" & .Cells(.Rows.Count, "B").End(xlUp).Row).Value
End With
ReDim dOut(1 To UBound(uniqueVals, 1), 1 To 1) 'for counts...

Set dict = CreateObject("scripting.dictionary")
'map unique value to index
For i = 1 To UBound(uniqueVals, 1)
v = uniqueVals(i, 1)
If Len(v) > 0 Then dict(v) = i
Next i


'loop over the main list and count each unique value in colB
For i = 1 To UBound(allVals, 1)
v = allVals(i, 1)
If Len(v) > 0 Then
If dict.exists(v) Then
r = dict(v)
dOut(r, 1) = dOut(r, 1) + 1
End If
End If
Next i

'output the counts
Sheet2.Range("C2").Resize(UBound(dOut, 1), 1).Value = dOut

End Sub

在约 30 秒内运行,A 中有 700k 值,B 中有 300k 唯一值

关于excel - Countif on sheet with 700k rows freezes program,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53622847/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com