Information aus TextBox in Excel speichern

  • VB.NET

Es gibt 11 Antworten in diesem Thema. Der letzte Beitrag () ist von vatbub.

    Information aus TextBox in Excel speichern

    Hallo,

    habe bisher diesen Code, welcher mir aus meinen TextBox1-24 auf Knopfdruck die Informationen holt und in ein Excel sheet speichert.


    VB.NET-Quellcode

    1. Private Sub Button1_Click_1(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    2. Dim Excel As Object, Workbook As ObjectExcel = CreateObject("Excel.Application")Workbook = Excel.Workbooks.OpenXML("B:\Mappe1.xlsx")
    3. Workbook.Sheets(1).Cells(4, 1).Formula = TextBox1.Text
    4. Workbook.Sheets(1).Cells(5, 1).Formula = TextBox2.Text
    5. Workbook.Sheets(1).Cells(6, 1).Formula = TextBox3.Text
    6. Workbook.Sheets(1).Cells(7, 1).Formula = TextBox4.Text
    7. Workbook.Sheets(1).Cells(8, 1).Formula = TextBox5.Text
    8. Workbook.Sheets(1).Cells(9, 1).Formula = TextBox6.Text
    9. Workbook.Sheets(1).Cells(4, 2).Formula = TextBox7.Text
    10. Workbook.Sheets(1).Cells(5, 2).Formula = TextBox8.Text
    11. Workbook.Sheets(1).Cells(6, 2).Formula = TextBox9.Text
    12. Workbook.Sheets(1).Cells(7, 2).Formula = TextBox10.Text
    13. Workbook.Sheets(1).Cells(8, 2).Formula = TextBox11.Text
    14. Workbook.Sheets(1).Cells(9, 2).Formula = TextBox12.Text
    15. Workbook.Sheets(1).Cells(4, 3).Formula = TextBox13.Text
    16. Workbook.Sheets(1).Cells(5, 3).Formula = TextBox14.Text
    17. Workbook.Sheets(1).Cells(6, 3).Formula = TextBox15.Text
    18. Workbook.Sheets(1).Cells(7, 3).Formula = TextBox16.Text
    19. Workbook.Sheets(1).Cells(8, 3).Formula = TextBox17.Text
    20. Workbook.Sheets(1).Cells(9, 3).Formula = TextBox18.Text
    21. Workbook.Sheets(1).Cells(4, 4).Formula = TextBox19.Text
    22. Workbook.Sheets(1).Cells(5, 4).Formula = TextBox20.Text
    23. Workbook.Sheets(1).Cells(6, 4).Formula = TextBox21.Text
    24. Workbook.Sheets(1).Cells(7, 4).Formula = TextBox22.Text
    25. Workbook.Sheets(1).Cells(8, 4).Formula = TextBox23.Text
    26. Workbook.Sheets(1).Cells(9, 4).Formula = TextBox24.Text
    27.  
    28.  
    29. Excel.ActiveWorkbook.Save()
    30. Workbook.Close()
    31. End Sub



    Funktioniert ganz gut.

    Nun aber zwei Fragen:

    a) Wie kann ich das mit den Zellen und Textboxzuweisungen verschönen (bisher kopierte Zeilen mit anderen Zahlen --> unschön)

    b) Bisher speichert es mir meine Mappe an einem fest zugeordneten platz, wie kann ichs machen, dass er mich fragt wo er es speichern soll?



    Vielen Dank für euere Antworten

    ehrmmst schrieb:

    wie müsste ich deine Zeile einfügen
    Anstatt deines Codes. ;)
    Die erste Zeile erweiterst du zu einer For-Schleife.
    Die zweite Zeile erweiterst du mit deinem Sheet-Objekt.
    Das Ganze ergibt (ohne Open/Save/Close) einen Dreizeiler.
    --
    If Not Program.isWorking Then Code.Debug Else Code.DoNotTouch
    --
    Ich brings nicht zusammen. Ich hab jetzt folgendes gschrieben:

    VB.NET-Quellcode

    1. For i = 0 To 23
    2. Workbook.Sheets(1).Cells(4 + (i Mod 6), 1 + (i \ 6)).Formula = (("TextBox" & i & ".Text").ToString)
    3. Next


    Jetz spuckt er mir natürlich immer nur TextBox0.Text bis Textbox23.Text im Excel File aus.
    Ich hab jetz schon 2h rumprobiert wie ichs schreiben muss, ich komm einfach nicht auf die Syntax. Was mach ich falsch?
    mfg
    Jedes "Control" enthält eine Liste mit evtl enthaltenen weiteren Controls (Eine FORM ist zb AUCH ein Control). Diese ruft man ab mit
    DieseControl.Controls()
    Wenn jetzt auf einer Form eine Textbox liegt, die "Foo" heißt, kann ich diese Textbox schnappen, indem ich schreibe:
    Me.Controls("Foo")
    Und wenn sie nummeriert sind zb
    Me.Controls("Foo" & "1")
    und wenn sie nicht auf der Form, sondern in nem Panel sind:
    Me.Panel1.Controls("Foo" & "1")
    Und wenn es nicht DIESE Form ist, dann zb
    AndereForm.Panel1.Groupbox4.Controls("Foo" & "2")

    ehrmmst schrieb:

    = Me.Controls("TextBox" & "i")

    Zuerstmal ist das "i" natürlich ... FALSCH. Es muss i.ToString (ohne Anführungsstriche) sein.
    Dann liefert Controls("Foo") ein OBJECT und keine Textbox, also muss man Casten. Und nach dem Cast hat man natürlich per "." Zugriff auf "Text"

    VB.NET-Quellcode

    1. Dim s as String = DirectCast(Me.Controls("TextBox" & i.ToString), Textbox).Text
    Danke Danke Danke für deine Gedult.

    Hast mir sehr weitergeholfen und es funktioniert jetz auch. Hatte ich vorher noch nie gemacht und nach LearningByDoing kanns ichs dann beim nächsten mal (hoffentlich)

    Wieso geht eig der "Hilfreich" knopf nie?

    :)