Live-Forum - Die aktuellen Beiträge
Anzeige
Archiv - Navigation
1232to1236
Aktuelles Verzeichnis
Verzeichnis Index
Übersicht Verzeichnisse
Vorheriger Thread
Rückwärts Blättern
Nächster Thread
Vorwärts blättern
Anzeige
HERBERS
Excel-Forum (Archiv)
20+ Jahre Excel-Kompetenz: Von Anwendern, für Anwender
Inhaltsverzeichnis

Diese Schleife läuft grottenlangsam

Diese Schleife läuft grottenlangsam
Marlon
Hallo,
kann mir jemand bitte mit dieser Schleife helfen. Ich weiß nicht, wie ich das Ding schneller bekomme.
Momentan schafft mein Excelchen gerade mal etwa 3-4 Zeilen pro Sekunde. Dabei hab ich das Hochzählen in der Schleife schon auf 30 limitiert. Das wird eine ziemliche Quälerei auf die 500000 Zeilen zu kommen.

Sub LastDurchlauf()
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Application.EnableEvents = False
Set Main = Sheets("Main")
lrow = Range("C1000000").End(xlUp).Row
For i = 5 To lrow
For y = i - 1 To 4 Step -1
If Cells(y, 3) = Cells(i, 3) Then
Cells(i, 57) = i - y
Exit For
End If
If i - y > 30 Then
Cells(i, 57) = "X"
Exit For
End If
Next y
For x = i + 1 To lrow
If Cells(x, 3) > Cells(i, 3) - 2 And Cells(x, 3)  30 Then
Cells(i, 58) = "X"
Exit For
End If
Next x
Range("I1") = i
Application.ScreenUpdating = True
Application.ScreenUpdating = False
Next i
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub
ESCAPE-Taste - Excel reagiert nicht:
Abgesehen davon läßt sich das VBA-Script, wenn es sich einmal warmgelaufen hat, nicht mehr mit esc stoppen. Ich habe jetzt zwischendurch dieses screenupdate an und aus eingeschoben, um wenigstens zu wissen, wo er gerade ist. Verlangsamt das den Prozess sehr?
Danke und herzliche Grüße,
Marlon

2
Beiträge zum Forumthread
Beiträge zu diesem Forumthread

Betreff
Benutzer
Anzeige
AW: Diese Schleife läuft grottenlangsam
10.10.2011 21:00:56
Nepumuk
Hallo,
versuch es mal so:

Public Sub LastDurchlauf()
    
    Dim lngLastRow As Long, lngMainRow As Long
    Dim lngSubRow1 As Long, lngSubRow2 As Long
    Dim vntInputArray As Variant
    Dim vntOutputArray1 As Variant, vntOutputArray2 As Variant
    
    lngLastRow = Cells(Rows.Count, 3).End(xlUp).Row
    vntInputArray = Range(Cells(1, 3), Cells(lngLastRow, 3)).Value2
    vntOutputArray1 = Range(Cells(1, 57), Cells(lngLastRow, 57)).Value2
    vntOutputArray2 = Range(Cells(1, 58), Cells(lngLastRow, 58)).Value2
    
    For lngMainRow = 5 To lngLastRow
        For lngSubRow1 = lngMainRow - 1 To 4 Step -1
            If vntInputArray(lngSubRow1, 1) = vntInputArray(lngMainRow, 1) Then
                vntOutputArray1(lngMainRow, 1) = lngMainRow - lngSubRow1
                Exit For
            End If
            If lngMainRow - lngSubRow1 > 30 Then
                vntOutputArray1(lngMainRow, 1) = "X"
                Exit For
            End If
        Next lngSubRow1
        For lngSubRow2 = lngMainRow + 1 To lngLastRow
            If vntInputArray(lngSubRow2, 1) > vntInputArray(lngMainRow, 1) - 2 Then
                If vntInputArray(lngSubRow2, 1) < vntInputArray(lngMainRow, 1) + 2 Then
                    vntOutputArray2(lngMainRow, 1) = lngSubRow2 - lngMainRow
                    Exit For
                End If
            End If
            If lngSubRow2 > 30 Then
                vntOutputArray2(lngMainRow, 1) = "X"
                Exit For
            End If
        Next lngSubRow2
    Next lngMainRow
    
    With Application
        .ScreenUpdating = False
        .Calculation = xlCalculationManual
        .EnableEvents = False
    End With
    
    Range(Cells(1, 57), Cells(lngLastRow, 57)).Value2 = vntOutputArray1
    Range(Cells(1, 58), Cells(lngLastRow, 58)).Value2 = vntOutputArray2
    
    With Application
        .EnableEvents = True
        .Calculation = xlCalculationAutomatic
        .ScreenUpdating = True
    End With
End Sub

Gruß
Nepumuk
Anzeige
AW: Diese Schleife läuft grottenlangsam
11.10.2011 10:16:37
Marlon
Ich mußte das mit den Arrays erstmal begreifen, aber jetzt: Die Geschwindigkeit ist der Wahnsinn. Vielen Dank dafür, dass Du Dir die Mühe gemacht hast. Großartig.
Marlon

Beliebteste Forumthreads (12 Monate)

Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige
Anzeige
Anzeige