Moin Moin,
ich bin auf das
"Game of Life"
von Conway gestoßen und wollte dieses gerne in EXCEL mittels VBA nachbauen. Bisher habe ich den Code im Anhang geschrieben.
Mein derzeitiges Problem ist der Laufzeitfehler 1004. Wenn ich in der
Sub
ScreenCells die Funktion CountNeighbours aufrufe, bekomme ich diesen Fehler.
Ich kann leider nicht herausfinden woran dieses genau liegt. Wenn ich in der
Sub
ScreenCells der Variable intNeighbours einen
Integer
zuweise, dann funktioniert es wie es soll.
Was ist mein Fehler?
Danke vielmals!
VG
LBE
Option
Explicit
Sub
Life()
Call
ScreenCells
End
Sub
Function
CountNeighbours(rZelle
As
Range)
As
Integer
Dim
iCounter
As
Integer
iCounter = 0
If
(rZelle.Offset(-1, -1).Interior.ColorIndex = 1)
Then
iCounter = iCounter + 1
End
If
If
(rZelle.Offset(-1, 0).Interior.ColorIndex = 1)
Then
iCounter = iCounter + 1
End
If
If
(rZelle.Offset(-1, 1).Interior.ColorIndex = 1)
Then
iCounter = iCounter + 1
End
If
If
(rZelle.Offset(0, -1).Interior.ColorIndex = 1)
Then
iCounter = iCounter + 1
End
If
If
(rZelle.Offset(0, 1).Interior.ColorIndex = 1)
Then
iCounter = iCounter + 1
End
If
If
(rZelle.Offset(1, 0).Interior.ColorIndex = 1)
Then
iCounter = iCounter + 1
End
If
If
(rZelle.Offset(-1, -1).Interior.ColorIndex = 1)
Then
iCounter = iCounter + 1
End
If
If
(rZelle.Offset(-1, -1).Interior.ColorIndex = 1)
Then
iCounter = iCounter + 1
End
If
CountNeighbours = iCounter
End
Function
Sub
ScreenCells()
Dim
Zelle
As
Object
Dim
intNachbarn
As
Integer
For
Each
Zelle
In
Worksheets(1).Range(
"A1:Z22"
)
intNachbarn = CountNeighbours(Zelle)
If
(((intNachbarn < 2)
Or
(intNachbarn > 3))
And
(Zelle.Interior.ColorIndex = 1))
Then
Worksheets(2).Cells(Zelle.Row, Zelle.Column).Interior.ColorIndex = 2
End
If
If
(((intNachbarn = 2)
Or
(intNachbarn = 3))
And
(Zelle.Interior.ColorIndex = 1))
Then
Worksheets(2).Cells(Zelle.Row, Zelle.Column).Interior.ColorIndex = 1
End
If
If
(intNachbarn = 3
And
Zelle.Interior.ColorIndex = 2)
Then
Worksheets(2).Cells(Zelle.Row, Zelle.Column).Interior.ColorIndex = 1
End
If
Next
End
Sub