Excel for循环语句的问题几乎冻结了我的程序
我有两个for语句可以工作,但是,它们通过了太多的单元格,最终将我的excel冻结了大约15秒到半分钟。我需要帮助找到一种在不冻结excel的情况下运行代码的方法 如果单元格中的语句正确(从第5行向下运行未声明的行数),我的一个代码将执行一个公式。我的第二个代码将Excel for循环语句的问题几乎冻结了我的程序,excel,vba,for-loop,freeze,Excel,Vba,For Loop,Freeze,我有两个for语句可以工作,但是,它们通过了太多的单元格,最终将我的excel冻结了大约15秒到半分钟。我需要帮助找到一种在不冻结excel的情况下运行代码的方法 如果单元格中的语句正确(从第5行向下运行未声明的行数),我的一个代码将执行一个公式。我的第二个代码将NumberFormat更改为“0,00”,这可能是因为我有很多单元格中都有数字。最好用不同于For-If语句的方式编写代码,但除了下面介绍的方式之外,我无法让它工作。我尝试添加了一个DoEvents,它不会改变任何东西,程序仍然冻结
NumberFormat
更改为“0,00”
,这可能是因为我有很多单元格中都有数字。最好用不同于For-If语句的方式编写代码,但除了下面介绍的方式之外,我无法让它工作。我尝试添加了一个DoEvents
,它不会改变任何东西,程序仍然冻结
导致我的程序运行缓慢的第一个代码:
For x = 5 To Rows.Count
If Application.WorksheetFunction.IsNumber(ws.Cells(x, 2)) = True Then
ws.Cells(x, 14).FormulaR1C1 = "Formula 1"
ws.Cells(x, 15).FormulaR1C1 = "Formula 2"
ws.Cells(x, 16).FormulaR1C1 = "Formula 3"
ws.Cells(x, 17).FormulaR1C1 = "Formula 4"
ws.Cells(x, 18).FormulaR1C1 = "Formula 5"
ElseIf Application.WorksheetFunction.IsNumber(ws.Cells(x, 2)) = False Then
Exit For
End If
Next x
导致我的程序在完成前冻结几秒钟的第二个代码:
For y = 2 To Columns.Count
For x = 5 To Rows.Count
If Application.WorksheetFunction.IsNumber(ws.Cells(x, y)) = True Then
ws.Cells(x, y).NumberFormat = "0.00"
ws.Cells(x, y).HorizontalAlignment = xlCenter
ws.Cells(x, y).VerticalAlignment = xlCenter
Else
Exit For
End If
Next x, y
首先,“冻结”在大多数情况下意味着您的代码仍在运行(这可能比您预期的时间更长)
代码中的问题是,即使未使用行/列(空),也要在所有行/列中运行循环
将循环限制为实际数据量。查找最后使用的行,如
Dim LastRow As Long
LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row 'last used row in column A
在你的循环中使用这个
Dim iRow As Long
For iRow = 5 To LastRow
您还可以使用相同的方法查找最后使用的列
Dim LastCol As Long
LastCol = ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column 'last used column in row 1
在你的另一个循环中使用这个
Dim iCol As Long
For iCol = 2 To LastCol
所以你最终会得到这样的结果
Dim LastRow As Long
LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row 'last used row in column A
Dim LastCol As Long
LastCol = ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column 'last used column in row 1
Dim iRow As Long, iCol As Long
For iCol = 2 To LastCol
For iRow = 5 To LastRow
If Application.WorksheetFunction.IsNumber(ws.Cells(iRow, iCol)) = True Then
ws.Cells(iRow, iCol).NumberFormat = "0.00"
ws.Cells(iRow, iCol).HorizontalAlignment = xlCenter
ws.Cells(iRow, iCol).VerticalAlignment = xlCenter
Else
Exit For
End If
Next iRow
Next iCol
请注意,我给了您的计数器变量x
和y
更有意义的名称,以便您始终知道哪个是行计数器,哪个是列计数器
循环的替代方案 使用查找所有带有数字的单元格,并立即将其格式化,速度应快得惊人
Dim CellsWithNumbers As Range
Set CellsWithNumbers = ws.Cells.SpecialCells(xlCellTypeConstants, xlNumbers)
If Not CellsWithNumbers Is Nothing Then
With CellsWithNumbers
.NumberFormat = "0.00"
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
End With
End With
如果要查找具有数值结果的所有公式,请改用xlcelltypeformals
Dim CellsWithNumbers As Range
Set CellsWithNumbers = ws.Cells.SpecialCells(xlCellTypeFormulas, xlNumbers)
If Not CellsWithNumbers Is Nothing Then
With CellsWithNumbers
.NumberFormat = "0.00"
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
End With
End With
如果您想要常数和公式数,请依次使用。谢谢您的帮助,非常感谢。数字格式的替代循环非常有用,我还利用您的建议设法使代码的第一部分(包括公式)加速。
Dim CellsWithNumbers As Range
Set CellsWithNumbers = ws.Cells.SpecialCells(xlCellTypeFormulas, xlNumbers)
If Not CellsWithNumbers Is Nothing Then
With CellsWithNumbers
.NumberFormat = "0.00"
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
End With
End With