0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

ライフゲーム for ExcelVBA

Posted at

GameOfLife.PNG

Option Explicit

Const ROW_SIZE = 8
Const COL_SIZE = 8

Sub GameOfLife()
    Dim rng As Range, arr
    Set rng = Sheet1.Cells.Resize(ROW_SIZE, COL_SIZE).Offset(1, 1)
    arr = rng.Value
    
    If WorksheetFunction.Sum(arr) = 0 Then Call Init
    
    Dim i, j
    For i = 1 To ROW_SIZE
    For j = 1 To COL_SIZE
        Select Case WorksheetFunction.Sum(Sheet1.Cells(i, j).Resize(3, 3)) - arr(i, j)
        Case 2
        Case 3
            arr(i, j) = 1
        Case Else
            arr(i, j) = 0
        End Select
    Next j, i
    rng.Value = arr
    
    With rng.Resize(1, 1).Offset(-1, rng.Columns.Count + 1)
        .Value = .Value + 1
    End With
    
    Application.OnTime Now + TimeValue("00:00:02"), "GameOfLife"

End Sub

Sub Init()
    Sheet1.Cells.Clear
    With Sheet1.Cells.Resize(ROW_SIZE, COL_SIZE).Offset(1, 1)
        .Formula = "=RANDBETWEEN(0,1)"
        .EntireColumn.AutoFit
        With .FormatConditions.Add(xlCellValue, xlEqual, 0)
            .Interior.ColorIndex = 2
            .Font.ColorIndex = 2
        End With
        With .FormatConditions.Add(xlCellValue, xlEqual, 1)
            .Interior.ColorIndex = 1
            .Font.ColorIndex = 1
        End With
    End With
End Sub
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?