Live-Forum - Die aktuellen Beiträge
Anzeige
Archiv - Navigation
1956to1960
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

Datei aufsplitten in Dateien mit gleichem Wert in Spalte A

Datei aufsplitten in Dateien mit gleichem Wert in Spalte A
20.12.2023 14:25:41
Kedi0815
Hallo ihr VBA-Profis,

hoffentlich könnt ihr mir helfen!

In meiner Exceldatei stehen in Zeile 1 Überschriften und in Spalte A Zahlen, die als Text formatiert sind und mehrfach vorkommen können.
Ich suche ein Makro, dass jeweils die Überschrift und alle Zeilen von Spalte A bis AI, die den gleichen Wert in Spalte A enthalten, in eine neue Datei kopiert und mit der Zahl in Spalte A als Dateinamen im Pfad C:\xxx speichert.

Das mit KI-Hilfe erzeugte Makro legt zwar brav die Dateien an, leider sind die aber alle leer und ich kann den Fehler nicht finden.

Vielen Dank im Voraus für eure Hilfe!

Gruß Kedi

Sub CopyRowsToNewFile()

Dim lastRow As Long
Dim i As Long
Dim dict As Object
Dim key As Variant
Dim cell As Range
Dim newBook As Workbook
Dim newSheet As Worksheet
Dim fileName As String

'Create a dictionary to store the unique values in column A
Set dict = CreateObject("Scripting.Dictionary")

'Get the last row of data in column A
lastRow = Cells(Rows.Count, "A").End(xlUp).Row

'Loop through the data and add the unique values to the dictionary
For i = 2 To lastRow
If Not dict.exists(Cells(i, "A").Value) Then
dict.Add Cells(i, "A").Value, i
End If
Next i

'Loop through the dictionary and copy the rows to a new file
For Each key In dict.keys
'Create a new workbook and worksheet
Set newBook = Workbooks.Add
Set newSheet = newBook.Sheets(1)

'Copy the rows with the matching value in column A
For Each cell In Range("A2:A" & lastRow)
If cell.Value = key Then
Range("A" & cell.Row & ":AI" & cell.Row).Copy newSheet.Range("A" & Rows.Count).End(xlUp).Offset(1)
End If
Next cell

'Save the new file with the value from column A as the filename
fileName = "C:\xxx\" & key & ".xlsx"
newBook.SaveAs fileName

'Close the new file
newBook.Close False
Next key
End Sub

4
Beiträge zum Forumthread
Beiträge zu diesem Forumthread

Betreff
Datum
Anwender
Anzeige
AW: Datei aufsplitten in Dateien mit gleichem Wert in Spalte A
20.12.2023 16:48:54
Uduuh
Hallo,
in der for each cell in -Schleife bist du auf dem falschen Worksheet. newSheet ist aktiv.
Benutze niemals VBA-Schlüsselwörter als Variablen. filename, key

Sub CopyRowsToNewFile()

Dim lastRow As Long
Dim i As Long
Dim dict As Object
Dim vntKey As Variant
Dim cell As Range
Dim newBook As Workbook
Dim newSheet As Worksheet, aktWS As Worksheet
Dim strFileName As String

'Create a dictionary to store the unique values in column A
Set dict = CreateObject("Scripting.Dictionary")

'Get the last row of data in column A
lastRow = Cells(Rows.Count, "A").End(xlUp).Row

'Loop through the data and add the unique values to the dictionary
For i = 2 To lastRow
If Not dict.exists(Cells(i, "A").Value) Then
dict.Add Cells(i, "A").Value, i
End If
Next i

'Loop through the dictionary and copy the rows to a new file
For Each vntKey In dict.keys
'Create a new workbook and worksheet
Set newBook = Workbooks.Add
Set newSheet = newBook.Sheets(1)
aktWS.Cells(1, 1).Resize(, 35).Copy newSheet.Cells(1, 1)
'Copy the rows with the matching value in column A
For Each cell In aktWS.Range("A2:A" & lastRow)
If cell.Value = vntKey Then
cell(i, 1).Resize(, 35).Copy newSheet.Range("A" & Rows.Count).End(xlUp).Offset(1)
End If
Next cell

'Save the new file with the value from column A as the filename
strFileName = "C:\xxx\" & vntKey
newBook.SaveAs strFileName, xlOpenXMLWorkbook

'Close the new file
newBook.Close False
Next vntKey
End Sub

Gruß aus'm Pott
Udo
Anzeige
AW: Datei aufsplitten in Dateien mit gleichem Wert in Spalte A
20.12.2023 17:19:18
Kedi0815
Hallo Udo,

vielen Dank für deine Mühe!

Leider kommt für die Zeile
aktWS.Cells(1, 1).Resize(, 35).Copy newSheet.Cells(1, 1)
die Fehlermeldung: Objektvariable oder With-Blockvariable nicht festgelegt.

Gruß Kedi
AW: Datei aufsplitten in Dateien mit gleichem Wert in Spalte A
20.12.2023 18:29:58
Uduuh
vor die erste Schleife:
Set aktWS = ActiveSheet
AW: Datei aufsplitten in Dateien mit gleichem Wert in Spalte A
22.12.2023 15:19:15
Kedi0815
Hallo Udo,

konnte es erst heute ausprobieren. Jetzt werden immerhin schon die Dateien mit der Überschriftszeile angelegt.
Bloß die Datensätze fehlen noch. Bin am Verzweifeln.

Gruß Kedi

Links zu Excel-Dialogen

Beliebteste Forumthreads (12 Monate)

Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige
Anzeige
Anzeige