Live-Forum - Die aktuellen Beiträge
Datum
Titel
24.04.2024 19:29:30
24.04.2024 18:49:56
24.04.2024 17:19:09
Anzeige
Archiv - Navigation
424to428
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
424to428
424to428
Aktuelles Verzeichnis
Verzeichnis Index
Verzeichnis Index
Übersicht Verzeichnisse
Inhaltsverzeichnis

Schleife programmieren

Schleife programmieren
13.05.2004 08:26:55
Regnar
Moin ich habe eine If then Anwesiung geschrieben bei der eine Zelle aufgrund eines Wertes in eíner anderen Zelle in einen bestimmten Ort kopiert wird. Bei meiner VBA Programmierung klappt das zwar aber bei mehr als einer Zeile wird die Geschwindigkeit so stark verringert das ein arbeiten nicht mehr möglich ist.
Hier der Code:

Private Sub Worksheet_Change(ByVal Target As Range)
Dim xWks As Worksheet
Set xWks = ThisWorkbook.Worksheets(1)
If xWks.Cells(1, 5) = 1 Then
xWks.Cells(4, 18).Value = xWks.Cells(4, 14).Value
End If
If xWks.Cells(1, 5) = 2 Then
xWks.Cells(4, 21).Value = xWks.Cells(4, 14).Value
End If
If xWks.Cells(1, 5) = 3 Then
xWks.Cells(4, 24).Value = xWks.Cells(4, 14).Value
End If
If xWks.Cells(1, 5) = 4 Then
xWks.Cells(4, 27).Value = xWks.Cells(4, 14).Value
End If
If xWks.Cells(1, 5) = 5 Then
xWks.Cells(4, 30).Value = xWks.Cells(4, 14).Value
End If
If xWks.Cells(1, 5) = 6 Then
xWks.Cells(4, 33).Value = xWks.Cells(4, 14).Value
End If
If xWks.Cells(1, 5) = 7 Then
xWks.Cells(4, 36).Value = xWks.Cells(4, 14).Value
End If
If xWks.Cells(1, 5) = 8 Then
xWks.Cells(4, 39).Value = xWks.Cells(4, 14).Value
End If
If xWks.Cells(1, 5) = 9 Then
xWks.Cells(4, 42).Value = xWks.Cells(4, 14).Value
End If
If xWks.Cells(1, 5) = 10 Then
xWks.Cells(4, 45).Value = xWks.Cells(4, 14).Value
End If
If xWks.Cells(1, 5) = 11 Then
xWks.Cells(4, 48).Value = xWks.Cells(4, 14).Value
End If
If xWks.Cells(1, 5) = 12 Then
xWks.Cells(4, 51).Value = xWks.Cells(4, 14).Value
End If
If xWks.Cells(1, 5) = 13 Then
xWks.Cells(4, 54).Value = xWks.Cells(4, 14).Value
End If
If xWks.Cells(1, 5) = 14 Then
xWks.Cells(4, 57).Value = xWks.Cells(4, 14).Value
End If
If xWks.Cells(1, 5) = 15 Then
xWks.Cells(4, 60).Value = xWks.Cells(4, 14).Value
End If
If xWks.Cells(1, 5) = 16 Then
xWks.Cells(4, 63).Value = xWks.Cells(4, 14).Value
End If
If xWks.Cells(1, 5) = 17 Then
xWks.Cells(4, 66).Value = xWks.Cells(4, 14).Value
End If
If xWks.Cells(1, 5) = 18 Then
xWks.Cells(4, 69).Value = xWks.Cells(4, 14).Value
End If
If xWks.Cells(1, 5) = 19 Then
xWks.Cells(4, 72).Value = xWks.Cells(4, 14).Value
End If
If xWks.Cells(1, 5) = 20 Then
xWks.Cells(4, 75).Value = xWks.Cells(4, 14).Value
End If
If xWks.Cells(1, 5) = 21 Then
xWks.Cells(4, 78).Value = xWks.Cells(4, 14).Value
End If
If xWks.Cells(1, 5) = 22 Then
xWks.Cells(4, 81).Value = xWks.Cells(4, 14).Value
End If
If xWks.Cells(1, 5) = 23 Then
xWks.Cells(4, 84).Value = xWks.Cells(4, 14).Value
End If
If xWks.Cells(1, 5) = 24 Then
xWks.Cells(4, 87).Value = xWks.Cells(4, 14).Value
End If
If xWks.Cells(1, 5) = 25 Then
xWks.Cells(4, 90).Value = xWks.Cells(4, 14).Value
End If
If xWks.Cells(1, 5) = 26 Then
xWks.Cells(4, 93).Value = xWks.Cells(4, 14).Value
End If
If xWks.Cells(1, 5) = 27 Then
xWks.Cells(4, 96).Value = xWks.Cells(4, 14).Value
End If
If xWks.Cells(1, 5) = 28 Then
xWks.Cells(4, 99).Value = xWks.Cells(4, 14).Value
End If
If xWks.Cells(1, 5) = 29 Then
xWks.Cells(4, 102).Value = xWks.Cells(4, 14).Value
End If
If xWks.Cells(1, 5) = 30 Then
xWks.Cells(4, 105).Value = xWks.Cells(4, 14).Value
End If
If xWks.Cells(1, 5) = 31 Then
xWks.Cells(4, 108).Value = xWks.Cells(4, 14).Value
End If
If xWks.Cells(1, 5) = 32 Then
xWks.Cells(4, 111).Value = xWks.Cells(4, 14).Value
End If
If xWks.Cells(1, 5) = 33 Then
xWks.Cells(4, 114).Value = xWks.Cells(4, 14).Value
End If
If xWks.Cells(1, 5) = 34 Then
xWks.Cells(4, 117).Value = xWks.Cells(4, 14).Value
End If
If xWks.Cells(1, 5) = 35 Then
xWks.Cells(4, 120).Value = xWks.Cells(4, 14).Value
End If
If xWks.Cells(1, 5) = 36 Then
xWks.Cells(4, 123).Value = xWks.Cells(4, 14).Value
End If
If xWks.Cells(1, 5) = 37 Then
xWks.Cells(4, 126).Value = xWks.Cells(4, 14).Value
End If
If xWks.Cells(1, 5) = 38 Then
xWks.Cells(4, 129).Value = xWks.Cells(4, 14).Value
End If
If xWks.Cells(1, 5) = 39 Then
xWks.Cells(4, 132).Value = xWks.Cells(4, 14).Value
End If
If xWks.Cells(1, 5) = 40 Then
xWks.Cells(4, 135).Value = xWks.Cells(4, 14).Value
End If
If xWks.Cells(1, 5) = 41 Then
xWks.Cells(4, 138).Value = xWks.Cells(4, 14).Value
End If
If xWks.Cells(1, 5) = 42 Then
xWks.Cells(4, 141).Value = xWks.Cells(4, 14).Value
End If
If xWks.Cells(1, 5) = 43 Then
xWks.Cells(4, 144).Value = xWks.Cells(4, 14).Value
End If
If xWks.Cells(1, 5) = 44 Then
xWks.Cells(4, 147).Value = xWks.Cells(4, 14).Value
End If
If xWks.Cells(1, 5) = 45 Then
xWks.Cells(4, 150).Value = xWks.Cells(4, 14).Value
End If
If xWks.Cells(1, 5) = 46 Then
xWks.Cells(4, 153).Value = xWks.Cells(4, 14).Value
End If
If xWks.Cells(1, 5) = 47 Then
xWks.Cells(4, 156).Value = xWks.Cells(4, 14).Value
End If
If xWks.Cells(1, 5) = 48 Then
xWks.Cells(4, 159).Value = xWks.Cells(4, 14).Value
End If
If xWks.Cells(1, 5) = 49 Then
xWks.Cells(4, 162).Value = xWks.Cells(4, 14).Value
End If
If xWks.Cells(1, 5) = 50 Then
xWks.Cells(4, 165).Value = xWks.Cells(4, 14).Value
End If
If xWks.Cells(1, 5) = 51 Then
xWks.Cells(4, 168).Value = xWks.Cells(4, 14).Value
End If
If xWks.Cells(1, 5) = 52 Then
xWks.Cells(4, 171).Value = xWks.Cells(4, 14).Value
End If
End Sub

Vielen Dank im voraus.

11
Beiträge zum Forumthread
Beiträge zu diesem Forumthread

Betreff
Datum
Anwender
Anzeige
AW: Schleife programmieren
13.05.2004 08:41:08
Matthias
Hallo Regnar,
geht der Code wirklich mit Excel 5?
Setze die Anweisung
Application.EnableEvents = False
an den Anfang des Codes und
Application.EnableEvents = True
ans Ende. Sonst wird nicht bei jeder Zelländerung innerhalb der Prozedur der Code neu aufgerufen, was zu den Verzögerungen führt.
Befasse dich außerdem mal mit der Select Case-Anweisung, um mehr Übersicht reinzukriegen (mehr Geschwindiglkeit damit wahrscheinlich nicht).
Gruß Matthias
AW: Schleife programmieren
Daniel
Hallo Regnar,
zu den Performanceproblemen kann ich Dir nichts sagen, aber um die Übersichtlichkeit Deines Codes zu erhöhen würde ich eine For ... Next Schleife benutzen. Sollte in Deinem Fall so funktioniern.
for i = 1 to 52
if xWks.Cells(1,5)=i+3 then
xWks.Cells(4, 24+3*i).Value=xWks.Cells(4,14).Value
next i
Gruß
Daniel
Anzeige
AW: Schleife programmieren
P@ulchen
Hi Daniel,
Dein Code funktioniert erst ab der Zahl 4...
Gruß aus Leipzig
P@ulchen
Das Forum lebt auch von den Rückmeldungen !
AW: Schleife programmieren
Daniel
Hallo Paulchen,
Du hast recht. Habe in der falschen Zeile angefangen.
Gruß Daniel
Der frühe Fuchs fängt das Huhn.
geht auch einfacher & vielleicht schneller
Tom
Code ersetzt Deine gesamten If Klauseln:
Dim x As Integer
Dim y As Integer
x = 1
y = 3
For i = 1 To 200

If Cells(1, 5) = x Then
xWks.Cells(4, 15 + y).Value = xWks.Cells(4, 14).Value
x = x + 1
y = y + 3

End If
Next i
Gruß
Tom
Anzeige
AW: geht auch einfacher & vielleicht schneller
P@ulchen
Hi Tom,
Dein Code funktioniert nicht richtig, da das End If an falscher Stelle steht und x somit nicht hochzählen kann...
So ginge es:


Dim As Integer
Dim As Integer
x = 1
y = 3
For i = 1 To 200
If Cells(1, 5) = x Then
    xWks.Cells(4, 15 + y).Value = xWks.Cells(4, 14).Value
    Exit Sub
End If
x = x + 1
y = y + 3
Next i


Gruß aus Leipzig
P@ulchen
Das Forum lebt auch von den Rückmeldungen !
Anzeige
AW: Schleife programmieren
P@ulchen
Hallo,
nun will ich auch noch meine Variante ohne Schleife vorschlagen:


Private Sub Worksheet_Change(ByVal Target As Range)
    Dim xWks As Worksheet
    Set xWks = ThisWorkbook.Worksheets(1)
    y = xWks.Cells(1, 5)
    If y > 0 And y < 53 And IsNumeric(y) Then
        xWks.Cells(4, 15 + 3 * y).Value = xWks.Cells(4, 14).Value
    End If
End Sub


Gruß aus Leipzig
P@ulchen
Das Forum lebt auch von den Rückmeldungen !
Anzeige
Ooops kleiner Fehler
Tom
habe die Schleife auch nicht getestet ! Gruß aus Hamburg
AW: Schleife programmieren
13.05.2004 09:31:16
Regnar
Vielen dank an alle aber wie mache ich das der auch in Zeile 5, 6, 7 etc funktioniert wenn ich einfach die 4 durch eine 5 ersetze klappt das nicht...Bin absoluter VBA Neuling muss man vielleicht dazu sagen
AW: Schleife programmieren
P@ulchen
Hi,
was meinst Du genau ? Soll die Bezugszelle immer E1 bleiben und bis zu welcher Zeile soll das Ganze funktionieren ?
Vielleicht könntest Du ja mal eine Beispieldatei hochladen.
Gruß aus Leipzig
P@ulchen
Das Forum lebt auch von den Rückmeldungen !
Anzeige
AW: Schleife programmieren
Regnar
Also E1 ist immer die Bezugsquelle für die KW und nach dieser soll er dann den Wert aus N4 in die zugeordnete Zelle kopieren
Bsp: wenn in E1 5 steht soll der Wert aus N4 nach AD 4 kopiert werden bei 6 nach AG4 usw.
und das bitte auch für die Zeilen 5,6 etc also so bis 15 wäre sensationell
BTW: Vielen Dank an alle die sich hier beteiligen

Beliebteste Forumthreads (12 Monate)

Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige
Anzeige
Anzeige