Performance 提高VBA中的循环效率

Performance 提高VBA中的循环效率,performance,excel,for-loop,vba,Performance,Excel,For Loop,Vba,我有一个For循环,它通过整数1到9循环,只找到与该整数对应的最底部的条目(即1,1,1,2,3,4,5将找到第三个“1”条目),并插入一个空行。我将数字与字符串“FN”连接起来,该字符串仅对应于此代码的应用程序,只是为了澄清。不管怎么说,它工作得很好,但是只需要运行9个整数,它就落后了很多。我希望有人能帮助我调试,以提高这段代码的速度。谢谢 如果任何人都能对填充空白行的一种好方法提供一些好处,那么就可以使用页面的页眉的格式化副本(“A1:L1”)。我尝试的代码在下一个I之前被注释掉 Sub t

我有一个
For
循环,它通过整数1到9循环,只找到与该整数对应的最底部的条目(即1,1,1,2,3,4,5将找到第三个“1”条目),并插入一个空行。我将数字与字符串“FN”连接起来,该字符串仅对应于此代码的应用程序,只是为了澄清。不管怎么说,它工作得很好,但是只需要运行9个整数,它就落后了很多。我希望有人能帮助我调试,以提高这段代码的速度。谢谢 如果任何人都能对填充空白行的一种好方法提供一些好处,那么就可以使用页面的页眉的格式化副本(“A1:L1”)。我尝试的代码在下一个I之前被注释掉

Sub test()

Dim i As Integer, Line As String, Cards As Range
Dim Head As Range, LR2 As Long


        For i = 1 To 9
    Line = "FN" & CStr(i)
    Set Cards = Sheets(1).Cells.Find(Line, after:=Cells(1, 1), searchdirection:=xlPrevious)

    Cards.Rows.Offset(1).EntireRow.Insert
    Cards.Offset(1).EntireRow.Select
'    Range("A" & (ActiveCell.Row), "K" & (ActiveCell.Row)) = Range("A3:K3")
'    Range("A" & (ActiveCell.Row), "K" & (ActiveCell.Row)).Font.Background = Range("A3:K3").Font.Background

     Next i


End Sub

这对我来说相当快

Sub Sample()
    Dim i As Long, line As String, Cards As Range

    With Sheets(1)
        For i = 1 To 9
            line = "FN" & i

            Set Cards = .Columns(6).Find(line, LookIn:=xlValues, lookat:=xlWhole)

            If Not Cards Is Nothing Then
                .Range("A3:K3").Copy
                Cards.Offset(1, -5).Insert Shift:=xlDown
            End If
         Next i
    End With
End Sub
之前

之后

这对我来说非常有效

Sub Sample()
    Dim i As Long, line As String, Cards As Range

    With Sheets(1)
        For i = 1 To 9
            line = "FN" & i

            Set Cards = .Columns(6).Find(line, LookIn:=xlValues, lookat:=xlWhole)

            If Not Cards Is Nothing Then
                .Range("A3:K3").Copy
                Cards.Offset(1, -5).Insert Shift:=xlDown
            End If
         Next i
    End With
End Sub
之前

之后

大多数改进将来自使用appTGGL helper函数更改应用程序环境变量,但这里的基本代码中有一些调整

Option Explicit

Sub ewrety()
    Dim f As Long, fn0 As String, fndfn As Range

    'appTGGL btggl:=false   'uncomment this when you are confident in it

    With Worksheets(1).Columns("F")
        For f = 1 To 9
            fn0 = Format$(f, "\F\N0")
            Set fndfn = .Find(What:=fn0, After:=.Cells(1), LookIn:=xlFormulas, LookAt:=xlWhole, _
                              SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False)
            With fndfn
                .Offset(1, -5).EntireRow.Insert Shift:=xlDown
                .Parent.Range("A1:L1, XFC1").Copy Destination:=.Offset(1, -5)
            End With
        Next f
    End With

    appTGGL
End Sub

Public Sub appTGGL(Optional bTGGL As Boolean = True)
    With Application
        .ScreenUpdating = bTGGL
        .EnableEvents = bTGGL
        .DisplayAlerts = bTGGL
        .AutoRecover.Enabled = bTGGL   'no interruptions with an auto-save
        .Calculation = IIf(bTGGL, xlCalculationAutomatic, xlCalculationManual)
        .CutCopyMode = False
        .StatusBar = vbNullString
    End With
    Debug.Print Timer
End Sub

大多数改进将来自使用appTGGL helper函数更改应用程序环境变量,但这里的基本代码中有一些调整

Option Explicit

Sub ewrety()
    Dim f As Long, fn0 As String, fndfn As Range

    'appTGGL btggl:=false   'uncomment this when you are confident in it

    With Worksheets(1).Columns("F")
        For f = 1 To 9
            fn0 = Format$(f, "\F\N0")
            Set fndfn = .Find(What:=fn0, After:=.Cells(1), LookIn:=xlFormulas, LookAt:=xlWhole, _
                              SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False)
            With fndfn
                .Offset(1, -5).EntireRow.Insert Shift:=xlDown
                .Parent.Range("A1:L1, XFC1").Copy Destination:=.Offset(1, -5)
            End With
        Next f
    End With

    appTGGL
End Sub

Public Sub appTGGL(Optional bTGGL As Boolean = True)
    With Application
        .ScreenUpdating = bTGGL
        .EnableEvents = bTGGL
        .DisplayAlerts = bTGGL
        .AutoRecover.Enabled = bTGGL   'no interruptions with an auto-save
        .Calculation = IIf(bTGGL, xlCalculationAutomatic, xlCalculationManual)
        .CutCopyMode = False
        .StatusBar = vbNullString
    End With
    Debug.Print Timer
End Sub

是否有一个特定的列,其中有
,或者它可以在工作表中的任何位置?我意识到我编写代码的方式对位置不明确,但它将始终位于F列。指定列会提高速度吗?是否有一个特定的列,其中有
,或者它可以位于表中的任何位置?我意识到我编写代码的方式对位置不明确,但它将始终在F列中。指定列会提高速度吗?一如既往,你是一个救生员!谢谢。很高兴能帮上忙:)一如既往,你是个救生员!谢谢。很高兴能帮上忙:)