Anzeige
Anzeige
HERBERS
Excel-Forum (Archiv)
20+ Jahre Excel-Kompetenz: Von Anwendern, für Anwender
Anzeige
Inhaltsverzeichnis

Makro Fehler

Forumthread: Makro Fehler

Makro Fehler
16.12.2025 12:00:19
Robert Pilz
Hallo liebe Exeler. Habe hier mein Makro das haut am Ende den Fehler raus. Vielleicht könnt Ihr dabei helfen.


Sub SummenNachBuchstaben_MehrereBlaetter()


Dim BlattNamen As Variant
BlattNamen = Array( _
"Zentral ABG Nord", "Zentral ABG Mitte", "Zentral ABG SO", _
"Zentral L 1 A", "Zentral L 1 B", "Zentral L 2", _
"Zentral L 3", "Zentral L 4", "Zentral L 5", _
"Zentral L 6 Nord", "Zentral L 6 Süd")

Dim ws As Worksheet
Dim dict As Object
Dim r As Long, i As Long
Dim key As String
Dim menge As Double
Dim c As Long
Dim rowAZ As Long

' ================================
' ALLE ANGEGEBENEN BLÄTTER
' ================================
For i = LBound(BlattNamen) To UBound(BlattNamen)

Set ws = Nothing
On Error Resume Next
Set ws = Worksheets(BlattNamen(i))
On Error GoTo 0

If Not ws Is Nothing Then

Set dict = CreateObject("Scripting.Dictionary")

' ----------------------------
' DATEN SAMMELN
' D = Menge
' FL = Buchstabe
' Muster: r und r+1
' ----------------------------
For r = 5 To 104 Step 3

' Zeile r
key = Trim(ws.Cells(r, "FL").value)
menge = Val(ws.Cells(r, "D").value)
If key > "" Then dict(key) = dict(key) + menge

' Zeile r+1
key = Trim(ws.Cells(r + 1, "FL").value)
menge = Val(ws.Cells(r + 1, "D").value)
If key > "" Then dict(key) = dict(key) + menge

Next r

' ----------------------------
' AUSGABE A–Z FEST
' FQ150 = A … FQ175 = Z
' ----------------------------
ws.Range("FQ150:FR175").ClearContents

For c = 65 To 90 ' ASCII A–Z
rowAZ = 150 + (c - 65)
key = Chr(c)

ws.Cells(rowAZ, "FQ").value = key
If dict.exists(key) Then
ws.Cells(rowAZ, "FR").value = dict(key)
Else
ws.Cells(rowAZ, "FR").value = 0

Next c

End If
Next i

MsgBox "Fertig ? Buchstaben A–Z stehen ab FQ150, Werte je Seite korrekt in FR.", vbInformation

End Sub



Fehler beim Kompillieren Next ohne For.

Könnt Ihr helfen.

Danke im voraqus.


LG Robert
Anzeige

9
Beiträge zum Forumthread
Beiträge zu diesem Forumthread

Betreff
Datum
Anwender
Anzeige
AW: Makro Fehler
16.12.2025 12:07:17
Fennek
Hallo,

ganz unten steht das "Next c" im "Else-"Zweig.


mfg
AW: Makro Fehler
16.12.2025 12:09:01
BoskoBiati
Hi,

wenn man vernünftig mit Einrücken arbeitet, dann kann man die Fehler ganz einfach feststellen:

Sub SummenNachBuchstaben_MehrereBlaetter()


Dim BlattNamen As Variant

Dim ws As Worksheet
Dim dict As Object
Dim r As Long, i As Long
Dim key As String
Dim menge As Double
Dim c As Long
Dim rowAZ As Long

BlattNamen = Array( _
"Zentral ABG Nord", "Zentral ABG Mitte", "Zentral ABG SO", _
"Zentral L 1 A", "Zentral L 1 B", "Zentral L 2", _
"Zentral L 3", "Zentral L 4", "Zentral L 5", _
"Zentral L 6 Nord", "Zentral L 6 Süd")

' ================================
' ALLE ANGEGEBENEN BLÄTTER
' ================================
For i = LBound(BlattNamen) To UBound(BlattNamen)

Set ws = Nothing
On Error Resume Next
Set ws = Worksheets(BlattNamen(i))
On Error GoTo 0
If Not ws Is Nothing Then Set dict = CreateObject("Scripting.Dictionary")


For r = 5 To 104 Step 3
key = Trim(ws.Cells(r, "FL").Value)
menge = Val(ws.Cells(r, "D").Value)
If key > "" Then dict(key) = dict(key) + menge
key = Trim(ws.Cells(r + 1, "FL").Value)
menge = Val(ws.Cells(r + 1, "D").Value)
If key > "" Then dict(key) = dict(key) + menge
Next r

' ----------------------------
' AUSGABE A–Z FEST
' FQ150 = A … FQ175 = Z
' ----------------------------
ws.Range("FQ150:FR175").ClearContents

For c = 65 To 90 ' ASCII A–Z
rowAZ = 150 + (c - 65)
key = Chr(c)

ws.Cells(rowAZ, "FQ").Value = key
If dict.exists(key) Then
ws.Cells(rowAZ, "FR").Value = dict(key)
Else
ws.Cells(rowAZ, "FR").Value = 0
End If
Next c


Next i

MsgBox "Fertig ? Buchstaben A–Z stehen ab FQ150, Werte je Seite korrekt in FR.", vbInformation

End Sub


Gruß

Edgar
Anzeige
AW: Makro Fehler
16.12.2025 12:45:23
Alwin Weisangler
Hallo Robert,

code vernünftig lesbar und repariert:


Option Explicit

Sub SummenNachBuchstaben_MehrereBlaetter()
Dim BlattNamen, ws As Worksheet, dict As Object, c&, rowAZ&, r&, i&, menge#, key$
BlattNamen = Array("Zentral ABG Nord", "Zentral ABG Mitte", "Zentral ABG SO", "Zentral L 1 A", "Zentral L 1 B", "Zentral L 2", "Zentral L 3", "Zentral L 4", "Zentral L 5", "Zentral L 6 Nord", "Zentral L 6 Süd")
For i = LBound(BlattNamen) To UBound(BlattNamen)
Set ws = Nothing
On Error Resume Next
Set ws = Worksheets(BlattNamen(i))
On Error GoTo 0
If Not ws Is Nothing Then
Set dict = CreateObject("Scripting.Dictionary")
For r = 5 To 104 Step 3
key = Trim(ws.Cells(r, "FL").Value)
menge = Val(ws.Cells(r, "D").Value)
If key > "" Then dict(key) = dict(key) + menge
key = Trim(ws.Cells(r + 1, "FL").Value)
menge = Val(ws.Cells(r + 1, "D").Value)
If key > "" Then dict(key) = dict(key) + menge
Next r
ws.Range("FQ150:FR175").ClearContents
For c = 65 To 90
rowAZ = 150 + (c - 65)
key = Chr(c)
ws.Cells(rowAZ, "FQ").Value = key
If dict.exists(key) Then
ws.Cells(rowAZ, "FR").Value = dict(key)
Else
ws.Cells(rowAZ, "FR").Value = 0
End If
Next c
End If
Next i
MsgBox "Fertig ? Buchstaben A–Z stehen ab FQ150, Werte je Seite korrekt in FR.", vbInformation
End Sub


Gruß Uwe
Anzeige
AW: Makro Fehler
16.12.2025 14:38:18
snb
Nur
Sub M_snb()

sn = Array("Zentral ABG Nord", "Zentral ABG Mitte", "Zentral ABG SO", "Zentral L 1 A", "Zentral L 1 B", "Zentral L 2", "Zentral L 3", "Zentral L 4", "Zentral L 5", "Zentral L 6 Nord", "Zentral L 6 Süd")
sp = [index(char(row(65:91)),)]

For Each it In Sheets
If UBound(Filter(sn, it.Name)) = 0 Then
sq = sp
st = it.UsedRange
For j = 5 To 104 Step 3
If st(r, 168) > "" Then sq(Asc(st(r, 158)) - 64) = Replace(sq(Asc(st(r, 158)) - 64), st(r, 168), "") + st(j, 5)
Next
it.Range("FQ150:FQ175") = sp
it.Range("FR150:FR175") = sq
End If
Next
End Sub

Anzeige
AW: Makro Fehler
16.12.2025 21:09:35
snb
Etwas besser:
Sub M_snb()

sn = Array("Zentral ABG Nord", "Zentral ABG Mitte", "Zentral ABG SO", "Zentral L 1 A", "Zentral L 1 B", "Zentral L 2", "Zentral L 3", "Zentral L 4", "Zentral L 5", "Zentral L 6 Nord", "Zentral L 6 Süd")
sp = [index(char(row(65:91)),)]
For Each it In Sheets
If UBound(Filter(sn, it.Name)) = 0 Then
sq = sp
st = it.UsedRange
For j = 5 To 104 Step 3
If st(r, 168) > "" Then sq(Asc(st(r, 158)) - 64,1) = Replace(sq(Asc(st(r, 158)) - 64), st(r, 168), "") + st(j, 5)
Next
it.Range("FQ150:FQ175") = sp
it.Range("FR150:FR175") = sq
End If
Next
End Sub

Anzeige
AW: Makro Fehler
16.12.2025 12:25:56
Robert Pilz
Hallo Edgar;

der Fehler bleibt der Gleiche
Next c


End If
Next i
Wie bekomme ichFehler beim Kompliieren Next ohne for weg.

LG Robert
AW: Makro Fehler
16.12.2025 12:34:22
BoskoBiati
Hi,

ich habe Dir einen Code eingestellt, der keinen Fehler bringt!

Gruß

Edgar
Anzeige
AW: Makro Fehler
16.12.2025 12:44:07
Robert Pilz
Hallo Edgar,

so wie ich es geschrieben habe ist die fehlermeldung.
Also wenn du...
16.12.2025 12:49:37
Case
Moin Robert, :-)

... für alle For ein Next hast, dann weist diese Fehlermeldung in der Regel auf ein fehlendes End If hin: ;-)
Option Explicit

Sub SummenNachBuchstaben_MehrereBlaetter()
Dim BlattNamen As Variant
Dim ws As Worksheet
Dim dict As Object
Dim r As Long, i As Long
Dim key As String
Dim menge As Double
Dim c As Long
Dim rowAZ As Long
BlattNamen = Array( _
"Zentral ABG Nord", "Zentral ABG Mitte", "Zentral ABG SO", _
"Zentral L 1 A", "Zentral L 1 B", "Zentral L 2", _
"Zentral L 3", "Zentral L 4", "Zentral L 5", _
"Zentral L 6 Nord", "Zentral L 6 Süd")
For i = LBound(BlattNamen) To UBound(BlattNamen)
Set ws = Nothing
On Error Resume Next
Set ws = Worksheets(BlattNamen(i))
On Error GoTo 0
If Not ws Is Nothing Then
Set dict = CreateObject("Scripting.Dictionary")
For r = 5 To 104 Step 3
key = Trim(ws.Cells(r, "FL").Value)
menge = Val(ws.Cells(r, "D").Value)
If key > "" Then dict(key) = dict(key) + menge
key = Trim(ws.Cells(r + 1, "FL").Value)
menge = Val(ws.Cells(r + 1, "D").Value)
If key > "" Then dict(key) = dict(key) + menge
Next r
ws.Range("FQ150:FR175").ClearContents
For c = 65 To 90 ' ASCII A–Z
rowAZ = 150 + (c - 65)
key = Chr(c)
ws.Cells(rowAZ, "FQ").Value = key
If dict.exists(key) Then
ws.Cells(rowAZ, "FR").Value = dict(key)
Else
ws.Cells(rowAZ, "FR").Value = 0
End If
Next c
End If
Next i
MsgBox "Fertig ? Buchstaben A–Z stehen ab FQ150, Werte je Seite korrekt in FR.", vbInformation
End Sub

Wie von Bosko vorgeschlagen - richtig einrücken. ;-)
Servus
Case
Anzeige
Anzeige
Anzeige
Live-Forum - Die aktuellen Beiträge
Datum
Titel
14.05.2026 13:31:09
14.05.2026 09:50:42
13.05.2026 19:14:18