Dropdown Formatierung in Excel Übertragen
Schritt-für-Schritt-Anleitung
Um die Dropdown-Formatierung in Excel zu übernehmen, kannst Du den folgenden VBA-Code verwenden. Dieser Code sorgt dafür, dass die Formatierungen bei der Erstellung neuer Tabellenblätter beibehalten werden:
Sub KritToSheet()
Dim objShSource As Worksheet, objSh As Worksheet
Dim rng As Range, rngCopy As Range
Dim varTemp As Variant
Dim strFind As String, strFirst As String
Dim lngLast As Long, lngAct As Long
On Error GoTo ErrExit
With Application
.ScreenUpdating = False
.EnableEvents = False
.DisplayAlerts = False
.Calculation = xlCalculationManual
.Cursor = xlWait
End With
Set objShSource = Sheets("Tabelle1") 'Quelltabelle - anpassen
With objShSource
lngLast = .Cells(Rows.Count, 24).End(xlUp).Row
lngAct = 2
varTemp = .Range("A2:IV" & lngLast)
Do
strFind = .Cells(lngAct, 24)
If strFind <> "" Then
Set rng = .Range("X2:X" & lngLast).Find(what:=strFind, lookat:=xlWhole)
If Not rng Is Nothing Then
strFirst = rng.Address
Do
If rngCopy Is Nothing Then
Set rngCopy = .Rows(rng.Row)
Else
Set rngCopy = Union(rngCopy, .Rows(rng.Row))
End If
Set rng = .Range("X2:X" & lngLast).FindNext(rng)
Loop While Not rng Is Nothing And strFirst <> rng.Address
End If
If Not rngCopy Is Nothing Then
Set objSh = Worksheets.Add(after:=Sheets(Sheets.Count))
On Error Resume Next
objSh.Name = strFind
If Err.Number <> 0 Then
objSh.Name = strFind & Format(Now, "hhmmss")
Err.Clear
End If
On Error GoTo ErrExit
rngCopy.Copy objSh.Cells(2, 1)
objShSource.Rows(1).Copy objSh.Rows(1)
rngCopy.ClearContents
Set rngCopy = Nothing
Set objSh = Nothing
End If
End If
lngAct = lngAct + 1
Loop Until lngAct > lngLast
.Range("A2:IV" & lngLast) = varTemp
End With
ErrExit:
Set objShSource = Nothing
With Application
.ScreenUpdating = True
.EnableEvents = True
.DisplayAlerts = True
.Calculation = xlCalculationAutomatic
.Cursor = xlDefault
End With
End Sub
Häufige Fehler und Lösungen
-
Fehler: Formatierung geht verloren
- Stelle sicher, dass Du die Zellen nur von ihren Inhalten befreist und nicht die Zeilen löschst. Verwende
ClearContents
anstelle von Delete
.
-
Fehler: Dropdowns erscheinen nicht
- Prüfe, ob die Dropdown-Listen korrekt in der Quelltabelle definiert sind. Manchmal müssen die Datenüberprüfungen manuell neu zugewiesen werden.
Alternative Methoden
Eine alternative Methode zur Übertragung der Dropdown-Formatierung könnte das Erstellen einer Collection sein, um doppelte Werte zu vermeiden. Hier ist ein Beispiel:
Sub KritToSheet_Alternativ()
Dim objShSource As Worksheet, objSh As Worksheet
Dim rng As Range, rngCopy As Range
Dim colFind As New Collection
Dim strFind As String, strFirst As String
Dim lngLast As Long, lngZei As Long
On Error GoTo ErrExit
With Application
.ScreenUpdating = False
.EnableEvents = False
.DisplayAlerts = False
.Calculation = xlCalculationManual
.Cursor = xlWait
End With
Set objShSource = Sheets("Tabelle1") 'Quelltabelle - anpassen
With objShSource
lngLast = .Cells(Rows.Count, 24).End(xlUp).Row
For lngZei = 2 To lngLast
colFind.Add Item:=.Cells(lngZei, 24), Key:=.Cells(lngZei, 24).Text
Next
For lngZei = 1 To colFind.Count
strFind = colFind(lngZei)
Set rng = .Range("X2:X" & lngLast).Find(what:=strFind, lookat:=xlWhole)
If Not rng Is Nothing Then
strFirst = rng.Address
Do
If rngCopy Is Nothing Then
Set rngCopy = .Rows(rng.Row)
Else
Set rngCopy = Union(rngCopy, .Rows(rng.Row))
End If
Set rng = .Range("X2:X" & lngLast).FindNext(rng)
Loop While Not rng Is Nothing And strFirst <> rng.Address
End If
If Not rngCopy Is Nothing Then
Set objSh = Worksheets.Add(after:=Sheets(Sheets.Count))
On Error Resume Next
objSh.Name = strFind
If Err.Number <> 0 Then
objSh.Name = strFind & Format(Now, "hhmmss")
Err.Clear
End If
On Error GoTo ErrExit
rngCopy.Copy objSh.Cells(2, 1)
objShSource.Rows(1).Copy objSh.Rows(1)
Set rngCopy = Nothing
Set objSh = Nothing
End If
Next lngZei
End With
ErrExit:
Set objShSource = Nothing
With Application
.ScreenUpdating = True
.EnableEvents = True
.DisplayAlerts = True
.Calculation = xlCalculationAutomatic
.Cursor = xlDefault
End With
End Sub
Praktische Beispiele
-
Beispiel 1: Wenn Du eine Dropdown-Liste mit Werten in Spalte X hast, kannst Du die oben genannten Codes verwenden, um das Format zu übertragen. Achte darauf, die Quell-Tabelle korrekt zu benennen.
-
Beispiel 2: Nutze die Methode ClearContents
, um sicherzustellen, dass die Formatierung beibehalten wird, während die Daten entfernt werden. Dies hilft insbesondere beim excel dropdown formatierung übernehmen
.
Tipps für Profis
- Dokumentation: Halte Deine VBA-Codes gut dokumentiert, um die Wartung zu erleichtern.
- Testen: Teste Deine Codes in einer Kopie Deiner Datei, um versehentliche Datenverluste zu vermeiden.
- Optimierung: Überprüfe den Code auf Performance-Optimierungen, besonders bei großen Datenmengen.
FAQ: Häufige Fragen
1. Wie kann ich die Dropdown-Formatierung in eine andere Excel-Datei übertragen?
Du kannst die Dropdown-Listen manuell kopieren oder den VBA-Code anpassen, um die Formatierungen in die neue Datei zu übertragen.
2. Funktioniert dieser Code in Excel 2016 und neueren Versionen?
Ja, der VBA-Code ist mit Excel 2016 und neueren Versionen kompatibel. Achte darauf, dass die Makros aktiviert sind.