gpt4 book ai didi

VBA 嵌套 For 循环导致 Excel 崩溃

转载 作者:行者123 更新时间:2023-12-03 02:02:54 25 4
gpt4 key购买 nike

我目前正在尝试从两个单独的工作表创建所有可能的条目组合的列表,但每当我尝试运行它时,Excel 都会在大约 20 秒后崩溃。有没有人有任何关于如何使其更有效的提示,或使其工作的方法?谢谢!

Sub Create()
Dim dates, groups, current As Integer
Dim dateValue As Date
Dim groupValue As String
Dim cell As Long

Application.ScreenUpdating = False
Sheets(3).Cells.Clear
cell = 1

For dates = 1 To 730

Sheets(1).Select
dateValue = Cells(dates, 1).Value

For groups = 1 To 155

Application.StatusBar = dateValue & " " & groupValue

Sheets(2).Select
groupValue = Cells(groups, 1).Value

Sheets(3).Select

Cells(cell, 1) = dateValue
Cells(cell, 2) = groupValue

cell = cell + 1

Next groups

Next dates

Application.StatusBar = False
Application.ScreenUpdating = True

End Sub

最佳答案

删除 .Select 调用。

groupValue = Sheets(2).Cells(groups, 1).Value

优于

Sheets(2).Select
groupValue = Cells(groups, 1).Value

.Select 速度慢、成本高且不必要。

状态栏真的更新了吗?这样做10万次同样是一个瓶颈;使用 mod 计数器更新每 n 次迭代。

关于VBA 嵌套 For 循环导致 Excel 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29800363/

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