Live-Forum - Die aktuellen Beiträge
Anzeige
Archiv - Navigation
1904to1908
Aktuelles Verzeichnis
Verzeichnis Index
Übersicht Verzeichnisse
Vorheriger Thread
Rückwärts Blättern
Nächster Thread
Vorwärts blättern
Anzeige
HERBERS
Excel-Forum (Archiv)
20+ Jahre Excel-Kompetenz: Von Anwendern, für Anwender
Inhaltsverzeichnis

csv - DS eine ID zufügen

csv - DS eine ID zufügen
12.11.2022 13:15:20
Fred
Guten Tag Excel Experten,
ich habe im Ordner "Scalping" dutzende "csv Dateien"
C:\Users\Fred Neumann\Desktop\scalping
Meine Frage bzw. Bitte;
Kann ein Experte bitte mir ein Makro schreiben, welches all diese "csv's"
nacheinander öffnet
eine Spalte (als erste Spalte) einfügt: Titel "ID"
jeder DS von 1 beginnend durchnummeriert wird (datei übergreifend)
gespeichert und geschlossen werden
Ich bin mir nicht sicher, ob mein Anliegen in dieser Form durchführbar ist,- ob ich nicht zuvor die "csv's" in zB "xlsx" konfertieren muss.
Gruss
Fred

6
Beiträge zum Forumthread
Beiträge zu diesem Forumthread

Betreff
Datum
Anwender
Anzeige
AW: csv - DS eine ID zufügen
12.11.2022 14:07:05
Nepumuk
Hallo Fred,
teste mal:

Option Explicit
Public Sub Fred()
Const FOLDER_PATH As String = "C:\Users\Fred Neumann\Desktop\scalping\"
Dim strFilename As String
Dim lngLastRow As Long
Dim objWorkbook As Workbook
Application.ScreenUpdating = False
strFilename = Dir$(FOLDER_PATH & "*.csv")
Do Until strFilename = vbNullString
Set objWorkbook = Workbooks.Open(Filename:=FOLDER_PATH & strFilename, Local:=True)
With objWorkbook.Worksheets(1)
If GetLastCell(Cells, lngLastRow, 0, True, False) Then
Call .Columns(1).Insert
.Cells(1, 1).Value = "ID"
.Cells(2, 1).Value = 1
Call .Range(.Cells(2, 1), .Cells(lngLastRow, 1)).DataSeries
End If
End With
Application.DisplayAlerts = False
Call objWorkbook.SaveAs(Filename:=FOLDER_PATH & strFilename, FileFormat:=xlCSV, Local:=True)
Application.DisplayAlerts = True
Call objWorkbook.Close(SaveChanges:=False)
Set objWorkbook = Nothing
strFilename = Dir$
Loop
Application.ScreenUpdating = True
End Sub
Private Function GetLastCell( _
ByRef probjRange As Range, _
ByRef prlngLastRow As Long, _
ByRef prlngLastColumn As Long, _
Optional ByVal povblnReturnLastRow As Boolean = True, _
Optional ByVal povblnReturnLastColumn As Boolean = True) As Boolean
Dim objCell As Range
If Application.CountBlank(probjRange)  probjRange.Cells.CountLarge Then
With probjRange
If povblnReturnLastRow Then
Set objCell = .Find(What:="*", LookIn:=xlValues, Lookat:=xlWhole, _
SearchOrder:=xlByRows, SearchDirection:=xlPrevious)
prlngLastRow = objCell.Row
GetLastCell = True
End If
If povblnReturnLastColumn Then
Set objCell = .Find(What:="*", LookIn:=xlValues, Lookat:=xlWhole, _
SearchOrder:=xlByColumns, SearchDirection:=xlPrevious)
prlngLastColumn = objCell.Column
GetLastCell = True
End If
End With
Set objCell = Nothing
End If
End Function
Gruß
Nepumuk
Anzeige
AW: csv - DS eine ID zufügen
12.11.2022 15:19:16
Fred
Hallo Nepumuk,
das Makro arbeitet die Dateien wirklich ratzfatz ab!!
Problem:
Zum einen habe ich mich wohl nicht verständlich ausgedrückt. Ich wollte die "ID" durchgehend aller csv's
Also Beispiel:
1. Datei auf, Spalte "ID" voran erstellen, ab 2 Zeile Daten, jeweils 10 Zeilen = Nummerierung 1 bis 10
2. Datei auf, Spalte "ID" voran erstellen, ab 2 Zeile 11 bis .....
3. Datei auf, Spalte "ID" voran erstellen, ab 2 Zeile .... bis ....
usw
zum anderen geht das Script wol davon aus, das in der jeweiligen csv mit Semikolon getrennt wird, aber mit Komma wird getrennt. Dadurch wird in erster Spalte jeweils ID;" der Titelspalte vorangestellt und in den weiteren Zeilen die jeweilige Nummerierung plus ein AnführungszeichenHoch
Was an sich nicht so tragisch ist, da die erste Spalte in der csv mir nicht wichtig ist und diese zuvor beschriebene Kombi ja auch eine ID darstellt.
Kannst du nochmals entsprechend ändern, Nepumuk?
Gruss
Fred
Anzeige
AW: csv - DS eine ID zufügen
12.11.2022 16:10:53
Nepumuk
Hallo Fred,
dann so:

Option Explicit
Public Sub Fred()
Const FOLDER_PATH As String = "C:\Users\Fred Neumann\Desktop\scalping\"
Dim strFilename As String
Dim lngLastRow As Long, lngCount As Long
Dim objWorkbook As Workbook
Application.ScreenUpdating = False
lngCount = 1
strFilename = Dir$(FOLDER_PATH & "*.csv")
Do Until strFilename = vbNullString
Set objWorkbook = Workbooks.Open(Filename:=FOLDER_PATH & strFilename)
With objWorkbook.Worksheets(1)
If GetLastCell(Cells, lngLastRow, 0, True, False) Then
Call .Columns(1).Insert
.Cells(1, 1).Value = "ID"
.Cells(2, 1).Value = lngCount
Call .Range(.Cells(2, 1), .Cells(lngLastRow, 1)).DataSeries
lngCount = .Cells(.Rows.Count, 1).End(xlUp).Value + 1
End If
End With
Application.DisplayAlerts = False
Call objWorkbook.SaveAs(Filename:=FOLDER_PATH & strFilename, FileFormat:=xlCSV)
Application.DisplayAlerts = True
Call objWorkbook.Close(SaveChanges:=False)
Set objWorkbook = Nothing
strFilename = Dir$
Loop
Application.ScreenUpdating = True
End Sub
Private Function GetLastCell( _
ByRef probjRange As Range, _
ByRef prlngLastRow As Long, _
ByRef prlngLastColumn As Long, _
Optional ByVal povblnReturnLastRow As Boolean = True, _
Optional ByVal povblnReturnLastColumn As Boolean = True) As Boolean
Dim objCell As Range
If Application.CountBlank(probjRange)  probjRange.Cells.CountLarge Then
With probjRange
If povblnReturnLastRow Then
Set objCell = .Find(What:="*", LookIn:=xlValues, Lookat:=xlWhole, _
SearchOrder:=xlByRows, SearchDirection:=xlPrevious)
prlngLastRow = objCell.Row
GetLastCell = True
End If
If povblnReturnLastColumn Then
Set objCell = .Find(What:="*", LookIn:=xlValues, Lookat:=xlWhole, _
SearchOrder:=xlByColumns, SearchDirection:=xlPrevious)
prlngLastColumn = objCell.Column
GetLastCell = True
End If
End With
Set objCell = Nothing
End If
End Function
Gruß
Nepumuk
Anzeige
AW: Nepumuk: PERFEKT
12.11.2022 16:32:04
Fred
Hallo Nepumuk,
Perfekt, das Makro arbeitet nun so ab wie gewünscht!!!
Vieleicht eine abschließende Frage noch;
Wo - im Code- bist du drauf eingegangen, dass die Spalten mit Komma statt Semikolon getrennt sind?
Gruss
Fred
AW: Nepumuk: PERFEKT
12.11.2022 17:33:52
Nepumuk
Hallo Fred,
indem ich den Parameter Local:=True beim Öffnen und beim Speichern weggelassen habe.
Gruß
Nepumuk
AW: Nepumuk: PERFEKT
12.11.2022 17:37:41
Fred
Hallo Nepumuk,
jo, kurz & schmerzlos :-)
Vielen Dank für deine Zeit und Kompetenz
& ein schönes Wochenende
Gruss
Fred

Beliebteste Forumthreads (12 Monate)

Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige
Anzeige
Anzeige