Textboxen aus Zellen füllen mit Schleifen

  • Excel

Es gibt 14 Antworten in diesem Thema. Der letzte Beitrag () ist von Neptun.

    Textboxen aus Zellen füllen mit Schleifen

    Hallo

    Ich habe sehr viele Textboxen in einem Formular und in diesen sollen Werte aus einem Excel Sheet.
    Nun ist es bestimmt relativ unelegant und auch vom Rechenaufwand hoch, wenn ich jeden Befehl ausschreibe.
    Kann man sowas auch mit For-Schleifen lösen? Müsste ja vermutlich eine doppelt sein, aber da komm ich irgendwie nicht weiter

    Hier ein Ausschnitt aus dem derzeitigen Problem:
    (Eigentlich gibt nicht wie hier 4 Spalten sondern 10 und nicht nur 5 Zeilen sondern viel mehr)

    Visual Basic-Quellcode

    1. Private Sub UserForm_Initialize()
    2. Dim wstTab As Worksheet
    3. Set wstTab = Tabelle6
    4. Me.txtBreite1 = wstTab.Cells(5, 1).Value
    5. Me.txtLaenge1 = wstTab.Cells(5, 2).Value
    6. Me.txtStaerke1 = wstTab.Cells(5, 3).Value
    7. Me.txtHoehe1 = wstTab.Cells(5, 4).Value
    8. Me.txtBreite2 = wstTab.Cells(6, 1).Value
    9. Me.txtLaenge2 = wstTab.Cells(6, 2).Value
    10. Me.txtStaerke2 = wstTab.Cells(6, 3).Value
    11. Me.txtHoehe2 = wstTab.Cells(6, 4).Value
    12. Me.txtBreite3 = wstTab.Cells(7, 1).Value
    13. Me.txtLaenge3 = wstTab.Cells(7, 2).Value
    14. Me.txtStaerke3 = wstTab.Cells(7, 3).Value
    15. Me.txtHoehe3 = wstTab.Cells(7, 4).Value
    16. Me.txtBreite4 = wstTab.Cells(8, 1).Value
    17. Me.txtLaenge4 = wstTab.Cells(8, 2).Value
    18. Me.txtStaerke4 = wstTab.Cells(8, 3).Value
    19. Me.txtHoehe4 = wstTab.Cells(8, 4).Value
    20. Me.txtBreite5 = wstTab.Cells(9, 1).Value
    21. Me.txtLaenge5 = wstTab.Cells(9, 2).Value
    22. Me.txtStaerke5 = wstTab.Cells(9, 3).Value
    23. Me.txtHoehe5 = wstTab.Cells(9, 4).Value
    24. End Sub
    Nein sinnvoller auf keinen Fall. Der Code oben ist lang und unübersichtlich. Mit For ist es aufjedenfall kürzer. Ich wollte nur damit sagen das die Aussage falsch ist, dass der Code weniger Rechenleistung braucht. Wegen den Controls. Es ist eben eine Liste von Steuerelementen aus der Userform. Die List kann man durchloopen.

    Lg Mokki
    ​Smartnotr - ein intelligentes Notizprogramm
    zum Thread

    hmm achso ok. Ich habe hier mal einen längeren Ausschnitt zur Veranschaulichung.

    Habe auch mal angefangen mit den Controls aber funktioniert noch nicht so wirklich.

    Visual Basic-Quellcode

    1. rivate Sub subFillList1()
    2. ' Dim wstTab As Worksheet
    3. '
    4. ' Set wstTab = Tabelle6 'Rahmenbau mit Zerspanner
    5. '
    6. ' i = 5
    7. ' Me.txtB1 = wstTab.Cells(i, 1).Value
    8. ' Me.txtL1 = wstTab.Cells(i, 2).Value
    9. ' Me.txtS1 = wstTab.Cells(i, 3).Value
    10. ' Me.txtH1 = wstTab.Cells(5, 4).Value
    11. ' Me.txtLamellen1 = wstTab.Cells(i, 5).Value
    12. ' Me.txtZwStk1 = wstTab.Cells(i, 6).Value
    13. ' Me.txtK1 = wstTab.Cells(i, 7).Value
    14. ' Me.txtR1 = wstTab.Cells(i, 8).Value
    15. ' Me.txtAnzahlSB1 = wstTab.Cells(i, 9).Value
    16. ' Me.txtAnzahlZerspanner1 = wstTab.Cells(i, 10).Value
    17. ' Me.txtAnzahlZwStk1 = wstTab.Cells(i, 11).Value
    18. ' Me.txtSpannkr1 = Format(wstTab.Cells(i, 12).Value, "0")
    19. ' i = 6
    20. ' Me.txtB2 = wstTab.Cells(i, 1).Value
    21. ' Me.txtL2 = wstTab.Cells(i, 2).Value
    22. ' Me.txtS2 = wstTab.Cells(i, 3).Value
    23. ' Me.txtH2 = wstTab.Cells(i, 4).Value
    24. ' Me.txtLamellen2 = wstTab.Cells(i, 5).Value
    25. ' Me.txtZwStk2 = wstTab.Cells(i, 6).Value
    26. ' Me.txtK2 = wstTab.Cells(i, 7).Value
    27. ' Me.txtR2 = wstTab.Cells(i, 8).Value
    28. ' Me.txtAnzahlSB2 = wstTab.Cells(i, 9).Value
    29. ' Me.txtAnzahlZerspanner2 = wstTab.Cells(i, 10).Value
    30. ' Me.txtAnzahlZwStk2 = wstTab.Cells(i, 11).Value
    31. ' Me.txtSpannkr2 = Format(wstTab.Cells(i, 12).Value, "0")
    32. ...
    33. ...
    34. ...



    Visual Basic-Quellcode

    1. Private Sub subFillList2(wstTab As Worksheet, intFeldnummer As Integer)
    2. ' Allgemeine Deklarationen
    3. ' Felder
    4. Dim ctlB As Control
    5. Dim ctlL As Control
    6. Dim ctlS As Control
    7. Dim ctlH As Control
    8. Dim ctlLamellen As Control
    9. Dim ctlZwStk As Control
    10. Set wstTab = Tabelle6 'Rahmenbau mit Zerspanner
    11. For i = 1 To 5
    12. strKuerzelTag = Format(datMontag + i, "ddd")
    13. Set ctlB = Controls("txtB" & intFeldnummer)
    14. Set ctlL = Controls("txtL" & intFeldnummer)
    15. Set ctlS = Controls("txtS" & intFeldnummer)
    16. Set ctlH = Controls("txtH" & intFeldnummer)
    17. Set ctlLamellen = Controls("txtLamellen" & intFeldnummer)
    18. Set ctlZwStk = Controls("txtZwStk" & intFeldnummer)
    19. ctlB = "0"
    20. ctlL = "0"
    21. ctlS = "0"
    22. ctlH = "0"
    23. ctlLamellen = "0"
    24. ctlZwStk = "0"
    25. Next
    26. End Sub
    27. Private Sub subFillData()
    28. subFillList2 Tabelle6, 2
    29. End Sub
    Hey du kannst unabhängig von der Schleife (die hier leider nicht wirklich etwas bringt) die Textboxen auch direkt an Zellen binden (Über die Property ControlSource). Das kannst du auch direkt im Designer machen. Dann werden Änderungen an der Userform direkt in die Tabelle geschrieben und umgekehrt.
    Das ist meine Signatur und sie wird wunderbar sein!
    Hallo TeamBob,
    probier' mal dies:

    Visual Basic-Quellcode

    1. Dim wstTab As Worksheet, i As Long, Idx As String
    2. Set wstTab = Tabelle6
    3. For i = 1 To 5
    4. Idx = CStr(i)
    5. Me.Controls("txtBreite" & Idx).Text = wstTab.Cells(4 + i, 1).Value
    6. Me.Controls("txtLaenge" & Idx).Text = wstTab.Cells(4 + i, 2).Value
    7. Me.Controls("txtStaerke" & Idx).Text = wstTab.Cells(4 + i, 3).Value
    8. Me.Controls("txtHoehe" & Idx).Text = wstTab.Cells(4 + i, 4).Value
    9. Next i
    Gruss,

    Neptun