gpt4 book ai didi

excel - 700K 行数据的功能非常慢

转载 作者:行者123 更新时间:2023-12-04 21:43:42 24 4
gpt4 key购买 nike

使用以下函数在短数据上表现良好,但每当我移动大型数据集时,处理时间很长,有时会卡住。
如果有另一种更快的方法来执行此操作,我将不胜感激。

Sub minustozeo()
Dim c As Range
For Each c In Selection.SpecialCells(xlCellTypeVisible)
If c.Value < 0 Then
c.Value = 0
End If
Next
End Sub

最佳答案

这是使用数组方法的更快方法:

Sub Tester()

Dim ws As Worksheet, c As Range, t, rngVis As Range, rngVals As Range
Dim arr, r As Long

Set ws = ActiveSheet
Set rngVals = ws.Range("A2:A500000") 'range to operate on

'create some dummy data with about 70% < 0
If ws.FilterMode Then ws.ShowAllData
With rngVals
.Formula = "=RAND()-0.7"
.Value = .Value
End With

rngVals.Copy rngVals.Offset(, 2) 'for verification only...

'autofilter
rngVals.AutoFilter Field:=1, Criteria1:="<0", Operator:=xlAnd

On Error Resume Next
Set rngVis = rngVals.SpecialCells(xlCellTypeVisible)
On Error GoTo 0
If rngVis Is Nothing Then Exit Sub 'no cells found

Debug.Print rngVis.Cells.Count 'approx 350k

t = Timer
arr = rngVals.Value 'read the data
For Each c In rngVis.Cells
If c.Value < 0 Then arr(c.Row - 1, 1) = 0 ' -1 because first array value is from row #2
Next c
rngVals.Value = arr 'write the altered data back
Debug.Print Timer - t ' ~1 sec

ws.ShowAllData

End Sub

关于excel - 700K 行数据的功能非常慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71026360/

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