先日は、あみだくじを一斉にスタートさせることで、結果が出るまでの待ち時間を短縮してみた。
infoment.hatenablog.com
本日は、阿弥陀編の最終回(と、「火の鳥」っぽい題名を付けてみた)。
といっても、行ったのは以下の二つ。
- クラスモジュール化(ワンパターン?)
- 梯子部分を辿る時、名前も一緒に表示させる。
クラスモジュール部分は、それなりにボリュームがある。
なので、↓ こちらに畳んで置いておく。
クラスモジュール(AmidaClass)
Option Explicit
Public NameRange As Range
Public StartRange As Range
Public WinnersNumber As Long
Private myTintAndShade As Double
Private Sub SetEntry()
StartRange.Resize(, UBound(NameArray)) = WorksheetFunction.Transpose(NameArray)
End Sub
Private Sub MakeLadderLine()
Dim BordersIndex As Variant
For Each BordersIndex In Array(xlEdgeRight, xlInsideVertical)
LadderRange.Borders(BordersIndex).LineStyle = xlContinuous
Next
Dim r As Range
For Each r In LadderRange
If LineCriteria(LadderRange, r) Then
Dim rndIndex As Long
rndIndex = WorksheetFunction.RandBetween(0, 1)
r.Borders.Item(xlEdgeBottom).LineStyle = (xlContinuous - xlNone) * rndIndex + xlNone
End If
Next
End Sub
Private Function LineCriteria(all_range As Range, target_range As Range) As Boolean
If target_range.Column = all_range(1).Column Then
ElseIf all_range(1).Row + all_range.Rows.Count - 1 = target_range.Row Then
ElseIf target_range.Borders.Item(xlEdgeTop).LineStyle <> xlNone Then
ElseIf target_range.Offset(, -1).Borders.Item(xlEdgeBottom).LineStyle = xlNone Then
LineCriteria = True
End If
End Function
Private Function GetNextRange(source_range As Range, source_color As Long) As Range
With source_range.Item(1)
If .Borders(xlEdgeRight).LineStyle = xlNone Then
Set GetNextRange = Nothing
.Interior.Color = source_color
.Interior.TintAndShade = myTintAndShade
Exit Function
End If
End With
source_range.Item(1).Interior.Color = source_color
source_range.Item(1).Interior.TintAndShade = myTintAndShade
With source_range.Item(1).Borders.Item(xlEdgeRight)
.Color = source_color
.Weight = xlMedium
.LineStyle = xlDouble
End With
Dim Border_Bottom(1 To 2) As Border
Dim i As Long
For i = 1 To UBound(Border_Bottom)
Set Border_Bottom(i) = source_range.Item(i).Borders(xlEdgeBottom)
Next
Dim dx As Long: dx = 0
For i = 1 To 2
If Border_Bottom(i).LineStyle <> xlNone Then
dx = 2 * i - 3
Border_Bottom(i).Color = source_color
Border_Bottom(i).Weight = xlMedium
Border_Bottom(i).LineStyle = xlDouble
Exit For
End If
Next
Set GetNextRange = source_range.Offset(1, dx)
End Function
Private Sub Solution()
Dim myRng() As Range
ReDim myRng(1 To LadderRange.Columns.Count)
Dim PersonColor() As Long
ReDim PersonColor(1 To UBound(myRng))
Dim i As Long
For i = 1 To UBound(myRng)
Set myRng(i) = LadderRange(i).Resize(2, 2)
PersonColor(i) = WorksheetFunction.RandBetween(150 ^ 3, 255 ^ 3)
myRng(i).Cells(0, 1).Interior.Color = PersonColor(i)
myRng(i).Cells(0, 1).Interior.TintAndShade = myTintAndShade
Next i
Dim counter As Long: counter = 1
Do Until myRng(1) Is Nothing
If counter >= 2 Then
LadderRange.Rows(counter - 1).Interior.Color = xlNone
LadderRange.Rows(counter - 1).Value = vbNullString
End If
For i = 1 To UBound(myRng)
On Error Resume Next
myRng(i).Item(1) = NameArray(i, 1)
Set myRng(i) = GetNextRange(myRng(i), PersonColor(i))
Next
counter = counter + 1
Application.Wait [Now() + "00:00:00.5"]
Loop
End Sub
Private Sub WinnerSet()
Dim LastRow As Range
Set LastRow = LadderRange.Rows(LadderRange.Rows.Count).Offset(2)
Dim r As Range
Do
For Each r In LastRow.Cells
r = WorksheetFunction.RandBetween(0, 1)
Next
Loop Until WorksheetFunction.Sum(LastRow) = WinnersNumber
LastRow.Replace 0, vbNullString
LastRow.Replace 1, "当選"
End Sub
Public Sub StartAmida()
SetEntry
MakeLadderLine
WinnerSet
Solution
End Sub
Private Property Get NameArray() As Variant
Dim arr() As Variant
arr = NameRange.Value
NameArray = arr
End Property
Private Property Get LadderRange() As Range
Set LadderRange = StartRange.Offset(1).Resize(UBound(NameArray) * 2, UBound(NameArray))
End Property
Private Sub Class_Initialize()
myTintAndShade = 0.6
End Sub
標準モジュール
毎度のごとく、クラスモジュールに殆ど渡してしまったため、こちらは実にアッサリしている。
Sub test()
Dim AC As AmidaClass
Set AC = New AmidaClass
Set AC.NameRange = Range("A5:A11")
AC.WinnersNumber = Range("B2")
Set AC.StartRange = Range("D5")
AC.StartAmida
End Sub
結果はこちら。
名前も一緒に降りてくることで、だいぶん目で追い易くなった気がする。
ということで、今回のシリーズはこれでおしまい。
ネタを提供してくださったコロ子さん、ありがとうございます。
参考まで。