Code Kürzen evt. mit Schleife !

Leider komme ich nicht darum einen langen Code einzusetzen.
Gäbe es eine Möglichkeit ausser Case Select um ihn zu kürzen ?

Mir ist nichts besseres eingefallen als If mit ElseIf, das Ding
würde sich 10mal wiederholen. Aber irgenwie sehe ich eine Schleife
i = 1 to 10 komme aber nicht drauf :wink:

Dim nrow As Long
Dim ncount As Long

If Cells(15, 5).End(xlDown) = „Golf“ Then

For nrow = 1 To Cells(Rows.count, 5).End(xlUp).row
If Rows(nrow).Hidden = False Then
If Range(„E“ & nrow).Value = „Golf“ Then ncount = ncount + 1
End If
Next nrow

ElseIf Cells(15, 5).End(xlDown) = „Audi“ Then

For nrow = 1 To Cells(Rows.count, 5).End(xlUp).row
If Rows(nrow).Hidden = False Then
If Range(„E“ & nrow).Value = „Audi“ Then ncount = ncount + 1
End If
Next nrow

ElseIf Cells(15, 5).End(xlDown) = „BMW“ Then

For nrow = 1 To Cells(Rows.count, 5).End(xlUp).row
If Rows(nrow).Hidden = False Then
If Range(„E“ & nrow).Value = „BMW“ Then ncount = ncount + 1
End If
Next nrow

End If

Range(„B12“).Value = ncount ’ Ausgabe von ncount

end sub

Hallo!

Probiere einmal diesen Code aus:

Dim Autos() As String
Dim stAuto As String
Dim ncount As Long
Autos = Array("Golf", "Audi", "BMW")

For Each stAuto In Autos
 If Cells(15, 5).End(xlDown) = stAuto Then
 Dim nrow As Long
 For nrow = 1 To Cells(Rows.Count, 5).End(xlUp).Row
 If Rows(nrow).Hidden = False Then
 If Range("E" & nrow).Value = stAuto Then
 ncount = ncount + 1
 End If
 End If
 Next nrow
 End If
Next stAuto

Range("B12").Value = ncount

mfg
christoph

Hallo Christoph

Danke für den Code.
Als String deklariert (Autos & stAuto) wollte das makro nicht laufen, mit Variant lief es dann.

Gruss
Flexo

Hallo!

Als String deklariert (Autos & stAuto) wollte das makro nicht
laufen, mit Variant lief es dann.

Ja, stimmt. Ich habe den Code zuerst in einem Editor geschrieben und dann erst zum Syntax-Check nach Excel kopiert. Da hat er mir dann auch den genannten Fehler ausgespuckt. Den Programmcode, den ich dir dann hierher gestellt habe, habe ich dann aber aus meinem Editor kopiert.

mfg
christoph