ich möchte gern nur die Zelleninhalte (aus der Spalte A) die mit der rote Hintergrundfarbe dargestellt sind in Arbeitsblatt "Export" in Spalte A kopieren.
Wie mache ich das per VBA?
Vielen Dank im Voraus...
Liebe Grüße
SELMA
Sub Zelleninhalte_bestimmte_Farbe_kopieren()
With Sheets("Export")
For i = 3 To Cells(Rows.Count, 1).End(xlUp).Row
If Cells(i, 1).Interior.ColorIndex = 3 Then
lz = .Cells(Rows.Count, 1).End(xlUp).Row + 1
Cells(i, 1).Copy .Cells(lz, 1)
End If
Next i
End With
End Sub
Sub Zelleninhalte_bestimmte_Farbe_kopieren()
Dim i, lz As Long
Application.ScreenUpdating = False ' schaltet die Screenaktualisierung aus
With Sheets("Export")
For i = 3 To Cells(Rows.Count, 1).End(xlUp).Row
If Cells(i, 1).Interior.ColorIndex = 3 Then
lz = .Cells(Rows.Count, 1).End(xlUp).Row + 1
.Cells(lz, 1) = Cells(i, 1) ' ohne Formatübertrag
End If
Next i
End With
Application.ScreenUpdating = True ' schaltet die Screenaktualisierung ein
End Sub
Option Explicit
Sub KopiereRotA()
Dim lngQ As Long, intS As Integer, rngF As Range, lngB As Long, lngZ As Long
Dim Calc As XlCalculation
Calc = Application.Calculation: Beschleuniger xlCalculationManual
lngQ = Cells(Rows.Count, 1).End(xlUp).Row
intS = Cells(1, Columns.Count).End(xlToLeft).Column
Cells(1, intS + 1).Select
ActiveWorkbook.Names.Add Name:="Farb", RefersToR1C1:= _
"=IF(GET.CELL(63,Tabelle1!RC[-10])=3,0,1)"
Range(Cells(1, intS + 1), Cells(lngQ, intS + 1)).Formula = "=Farb"
Range(Cells(1, intS + 2), Cells(lngQ, intS + 2)).Formula = "=ROW()"
ActiveSheet.Calculate
Range(Cells(1, intS + 2), Cells(lngQ, intS + 2)) = _
Range(Cells(1, intS + 2), Cells(lngQ, intS + 2)).Value
Cells(1, 1).Sort Key1:=Cells(1, intS + 1), Order1:=xlAscending, _
Header:=xlNo, OrderCustom:=1, Orientation:=xlTopToBottom
Set rngF = Columns(intS + 1).Find(What:=1, After:=Cells(lngQ, intS + 1), _
LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, SearchFormat:=False)
If rngF Is Nothing Then lngB = lngQ Else lngB = rngF.Row - 1
If lngB > 0 Then
With Sheets("Export")
lngZ = .Cells(Rows.Count, 1).End(xlUp).Row + 1
If IsEmpty(.Cells(lngZ, 1)) Then lngZ = 1
Range(Cells(1, 1), Cells(lngB, intS)).Copy Sheets("Export").Cells(lngZ, 1)
If lngB < lngQ Then _
Cells(1, 1).Sort Key1:=Cells(1, intS + 2), Order1:=xlAscending, _
Header:=xlNo, OrderCustom:=1, Orientation:=xlTopToBottom
End With
End If
Range(Columns(intS + 1), Columns(intS + 2)).Delete
ActiveWorkbook.Names("Farb").Delete
Beschleuniger Calc
End Sub
' Beschleuniger _______ Parameter: Calc-Status ______ gi/12.03.2006
'Aufruf:
' Dim Calc As XlCalculation
' Calc = Application.Calculation: Beschleuniger xlCalculationManual
' ....Code....
' Beschleuniger Calc
Sub Beschleuniger(StatCal As XlCalculation)
Application.Calculation = StatCal
Application.ScreenUpdating = (StatCal <> xlCalculationManual)
End Sub
Rückmeldung wäre nett! - Erich aus Kamp-Lintfort wünscht allen einen guten Start ins neue Jahr! Range(Cells(1, 1), Cells(lngB, 1)).Copy .Cells(lngZ, 1)
Rückmeldung wäre nett! - Grüße von Erich aus Kamp-Lintfort
Option Explicit
Sub KopiereRotA()
Dim lngQ As Long, intS As Integer, rngF As Range, lngB As Long, lngZ As Long
Dim Calc As XlCalculation
Calc = Application.Calculation: Beschleuniger xlCalculationManual
lngQ = Cells(Rows.Count, 1).End(xlUp).Row
intS = Cells(1, Columns.Count).End(xlToLeft).Column
Cells(1, intS + 1).Select
ActiveWorkbook.Names.Add Name:="Farb", RefersToR1C1:= _
"=IF(GET.CELL(63,Tabelle1!RC[-" & intS & "])=3,0,1)"
Range(Cells(1, intS + 1), Cells(lngQ, intS + 1)).Formula = "=Farb"
Range(Cells(1, intS + 2), Cells(lngQ, intS + 2)).Formula = "=ROW()"
ActiveSheet.Calculate
Range(Cells(1, intS + 2), Cells(lngQ, intS + 2)) = _
Range(Cells(1, intS + 2), Cells(lngQ, intS + 2)).Value
Cells(1, 1).Sort Key1:=Cells(1, intS + 1), Order1:=xlAscending, _
Header:=xlNo, OrderCustom:=1, Orientation:=xlTopToBottom
Set rngF = Columns(intS + 1).Find(What:=1, After:=Cells(lngQ, intS + 1), _
LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, SearchFormat:=False)
If rngF Is Nothing Then lngB = lngQ Else lngB = rngF.Row - 1
If lngB > 0 Then
With Sheets("Export")
lngZ = .Cells(Rows.Count, 1).End(xlUp).Row + 1
If IsEmpty(.Cells(lngZ, 1)) Then lngZ = 1
' Range(Cells(1, 1), Cells(lngB, intS)).Copy .Cells(lngZ, 1) ' ganze Zeilen
' Range(Cells(1, 1), Cells(lngB, 1)).Copy .Cells(lngZ, 1) ' nur Spalte A
' Range(.Cells(lngZ, 1), .Cells(lngZ + lngB - 1, intS)) = _
Range(Cells(1, 1), Cells(lngB, intS)).Value ' Werte, ganze Zeilen
Range(.Cells(lngZ, 1), .Cells(lngZ + lngB - 1, 1)) = _
Range(Cells(1, 1), Cells(lngB, 1)).Value ' Werte, nur Spalte A
If lngB < lngQ Then _
Cells(1, 1).Sort Key1:=Cells(1, intS + 2), Order1:=xlAscending, _
Header:=xlNo, OrderCustom:=1, Orientation:=xlTopToBottom
End With
End If
Range(Columns(intS + 1), Columns(intS + 2)).Delete
ActiveWorkbook.Names("Farb").Delete
Beschleuniger Calc
End Sub
' Beschleuniger _______ Parameter: Calc-Status ______ gi/12.03.2006
'Aufruf:
' Dim Calc As XlCalculation
' Calc = Application.Calculation: Beschleuniger xlCalculationManual
' ....Code....
' Beschleuniger Calc
Sub Beschleuniger(StatCal As XlCalculation)
Application.Calculation = StatCal
Application.ScreenUpdating = (StatCal <> xlCalculationManual)
End Sub
Unten kannst du dir aussuchen, ob du komplette Zellen oder nur Werte bzw. ganze Zeilen oder nur Spalte A kopieren willst.Um Zelleninhalte aus einem Arbeitsblatt zu kopieren, die eine bestimmte Hintergrundfarbe haben, kannst du ein VBA-Makro verwenden. Hier ist eine einfache Schritt-für-Schritt-Anleitung:
Öffne Excel und drücke Alt + F11
, um den VBA-Editor zu öffnen.
Füge ein neues Modul hinzu:
Einfügen
> Modul
.Kopiere den folgenden Code in das Modul:
Sub Zelleninhalte_bestimmte_Farbe_kopieren()
Dim i As Long, lz As Long
Application.ScreenUpdating = False ' Deaktiviere die Bildschirmaktualisierung
With Sheets("Export")
For i = 3 To Cells(Rows.Count, 1).End(xlUp).Row
If Cells(i, 1).Interior.ColorIndex = 3 Then ' Index 3 entspricht roter Farbe
lz = .Cells(Rows.Count, 1).End(xlUp).Row + 1
.Cells(lz, 1) = Cells(i, 1) ' Nur den Wert kopieren
End If
Next i
End With
Application.ScreenUpdating = True ' Aktiviere die Bildschirmaktualisierung
End Sub
Starte das Makro:
Alt + F8
, wähle das Makro aus und klicke auf Ausführen
.Mit diesem Makro kannst du effektiv die Zelleninhalte, die eine bestimmte Füllfarbe haben, kopieren.
ColorIndex
verwendest. Der ColorIndex
für Rot ist 3.Eine weitere Möglichkeit, Zelleninhalte mit einer bestimmten Farbe zu kopieren, ist die Verwendung von Excel-Formeln oder bedingter Formatierung. Diese Methoden sind jedoch weniger flexibel als die VBA-Lösung.
Hier sind einige Beispiele, wie du die VBA-Methode anpassen kannst:
Kopieren von Zellen mit blauer Farbe:
Ändere If Cells(i, 1).Interior.ColorIndex = 3
zu If Cells(i, 1).Interior.ColorIndex = 5
(Index 5 entspricht Blau).
Kopieren von Werten statt formatierten Zellen:
Verwende .Cells(lz, 1) = Cells(i, 1).Value
, um nur die Werte zu kopieren und nicht die Formatierung.
Application.Calculation = xlCalculationManual
, um die Berechnung während des Kopierens zu deaktivieren, was die Geschwindigkeit erhöhen kann.On Error Resume Next
verwendest.1. Wie kann ich die Hintergrundfarbe ändern, die ich kopieren möchte?
Ändere einfach den ColorIndex
im Code, um die Farbe zu wählen, die du kopieren möchtest.
2. Kann ich das Makro für andere Arbeitsblätter verwenden?
Ja, passe den Namen des Arbeitsblatts in der Zeile With Sheets("Export")
an, um ein anderes Arbeitsblatt zu verwenden.
3. Was mache ich, wenn ich nur die Formatierung und nicht die Werte kopieren möchte?
Verwende .Cells(lz, 1).PasteSpecial Paste:=xlPasteFormats
, um nur die Formatierungen zu kopieren.
Die erweiterte Suchfunktion hilft dir, gezielt die besten Antworten zu finden
Suche nach den besten AntwortenEntdecke unsere meistgeklickten Beiträge in der Google Suche
Top 100 Threads jetzt ansehen