Einsteigerhilfe (VBA)

tobi91 @, Rottenburg am Neckar, Freitag, 07.06.2019, 14:03 (vor 10 Tagen) @ Martin Asal

mein erster Erfolg: :-D

geht bestimmt auch eleganter aber ^\_O_/^


Sub dataToTxt()

Dim myFile As String, rng As Range, cellValue As Variant, p As Integer, r As Integer, cellValue_d As Variant
Dim lastRow As Long, fileName As String, a As Integer, columnumber As Integer, b As Integer


With Sheets("makros_probieren.txt")                            'Sheet namen eintragen
    lastRow = .Range("A" & .Rows.Count).End(xlUp).Row
                                                'bestimmung letzte zeile
Set rng = Range("A1:B" & lastRow)                                'range für p, r loop
 
For b = 3 To 4                                                                 '1-48 eintragen, anzahl an files/ iterationen
   
    columnumber = b
    a = (columnumber - 1) * 6                                         'spalte überprüfen -> 1-48; 1 mit 5 ersetzten

                                                                                        'set proper location and file name
    fileName = "precipitation_h" & a

    myFile = Application.DefaultFilePath & "\" & fileName & ".txt"
    Open myFile For Output As #1

    For p = 1 To lastRow                                                    'p = zeilen, r = spalten
    cellValue_d = Cells(p, columnumber).Value
      For r = 1 To rng.Columns.Count
        cellValue = rng.Cells(p, r).Value
            If r = rng.Columns.Count Then
                Write #1, cellValue,
                Write #1, cellValue_d                                       'ohne Komma wird eine neue Zeile im txt begonnen
                Else
                Write #1, cellValue,
            End If
        Next r
    Next p
  
    Close #1                                                                     'schließe txt

Debug.Print fileName & " written"

Next b

End With

Debug.Print "loop finished"

End Sub

gesamter Thread:

 RSS-Feed dieser Diskussion

powered by my little forum