VBA_Fahrplan mit OptionButton + Checkbox bestimmen

  • Excel

Es gibt 6 Antworten in diesem Thema. Der letzte Beitrag () ist von Hamsi.

    VBA_Fahrplan mit OptionButton + Checkbox bestimmen

    Hallo Zusammen,
    ich möchte mit VBA die Zeit eines Fahrplans ausrechnen. Ich habe sieben OptionsButtons die je ein Tag und vier Checkboxen die je ein Problem darstellen. Hinter jeder dieser Buttons ist ein Wert hinterlegt.Ich habe auch schon eine Programm geschrieben nur ist es sehr aufwändig, ich hoffe Ihr könnt mir vielleicht weiter helfen, wie ich es verkürzen bzw. vereinfachen kann.Ich hoffe es findet sich jemand.
    Danke :)

    Es ist ein kleiner Ausschnitt vom ganzen Code. Ist es möglich,diese Kombinationen von Klicks anders zur formulieren, da sich dieser Abschnitt sich erst nur für den Montag bezieht.

    VB.NET-Quellcode

    1. Option Explicit
    2. Private Sub CheckBox1_Click() 'Feiertag - a
    3. If OptionButton1.Value = True And CheckBox1.Value = True And CheckBox2.Value = False And CheckBox3.Value = False And CheckBox4.Value = False Then
    4. TextBox2.Text = Format$(DateAdd("n", Mo + a, TextBox1), " dd.mm.yyyy hh:nn:ss")
    5. ElseIf OptionButton1.Value = True And CheckBox1.Value = True And CheckBox2.Value = True And CheckBox3.Value = False And CheckBox4.Value = False Then
    6. TextBox2.Text = Format$(DateAdd("n", Mo + a + b, TextBox1), "dd.mm.yyyy hh:nn:ss")
    7. ElseIf OptionButton1.Value = True And CheckBox1.Value = True And CheckBox3.Value = True And CheckBox2.Value = False And CheckBox4.Value = False Then
    8. TextBox2.Text = Format$(DateAdd("n", Mo + a + c, TextBox1), " dd.mm.yyyy hh:nn:ss")
    9. ElseIf OptionButton1.Value = True And CheckBox1.Value = True And CheckBox4.Value = True And CheckBox2.Value = False And CheckBox3.Value = False Then
    10. TextBox2.Text = Format$(DateAdd("n", Mo + a + d, TextBox1), " dd.mm.yyyy hh:nn:ss")
    11. ElseIf OptionButton1.Value = True And CheckBox1.Value = True And CheckBox2.Value = True And CheckBox3.Value = True And CheckBox4.Value = False Then
    12. TextBox2.Text = Format$(DateAdd("n", Mo + a + b + c, TextBox1), " dd.mm.yyyy hh:nn:ss")
    13. ElseIf OptionButton1.Value = True And CheckBox1.Value = True And CheckBox2.Value = True And CheckBox4.Value = True And CheckBox3.Value = False Then
    14. TextBox2.Text = Format$(DateAdd("n", Mo + a + b + d, TextBox1), " dd.mm.yyyy hh:nn:ss")
    15. ElseIf OptionButton1.Value = True And CheckBox1.Value = True And CheckBox3.Value = True And CheckBox4.Value = True And CheckBox2.Value = False Then
    16. TextBox2.Text = Format$(DateAdd("n", Mo + a + c + d, TextBox1), " dd.mm.yyyy hh:nn:ss")
    17. ElseIf OptionButton1.Value = True And CheckBox1.Value = True And CheckBox2.Value = True And CheckBox3.Value = True And CheckBox4.Value = True Then
    18. TextBox2.Text = Format$(DateAdd("n", Mo + a + b + c + d, TextBox1), " dd.mm.yyyy hh:nn:ss")
    19. ElseIf OptionButton1.Value = True And CheckBox1.Value = False And CheckBox2.Value = False And CheckBox3.Value = False And CheckBox4.Value = False Then
    20. TextBox2.Text = Format$(DateAdd("n", Mo, TextBox1), " dd.mm.yyyy hh:nn:ss")
    21. '--------
    22. ElseIf OptionButton1.Value = True And CheckBox1.Value = False And CheckBox2.Value = False And CheckBox3.Value = False And CheckBox4.Value = False Then
    23. TextBox2.Text = Format$(DateAdd("n", Mo, TextBox1), " dd.mm.yyyy hh:nn:ss")
    24. ElseIf OptionButton1.Value = True And CheckBox1.Value = True And CheckBox2.Value = True And CheckBox3.Value = False And CheckBox4.Value = False Then
    25. TextBox2.Text = Format$(DateAdd("n", Mo + a + b, TextBox1), " dd.mm.yyyy hh:nn:ss")
    26. ElseIf OptionButton1.Value = True And CheckBox1.Value = True And CheckBox3.Value = True And CheckBox2.Value = False And CheckBox4.Value = False Then
    27. TextBox2.Text = Format$(DateAdd("n", Mo + a + c, TextBox1), " dd.mm.yyyy hh:nn:ss")
    28. ElseIf OptionButton1.Value = True And CheckBox1.Value = True And CheckBox4.Value = True And CheckBox2.Value = False And CheckBox3.Value = False Then
    29. TextBox2.Text = Format$(DateAdd("n", Mo + a + d, TextBox1), " dd.mm.yyyy hh:nn:ss")
    30. ElseIf OptionButton1.Value = True And CheckBox2.Value = True And CheckBox3.Value = True And CheckBox3.Value = False Then
    31. TextBox2.Text = Format$(DateAdd("n", Mo + b + c, TextBox1), " dd.mm.yyyy hh:nn:ss")
    32. ElseIf OptionButton1.Value = True And CheckBox2.Value = True And CheckBox4.Value = True And CheckBox3.Value = False Then
    33. TextBox2.Text = Format$(DateAdd("n", Mo + b + d, TextBox1), " dd.mm.yyyy hh:nn:ss")
    34. ElseIf OptionButton1.Value = True And CheckBox3.Value = True And CheckBox4.Value = True And CheckBox1.Value = False And CheckBox2.Value = False Then
    35. TextBox2.Text = Format$(DateAdd("n", Mo + c + d, TextBox1), " dd.mm.yyyy hh:nn:ss")
    36. ElseIf OptionButton1.Value = True And CheckBox1.Value = True And CheckBox2.Value = False And CheckBox3.Value = False And CheckBox4.Value = False Then
    37. TextBox2.Text = Format$(DateAdd("n", Mo + a, TextBox1), " dd.mm.yyyy hh:nn:ss")
    38. ElseIf OptionButton1.Value = True And CheckBox2.Value = True And CheckBox1.Value = False And CheckBox3.Value = False And CheckBox4.Value = False Then
    39. TextBox2.Text = Format$(DateAdd("n", Mo + b, TextBox1), " dd.mm.yyyy hh:nn:ss")
    40. ElseIf OptionButton1.Value = True And CheckBox3.Value = True And CheckBox2.Value = False And CheckBox1.Value = False And CheckBox4.Value = False Then
    41. TextBox2.Text = Format$(DateAdd("n", Mo + c, TextBox1), " dd.mm.yyyy hh:nn:ss")
    42. ElseIf OptionButton1.Value = True And CheckBox4.Value = True And CheckBox2.Value = False And CheckBox3.Value = False And CheckBox1.Value = False Then
    43. TextBox2.Text = Format$(DateAdd("n", Mo + d, TextBox1), " dd.mm.yyyy hh:nn:ss")
    44. ElseIf OptionButton1.Value = True Then
    45. TextBox2.Text = Format$(DateAdd("n", Mo, TextBox1), " dd.mm.yyyy hh:nn:ss")
    46. End If
    47. End Sub
    48. Private Sub OptionButton1_Click() 'Montag
    49. If OptionButton1.Value = True And CheckBox1.Value = True And CheckBox2.Value = True And CheckBox3.Value = True And CheckBox4.Value = True Then
    50. TextBox2.Text = Format$(DateAdd("n", Mo + a + b + c + d, TextBox1), " dd.mm.yyyy hh:nn:ss")
    51. ElseIf OptionButton1.Value = True And CheckBox1.Value = True And CheckBox2.Value = True And CheckBox3.Value = True And CheckBox4.Value = False Then
    52. TextBox2.Text = Format$(DateAdd("n", Mo + a + b + c, TextBox1), " dd.mm.yyyy hh:nn:ss")
    53. ElseIf OptionButton1.Value = True And CheckBox1.Value = True And CheckBox2.Value = True And CheckBox4.Value = True And CheckBox3.Value = False Then
    54. TextBox2.Text = Format$(DateAdd("n", Mo + a + b + d, TextBox1), " dd.mm.yyyy hh:nn:ss")
    55. ElseIf OptionButton1.Value = True And CheckBox1.Value = True And CheckBox3.Value = True And CheckBox4.Value = True And CheckBox2.Value = False Then
    56. TextBox2.Text = Format$(DateAdd("n", Mo + a + c + d, TextBox1), " dd.mm.yyyy hh:nn:ss")
    57. ElseIf OptionButton1.Value = True And CheckBox2.Value = True And CheckBox3.Value = True And CheckBox4.Value = True And CheckBox1.Value = False Then
    58. TextBox2.Text = Format$(DateAdd("n", Mo + b + c + d, TextBox1), " dd.mm.yyyy hh:nn:ss")
    59. ElseIf OptionButton1.Value = True And CheckBox1.Value = True And CheckBox2.Value = True And CheckBox3.Value = False And CheckBox4.Value = False Then
    60. TextBox2.Text = Format$(DateAdd("n", Mo + a + b, TextBox1), " dd.mm.yyyy hh:nn:ss")
    61. ElseIf OptionButton1.Value = True And CheckBox1.Value = True And CheckBox3.Value = True And CheckBox2.Value = False And CheckBox4.Value = False Then
    62. TextBox2.Text = Format$(DateAdd("n", Mo + a + c, TextBox1), " dd.mm.yyyy hh:nn:ss")
    63. ElseIf OptionButton1.Value = True And CheckBox1.Value = True And CheckBox4.Value = True And CheckBox2.Value = False And CheckBox3.Value = False Then
    64. TextBox2.Text = Format$(DateAdd("n", Mo + a + d, TextBox1), " dd.mm.yyyy hh:nn:ss")
    65. ElseIf OptionButton1.Value = True And CheckBox2.Value = True And CheckBox3.Value = True And CheckBox1.Value = False And CheckBox4.Value = False Then
    66. TextBox2.Text = Format$(DateAdd("n", Mo + b + c, TextBox1), " dd.mm.yyyy hh:nn:ss")
    67. ElseIf OptionButton1.Value = True And CheckBox2.Value = True And CheckBox4.Value = True And CheckBox1.Value = False And CheckBox3.Value = False Then
    68. TextBox2.Text = Format$(DateAdd("n", Mo + b + d, TextBox1), " dd.mm.yyyy hh:nn:ss")
    69. ElseIf OptionButton1.Value = True And CheckBox3.Value = True And CheckBox4.Value = True And CheckBox1.Value = False And CheckBox2.Value = False Then
    70. TextBox2.Text = Format$(DateAdd("n", Mo + c + d, TextBox1), " dd.mm.yyyy hh:nn:ss")
    71. ElseIf OptionButton1.Value = True And CheckBox1.Value = True And CheckBox2.Value = False And CheckBox3.Value = False And CheckBox4.Value = False Then
    72. TextBox2.Text = Format$(DateAdd("n", Mo + a, TextBox1), " dd.mm.yyyy hh:nn:ss")
    73. ElseIf OptionButton1.Value = True And CheckBox2.Value = True And CheckBox1.Value = False And CheckBox3.Value = False And CheckBox4.Value = False Then
    74. TextBox2.Text = Format$(DateAdd("n", Mo + b, TextBox1), " dd.mm.yyyy hh:nn:ss")
    75. ElseIf OptionButton1.Value = True And CheckBox3.Value = True And CheckBox2.Value = False And CheckBox1.Value = False And CheckBox4.Value = False Then
    76. TextBox2.Text = Format$(DateAdd("n", Mo + c, TextBox1), " dd.mm.yyyy hh:nn:ss")
    77. ElseIf OptionButton1.Value = True And CheckBox4.Value = True And CheckBox2.Value = False And CheckBox3.Value = False And CheckBox1.Value = False Then
    78. TextBox2.Text = Format$(DateAdd("n", Mo + d, TextBox1), " dd.mm.yyyy hh:nn:ss")
    79. ElseIf OptionButton1.Value = True Then
    80. TextBox2.Text = Format$(DateAdd("n", Mo, TextBox1), " dd.mm.yyyy hh:nn:ss")
    81. End If
    82. End Sub

    Dieser Beitrag wurde bereits 4 mal editiert, zuletzt von „Hamsi“ ()

    Formatiere mal den Code mit den BB Tags. Deinen Spaghetti Code kann so doch keiner lesen...
    "Gib einem Mann einen Fisch und du ernährst ihn für einen Tag. Lehre einen Mann zu fischen und du ernährst ihn für sein Leben."

    Wie debugge ich richtig? => Debuggen, Fehler finden und beseitigen
    Wie man VisualStudio nutzt? => VisualStudio richtig nutzen
    Nein. Das das ist doch kein XML Code... Du programmierst in VB.Net, also nutze den zugehörigen Knopf um den Code zu formatieren.
    "Gib einem Mann einen Fisch und du ernährst ihn für einen Tag. Lehre einen Mann zu fischen und du ernährst ihn für sein Leben."

    Wie debugge ich richtig? => Debuggen, Fehler finden und beseitigen
    Wie man VisualStudio nutzt? => VisualStudio richtig nutzen
    @mrMo: Nee, ist VBA, also wär der VB6-Button der richtige.
    @Hamsi: Zusätzlich schreibst Du, dass es um VBS geht, obwohl es um VBA geht. Nuja.

    Da ist sehr viel Redundanz im Code.
    Du hast überall als erste Bedingung: If OptionButton1.Value = True. Da kannst Du auch zum Prozeduranfang schreiben (hier mal korrekt getaggt):

    Visual Basic-Quellcode

    1. If OptionButton1.Value = False Then Exit Sub
    (@AlleAnderen: über das = False brauchen wir nicht zu reden. Es wäre effizienter zu schreiben If Not OptionButton1.Value, aber lesbarer/inhaltlich verständlicher wird's in diesem Falle dadurch wohl eher nicht)
    Dann kannst Du nämlich aus den ganzen komplexen Bedingungen die erste Bedingung (OptionButton1.Value = True) rausnehmen.
    Dann statt überall sowas zu schreiben:

    Visual Basic-Quellcode

    1. ... Then TextBox2.Text = Format$(DateAdd("n", Mo + d, TextBox1), " dd.mm.yyyy hh:nn:ss")
    schreib stattdessen

    Visual Basic-Quellcode

    1. 'zum Prozeduranfang:
    2. Dim FinalValue
    3. 'und später überall
    4. ... Then FinalValue = Mo + d
    5. 'und am Prozedurende:
    6. TextBox2.Text = Format$(DateAdd("n", FinalValue, TextBox1), " dd.mm.yyyy hh:nn:ss")


    ##########

    btw: wofür stehen denn die ganzen CheckBoxen?
    Dieser Beitrag wurde bereits 5 mal editiert, zuletzt von „VaporiZed“, mal wieder aus Grammatikgründen.

    Aufgrund spontaner Selbsteintrübung sind all meine Glaskugeln beim Hersteller. Lasst mich daher bitte nicht den Spekulatiusbackmodus wechseln.

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „VaporiZed“ ()

    Hallo VaporiZed,
    erstmal Danke für die Antwort.

    Ich will eine Fahrplan Simulieren, d.h ich habe eine Abfahrzeit, die Optionbuttons sind die Wochentage an dem der Zug abfährt, hinter jeder dieser Button steckt ein fester Wert. Die CheckBoxen sind unvorhergesehen Ereignisse die passieren können und im regulären Plan nicht drin stehen z.B Unfälle, Personalausfall usw. und diese CheckBoxen sind kombinierbar, also mehrere Buttons können angeklickt werden und jeder dieser Buttons hat einen andern Wert, da es einige Kombinationen sind ist der Code länger als erwartet und brauche es doch etwas vereinfacht, da ich bis jetzt nur eine Optionbutton (Montag) erledigt habe.