Makro auf mehrere Blätter anwenden
Hallo zusammen,
habe folgendes Makro(Auszug), das ich gerne auf mehrere Blätter anwenden möchte. Bisher funktioniert es nur, wenn ich es auf jedes einzelne Blatt anwende. Hier werden Werte aus Spalte I mit denen aus Spalte G verglichen. Steht in Spalte I ein Wert, werden die Zellen F,G,H durchgestrichen.
Hoffe ihr habt Ideen.
Option Explicit
Public Sub vergleichen()
Dim lngI As Long, intWert As Integer
Application.ScreenUpdating = False
For lngI = 1 To Cells(Rows.Count, 1).End(xlUp).Row
intWert = Application.WorksheetFunction. _
CountIf(Worksheets("11.15").Range("I:I"), Cells(lngI, 7).Value)
If intWert > 0 Then
Cells(lngI, 6).Font.Strikethrough = True
Cells(lngI, 7).Font.Strikethrough = True
Cells(lngI, 8).Font.Strikethrough = True
End If
Next lngI
For lngI = 1 To Cells(Rows.Count, 1).End(xlUp).Row
intWert = Application.WorksheetFunction. _
CountIf(Worksheets("11.35").Range("I:I"), Cells(lngI, 7).Value)
If intWert > 0 Then
Cells(lngI, 6).Font.Strikethrough = True
Cells(lngI, 7).Font.Strikethrough = True
Cells(lngI, 8).Font.Strikethrough = True
End If
Next lngI
Application.ScreenUpdating = True
End Sub
Antwort schreiben
Antwort 1 von Saarbauer vom 05.09.2020, 08:28 Options
Hallo,
du musst deine entsprechenden Blätter auch einbinden.
Da entsprechende Angaben zu den Blättern fehlen, wäre es vielleicht mit einer For-Schleife zu machen
For i = (1.Blatt) to (letztes Blatt
Sheets(i).Select
und dann deine Anweisungen
zum Schluss
Nexi i
Gruß
Helmut
Antwort 2 von Martin123 vom 05.09.2020, 08:43 Options
Hallo Helmut,
bin leider nicht so der Held, was VBA angeht. Hab's mal so versucht. War leider nix. kannst du mir zeigen, wo ich die Schleife einbinden muss?
Public Sub vergleichen()
Dim lngI As Long, intWert As Integer
Dim i As Integer
Application.ScreenUpdating = False
For i = (11.15) to (11.35)
Sheets(i).Select
For lngI = 1 To Cells(Rows.Count, 1).End(xlUp).Row
intWert = Application.WorksheetFunction. _
CountIf(Worksheets("11.15").Range("I:I"), Cells(lngI, 7).Value)
If intWert > 0 Then
Cells(lngI, 6).Font.Strikethrough = True
Cells(lngI, 7).Font.Strikethrough = True
Cells(lngI, 8).Font.Strikethrough = True
End If
Next lngI
For lngI = 1 To Cells(Rows.Count, 1).End(xlUp).Row
intWert = Application.WorksheetFunction. _
CountIf(Worksheets("11.35").Range("I:I"), Cells(lngI, 7).Value)
If intWert > 0 Then
Cells(lngI, 6).Font.Strikethrough = True
Cells(lngI, 7).Font.Strikethrough = True
Cells(lngI, 8).Font.Strikethrough = True
End If
Next lngI
Next i
Application.ScreenUpdating = True
End Sub
Antwort 3 von Saarbauer vom 05.09.2020, 20:53 Options
Hallo,
so funktioniert das nicht
Zitat:
For i = (11.15) to (11.35)
Hier müsste z.B. stehen
For i = 15 to 35
Wenn es das Tabellenblatt 15 bis 35 ist
Leider sind deine Angaben nicht so, dass man dir direkt weiterhelfen kann.
Gruß
Helmut
Antwort 4 von Martin123 vom 08.09.2020, 07:11 Options
Hallo,
Die Tabellenblätter heißen 11.15, 11.35, 12.35......40.91. Das sind Typenbezeichnungen.
Bekomme beim Ausführen die Fehlermeldung " Laufzeitfehler 9: Index außerhalb des gültigen Bereichs". Die Tabellen können/sollten nicht umbenannt werden. Ist das Problem dennoch mit einer Schleife lösbar?
Gruß
Martin
Antwort 5 von rainberg vom 08.09.2020, 08:19 Options
Hallo Martin,
probiers mal so.
Option Explicit
Public Sub vergleichen()
Dim lngI As Long, lngN As Long, intWert As Integer
Application.ScreenUpdating = False
For lngN = 1 To Worksheets.Count
For lngI = 1 To Cells(Rows.Count, 1).End(xlUp).Row
intWert = Application.WorksheetFunction. _
CountIf(Worksheets(lngN).Range("I:I"), Cells(lngI,
7).Value)
If intWert > 0 Then
With Worksheets(lngI)
.Cells(lngI, 6).Font.Strikethrough = True
.Cells(lngI, 7).Font.Strikethrough = True
.Cells(lngI, 8).Font.Strikethrough = True
End With
End If
Next lngI
Next lngN
Application.ScreenUpdating = True
End Sub
Gruß
Rainer
Antwort 6 von Martin123 vom 08.09.2020, 09:22 Options
Hallo Rainer,
jetzt wird die Fehlermeldung "Variable nicht definiert" für IngN angezeigt. Mit String und Integer klappts auch nicht. Hast du eine Idee?
Antwort 7 von rainberg vom 08.09.2020, 10:47 Options
Hallo Martin,
da hast Du einen Schreibfehler eingebaut.
Die Variable heißt
lngN und nicht IngN
Schreibfehler vermeidet man indem man das Makro einfach kopiert und einfügt.
Gruß
Rainer
Antwort 8 von Martin123 vom 08.09.2020, 11:43 Options
Hab dein Makro kopiert und die gleiche Fehlermeldung wie bei Helmuts Vorschlag: "Index ausserhalb des gültigen Bereichs"
Antwort 9 von rainberg vom 08.09.2020, 12:18 Options
Hallo Martin,
ich habe Dein Makro nur angepasst und bin davon ausgegangen, dass es im Urzustand lief.
Da ich kein Testobjekt habe, kann ich Dir leider nicht helfen.
Du könntest noch folgende Änderung durchführen.
Option Explicit
Public Sub vergleichen()
Dim lngI As Long, lngN As Long, intWert As Integer
Application.ScreenUpdating = False
For lngN = 1 To Worksheets.Count
For lngI = 1 To Cells(Rows.Count, 1).End(xlUp).Row
intWert = Application.WorksheetFunction. _
CountIf(Worksheets(lngN).Range("I:I"), _
Worksheets(lngN).Cells(lngI, 7).Value)
If intWert > 0 Then
With Worksheets(lngI)
.Cells(lngI, 6).Font.Strikethrough = True
.Cells(lngI, 7).Font.Strikethrough = True
.Cells(lngI, 8).Font.Strikethrough = True
End With
End If
Next lngI
Next lngN
Application.ScreenUpdating = True
End Sub
Gruß
Rainer
Antwort 10 von rainberg vom 08.09.2020, 12:39 Options
Hallo Martin,
ändere diese Zeile
With Worksheets(lngI)
in
With Worksheets(lngN)
Das war ein Fehler von mir.
Gruß
Rainer
Antwort 11 von Martin123 vom 08.09.2020, 12:44 Options
hallo Rainer,
im Urzustand läuft das Makro. Ich muss halt jedes Tabellenblatt öffnen und das Makro ausführen. Mein Problem ist nur, dass ich das Makro gerne einmal für die komplette Datei ausführen möchte.
Ausschnitt der funktionsfähigen Version:
Option Explicit
Public Sub vergleichen()
Dim lngI As Long, intWert As Integer
Application.ScreenUpdating = False
For lngI = 1 To Cells(Rows.Count, 1).End(xlUp).Row
intWert = Application.WorksheetFunction. _
CountIf(Worksheets("11.15").Range("I:I"), Cells(lngI, 7).Value)
If intWert > 0 Then
Cells(lngI, 6).Font.Strikethrough = True
Cells(lngI, 7).Font.Strikethrough = True
Cells(lngI, 8).Font.Strikethrough = True
End If
Next lngI
For lngI = 1 To Cells(Rows.Count, 1).End(xlUp).Row
intWert = Application.WorksheetFunction. _
CountIf(Worksheets("11.35").Range("I:I"), Cells(lngI, 7).Value)
If intWert > 0 Then
Cells(lngI, 6).Font.Strikethrough = True
Cells(lngI, 7).Font.Strikethrough = True
Cells(lngI, 8).Font.Strikethrough = True
End If
Next lngI
Application.ScreenUpdating = True
End Sub
Kannst du damit was anfangen?
Antwort 12 von Martin123 vom 08.09.2020, 12:47 Options
werd's damit mal versuchen. Danke
Antwort 13 von Martin123 vom 09.09.2020, 08:07 Options
Das Ergebnis ist das gleiche, wie mit meiner Version. Der Code ist nur übersichtlicher. Muss aber immer noch jedes Blatt öffnen, um die Zellen durchzustreichen.
Ich lebe vorerst mal damit.
Danke an alle
Gruß
Antwort 14 von rainberg vom 09.09.2020, 08:12 Options
Hallo Martin,
der Code ist getestet und läuft.
Wenn das bei Dir nicht der Fall ist, solltest Du Deine Mappe mal hoch laden, ansonsten kann ich Dir nicht weiter helfen.
Gruß
Rainer
Antwort 15 von Martin123 vom 09.09.2020, 09:33 Options
Hallo Rainer,
der Code läuft, jedoch nur für das jeweils aktive Blatt. Bei 8 Blättern muss ich das Makro also 8 mal ausführen. Mir wäre einmal für die ganze Datei lieber gewesen.
Hochladen kann ich die Datei nicht. Müsste eine ähnliche erstellen. Würde etwas dauern.
Gruß
Martin
Antwort 17 von Martin123 vom 09.09.2020, 13:28 Options
Da bin ich platt :-)
werde die Datei mal hochladen. Muss schauen ob ich es heute noch schaffe.
Antwort 18 von Martin123 vom 09.09.2020, 13:51 Options
hier nun der Link der stark abgespeckten version
http://www.datei-upload.eu/file.php?id=aad6a5afc163b909d2592d9a3bbb880e
Antwort 19 von rainberg vom 09.09.2020, 14:44 Options
Hallo Martin,
- das Maklro gehört in ein allgemeines Modul und nicht in den Codebereich "DieseArbeitsmappe"
- in Deiner Datei befindet sich mein erstes Makro, richtig wäre das Makro aus Frage 9 mit der Änderung aus Frage 10
- in Deiner Datei befinden sich noch andere Tabellen außer denen, die Du auswerten willst, deshalb ist noch eine weitere Änderung erforderlich.
Füge alle weiteren auszuwertenden Tabellen vor der Tabelle "
Abfrage geliefert" ein und füge vor den auszuwertenden Tabellen
keine weiteren Tabellen ein.
Hier nun das funktionierende Makro.
Option Explicit
Public Sub vergleichen()
Dim lngI As Long, lngN As Long, intWert As Integer
Application.ScreenUpdating = False
For lngN = 2 To Worksheets.Count - 1
For lngI = 1 To Cells(Rows.Count, 1).End(xlUp).Row
intWert = WorksheetFunction.CountIf(Worksheets(lngN).Range("I:I"), _
Worksheets(lngN).Cells(lngI, 7).Value)
If intWert > 0 Then
With Worksheets(lngN)
.Cells(lngI, 6).Font.Strikethrough = True
.Cells(lngI, 7).Font.Strikethrough = True
.Cells(lngI, 8).Font.Strikethrough = True
End With
End If
Next lngI
Next lngN
Application.ScreenUpdating = True
End Sub
Gruß
Rainer
Antwort 20 von Martin123 vom 09.09.2020, 15:27 Options
Hallo Rainer,
werde das morgen mal ausprobieren. Melde mich dann nochmal. Danke im Voraus.
Gruß
Martin