Excel VBA宏执行时间过长

Excel VBA宏执行时间过长,excel,vba,Excel,Vba,这个非常简单的宏只需运行55次迭代,耗时93秒。我也尝试了它作为下一个循环,同样的结果 Dim thedate As Date Dim current_cell As Long Dim f As Single f = Timer() current_cell = Range("e65000").End(xlUp).Row thedate = Range("e" & current_cell).Value Dim i As Integer Application.ScreenUpdat

这个非常简单的宏只需运行55次迭代,耗时93秒。我也尝试了它作为下一个循环,同样的结果

Dim thedate As Date
Dim current_cell As Long
Dim f As Single
f = Timer()

current_cell = Range("e65000").End(xlUp).Row

thedate = Range("e" & current_cell).Value
Dim i As Integer
Application.ScreenUpdating = False

Do Until Range("f" & current_cell).Value = ""
i = i + 1
If i = 900 Then
End
End If

    If Range("g" & current_cell).Value <> "x" Then
    Cells(current_cell, "e").Value = thedate
    Else
    thedate = thedate + 1
    Cells(current_cell, "e").Value = thedate
    End If
current_cell = current_cell + 1

Loop

Application.ScreenUpdating = True
MsgBox "ET: " & Format(Timer - f, "0.000") & "s"

问题解决了。我需要将计算更改为手动并禁用事件触发

Application.Calculation = xlCalculationManual
    Application.EnableEvents = False

    For iRow = 3 To Cells(Rows.Count, "G").End(xlUp).Row
        If Cells(iRow, "G") = "x" Then
            Cells(iRow, "E").Value = Cells(iRow - 1, "E").Value + 1
        Else
            Cells(iRow, "E").Value = Cells(iRow - 1, "E").Value
        End If
    Next iRow


    Application.Calculation = xlCalculationAutomatic
    Application.EnableEvents = True

这似乎在很大程度上取决于工作表数据,你不认为吗?可能的重复为什么它应该依赖于工作表数据?我所做的只是确定g列单元格中是否有x?@user147178请阅读建议的副本。这个问题的答案提供了你的答案。请参阅我对OP的编辑
Sub dates()


Dim thedate
Dim current_cell As Long
Dim f As Single
f = Timer()
Dim rng As Range, rng2 As Range


current_cell = Range("e65000").End(xlUp).Row


Dim done As Long
done = Range("f65000").End(xlUp).Row - 1


Set rng = Range("g" & current_cell, "g" & done)
Set rng2 = Range("e" & current_cell, "e" & done)


thedate = Format(thedate, Date)
thedate = rng2.Value
'thedate = rng2.Value
Dim i As Integer
i = 7
'Application.ScreenUpdating = False


'With Sheets("time")


For Each cell In rng






    If cell.Value <> "x" Then
    rng2.Value = thedate
    Else
    thedate = thedate + 1
    rng2.Value = thedate
    End If



Next


'End With


'Application.ScreenUpdating = True
MsgBox "ET: " & Format(Timer - f, "0.000") & "s"
Dim iRow As Long, erow As Long
erow = Cells(Rows.Count, "e").End(xlUp).Row
Dim thedate As Date
Dim f As Single
f = Timer()

    For iRow = erow To 35856
        If Cells(iRow, "G") = "x" Then

            Cells(iRow, "E").Value = Cells(iRow - 1, "E").Value + 1
        Else
            Cells(iRow, "E").Value = Cells(iRow - 1, "E").Value
        End If

    Next iRow

    MsgBox "ET: " & Format(Timer - f, "0.000") & "s"
End Sub
Application.Calculation = xlCalculationManual
    Application.EnableEvents = False

    For iRow = 3 To Cells(Rows.Count, "G").End(xlUp).Row
        If Cells(iRow, "G") = "x" Then
            Cells(iRow, "E").Value = Cells(iRow - 1, "E").Value + 1
        Else
            Cells(iRow, "E").Value = Cells(iRow - 1, "E").Value
        End If
    Next iRow


    Application.Calculation = xlCalculationAutomatic
    Application.EnableEvents = True