AW: Wenn textbox leer bei Export ausfüllen
13.04.2022 11:13:19
Yal
Hallo Andre,
sehe ich das richtig, dass der einzige Grund, die letzte Zeile anders zu behandeln, ist die letzte zeile in der Datei ohne Carriage Return (=neue Zeile, = ";") reinzubringen?
Wenn Du schon durch die For-Schleife weisst, welche Wert die Variable i hat, musst Du diese nicht separat testen.
Reduziere die For-Schleife um ein Element (also ListCount -2) und behandle anschliessend die letzte Zeile*
Private Sub CommandButton_Export_Click()
Dim i As Long
Dim j As Long
Dim sFile As String, stext$, iFilenr As Integer
Const sSep As String = ";"
iFilenr = FreeFile
sFile = "C:\Users\andre\OneDrive\Dokumente\Arbeit Andre\" & TextBox_kurzbez.Value & "_warenausgang_" & TextBox_Auftrag.Value & ".csv"
Open sFile For Output As iFilenr
Print #iFilenr, "AUFTRAGSNR;MANDANT;ZUSTAND;AUFTRAGSART;VERSANDART;ZOLL_KNZ;PRIO;ANZ_POS;ANZ_OPOS;LIEFER_DATUM;VERLADE_DATUM;AUFTRAGS_DATUM;KUNDENNR;SPRACHE;LAGER_NR;POS_AUFTRAGSNR;POS_MANDANT;POS_AUFTRAGSPOS;POS_ARTIKELNR;POS_ZUSTAND;POS_MENGE_SOLL;POS_MENGE_IST;POS_EINHEIT;CHARGE"
With UserForm_WA.ListBox1
For i = 0 To .ListCount - 2
Print #iFilenr, Join(Array(.List(i, 0), .List(i, 2), "10", ComboBox_art, .List(i, 9), "N", "50", ListBox1.ListCount, ListBox1.ListCount, .List(i, 4) & " 00:00:00.000", .List(i, 5) & " 00:00:00.000", .List(i, 6) & " 00:00:00.000", .List(i, 3), "D", "001", .List(i, 0), .List(i, 2), .List(i, 1), .List(i, 7), "10", .List(i, 8), "0", "STK", IIf(TextBox_charge = "", "nocharge", TextBox_charge)), sSep)
Next
Print #iFilenr, Join(Array(.List(i, 0), .List(i, 2), "10", ComboBox_art, .List(i, 9), "N", "50", ListBox1.ListCount, ListBox1.ListCount, .List(i, 4) & " 00:00:00.000", .List(i, 5) & " 00:00:00.000", .List(i, 6) & " 00:00:00.000", .List(i, 3), "D", "001", .List(i, 0), .List(i, 2), .List(i, 1), .List(i, 7), "10", .List(i, 8), "0", "STK", IIf(TextBox_charge = "", "nocharge", TextBox_charge)), sSep);
End With
Close iFilenr
MsgBox "Datei wurde angelegt:" & vbLf & sFile, vbInformation, " "
End Sub
*: eine For i = 1 to 5 wirkt nur bis i = 5 aber springt erst raus, wenn i = 6. Das erfolgt durch "Next". Also wenn die For-Schleife zu Ende ist, ist i = 6.
genauso bei For i = 0 To .ListCount - 2 : nach Ende der For-Schleifendurchlauf ist i = .ListCount -1
VG
Yal