循环运行,但"not responding"几秒钟?



我正在创建年度预算。当用户点击";创建新预算;此循环用于确定该地区最近的预算年度。

循环执行但接收到一个"0";没有响应";错误几秒钟后才会结束。

For i = 2 To Rows.Count
cellYearRaw = Sheets("Budgets").Cells(i, "A").Value
cellYear = CInt(cellYearRaw)
cellAreaKey = Sheets("Budgets").Cells(i, "H").Value
If cellYear > year And cellAreaKey = areaKey Then
year = cellYear
End If
Next i

您的代码需要强制事件到excel,并告诉系统它正在响应。您需要使用DoEvents:

For i = 2 To Rows.Count
cellYearRaw = Sheets("Budgets").Cells(i, "A").Value
cellYear = CInt(cellYearRaw)
cellAreaKey = Sheets("Budgets").Cells(i, "H").Value
If cellYear > year And cellAreaKey = areaKey Then
year = cellYear
End If
DoEvents 'for each line can be slow. Use for exemple "If i Mod 10 = 0 Then: DoEvents", to activate events after 10 rows.
Next i

最新更新