ジーワンシステム社長のITコラム。
   ITについて、その他もろもろ
>> HOME >> コラム >> 第37回目 「エクセル de バブルブレーカー(Excel de Bubble Breaker)その9」

« 第36回目 「エクセル de バブルブレーカー(Excel de Bubble Breaker)その8」 | メイン | 第38回目 「業界を変えたい」 »

第37回目 「エクセル de バブルブレーカー(Excel de Bubble Breaker)その9」

今回は、配列を使っていないバージョンのソースとなぜ配列を使うのかを簡単に説明します。

エクセル de バブルブレーカー(Excel de Bubble Breaker)のダウンロードはこちらから

Option Explicit
 Public sht設定シート As Worksheet
 Public sht盤面シート As Worksheet
Public rng盤面 As Range
 Private rng開始位置 As Range
Private str開始位置 As String
Private int盤面縦 As Integer
Private int盤面横 As Integer
Private int開始位置縦 As Integer
Private int開始位置横 As Integer
Private rngバブル色範囲 As Range
Private lngバブル色(1 To 5) As Long
Private lng通常背景色 As Long
Private lng選択背景色 As Long
Private Sub 設定値取得()
  Dim i As Integer
  Set sht設定シート = Application.Worksheets("Sheet1") 将来シート名が変わったときここだけを変更する。
  Set sht盤面シート = Application.Worksheets("Sheet1") 将来シート名が変わったときここだけを変更する。
With sht設定シート
   str開始位置 = .Range("開始位置").Value      範囲名があっていれば、将来、どのセルに移動しても変更不要。
   int盤面縦 = .Range( "盤面縦").Value
   int盤面横 = .Range( "盤面横").Value
   Set rngバブル色範囲 = .Range( "通常色")      オブジェクトを代入していることに注意。
   For i = 1 To rngバブル色範囲.Cells.Count
    lngバブル色(i) = rngバブル色範囲.Cells(i).Font.Color
   Next i
   lng通常背景色 = rngバブル色範囲.Interior.Color
   lng選択背景色 = .Range("選択色").Interior.Color
End With
'盤面の範囲を指定する。
 Set rng開始位置 = sht盤面シート.Range(str開始位置)
 Set rng盤面 = sht盤面シート.Range(rng開始位置, rng開始位置.Offset(int盤面縦 - 1, int盤面横 - 1))
 int開始位置縦 = rng開始位置.Row
 int開始位置横 = rng開始位置.Column
End Sub
Public Sub 盤面初期化()
Dim i As Integer
Dim j As Integer
'設定値取得 Subプロシージャを呼び出す
Call 設定値取得
With sht盤面シート.Range(rng開始位置, "IV65536")
  .ColumnWidth = 3               '列幅を変更する
  .Clear                       'クリアーする
End With
sht盤面シート.Range("得点").Value = 0          '得点を 0 に
'With ActiveSheet.Range("F4:X10")
' 指定された大きさで作成する
With rng盤面
   With .Borders(xlEdgeLeft) '以降、F4:X10の左外枠線オブジェクトについて
     .LineStyle = xlContinuous   '線を直線に
     .Weight = xlMedium   '太さを中太線に
     .Color = RGB(0, 0, 0)   '線の色を黒に
   End With '左外枠の操作終了
   With .Borders(xlEdgeTop) '以降、F4:X10の上外枠線オブジェクトについて
     .LineStyle = xlContinuous   '線を直線に
     .Weight = xlMedium   '太さを中太線に
     .Color = RGB(0, 0, 0)   '線の色を黒に
   End With '上外枠の操作終了
   With .Borders(xlEdgeBottom) '以降、F4:X10の下外枠線オブジェクトについて
     .LineStyle = xlContinuous   '線を直線に
     .Weight = xlMedium   '太さを中太線に
     .Color = RGB(0, 0, 0)   '線の色を黒に
   End With '下外枠の操作終了
   With .Borders(xlEdgeRight) '以降、F4:X10の右外枠線オブジェクトについて
     .LineStyle = xlContinuous   '線を直線に
     .Weight = xlMedium   '太さを中太線に
     .Color = RGB(0, 0, 0)   '線の色を黒に
   End With '右外枠の操作終了
   .Value = "●" '"●"と入力セルを指定しなければ範囲内の全てに入力される
   For i = 1 To int盤面縦  
     For j = 1 To int盤面横  
       .Cells(i, j).Interior.Color = lng通常背景色
       .Cells(i, j).Font.Color = lngバブル色(Int(Rnd() * 5) + 1)
     Next j  
   Next i  
 End With  
End Sub  
Public Function checkBreak(ByRef Target As Range) As Boolean
 Dim intV As Integer
 Dim intH As Integer
 intV = Target.Row - int開始位置縦 + 1
 intH = Target.Column - int開始位置横 + 1
 checkBreak = checkBreakSub(intV, intH)
End Function
Private Function checkBreakSub(intV As Integer, intH As Integer) As Boolean
 Dim lngCurrentColor As Long
 Dim i As Integer
 Dim j As Integer
 lngCurrentColor = rng盤面.Cells(intV, intH).Font.Color
 i = intV - 1
 j = intH
 If i >= 1 Then
  If rng盤面.Cells(i, j).Font.Color = lngCurrentColor Then
   Call changeBackColor(lngCurrentColor, intV, intH)
  End If
 End If
 i = intV
 j = intH - 1
 If j >= 1 Then
  If rng盤面.Cells(i, j).Font.Color = lngCurrentColor Then
   Call changeBackColor(lngCurrentColor, intV, intH)
  End If
 End If
 i = intV + 1
 j = intH
 If i <= 30 Then
  If rng盤面.Cells(i, j).Font.Color = lngCurrentColor Then
   Call changeBackColor(lngCurrentColor, intV, intH)
  End If
 End If
 i = intV
 j = intH + 1
 If j <= 200 Then
  If rng盤面.Cells(i, j).Font.Color = lngCurrentColor Then
   Call changeBackColor(lngCurrentColor, intV, intH)
  End If
 End If
End Function
Private Function changeBackColor(lngCurrentColor As Long, intI As Integer, intJ As Integer)
 Dim i As Integer
 Dim j As Integer
 rng盤面.Cells(intI, intJ).Interior.Color = lng選択背景色
 i = intI - 1
 j = intJ
 If i >= 1 Then
  If rng盤面.Cells(i, j).Interior.Color <> lng選択背景色 Then
   If rng盤面.Cells(i, j).Font.Color = lngCurrentColor Then
    Call changeBackColor(lngCurrentColor, i, j)
   End If
  End If
End If
 i = intI
 j = intJ - 1
 If j >= 1 Then
  If rng盤面.Cells(i, j).Interior.Color <> lng選択背景色 Then
   If rng盤面.Cells(i, j).Font.Color = lngCurrentColor Then
    Call changeBackColor(lngCurrentColor, i, j)
   End If
  End If
 End If
 i = intI + 1
 j = intJ
 If i <= 30 Then
  If rng盤面.Cells(i, j).Interior.Color <> lng選択背景色 Then
   If rng盤面.Cells(i, j).Font.Color = lngCurrentColor Then
    Call changeBackColor(lngCurrentColor, i, j)
   End If
  End If
 End If
 i = intI
 j = intJ + 1
 If j <= 200 Then
  If rng盤面.Cells(i, j).Interior.Color <> lng選択背景色 Then
   If rng盤面.Cells(i, j).Font.Color = lngCurrentColor Then
    Call changeBackColor(lngCurrentColor, i, j)
   End If
  End If
 End If
End Function

 配列の時とほぼ変わりはありません。
 変わっている部分といえば配列の時はarrPlayField()という配列を使っていましたが、  こちらではrng盤面.Cells()としています。
 ではこれで何が変わってくるかといいますと、処理速度です。
 CellsもarrPlayFieldもオンメモリ処理なのですが、CellsプロパティではどうしてもExcelによる評価が発生します。
 ですから、メモリ上の配列arrPlayFieldに直接アクセスする方が処理速度が速いというわけです。
 arrPlayFieldという配列をわざわざつくっているのはその為です。
 今回のように要素数が多くない場合はそれほど速度の変化は感じないと思いますが、  何十万、何百万と要素数が膨大な場合には処理速度の要素数が膨大になってくると速度は変わってきます。
 覚えておくと便利なのでこれを機会に配列を使う方が良いことを覚えておきましょう。

解説で利用したエクセルファイルのダウンロード

<< 第36回目 「エクセル de バブルブレーカー(Excel de Bubble Breaker)その8」 | | 第38回目 「業界を変えたい」 >>

>> HOME >> コラム >> 第37回目 「エクセル de バブルブレーカー(Excel de Bubble Breaker)その9」

トラックバック

このエントリーのトラックバックURL:
http://www.g1sys.co.jp/cgi-bin/app/mt-tb.cgi/55

コメントを投稿

(いままで、ここでコメントしたことがないときは、コメントを表示する前にこのブログのオーナーの承認が必要になることがあります。承認されるまではコメントは表示されません。そのときはしばらく待ってください。)

  • 製品案内
システム構築・運用、技術サポート・ビジネスブログ・ホームページ制作の株式会社ジーワンシステム
株式会社ジーワンシステム

〒550-0014
大阪市西区北堀江1-5-2
四ツ橋新興産ビル 11F

TEL: 06-6535-8660
FAX: 06-6535-8661

E-mail: info@g1sys.co.jp

Powered by
Movable Type 3.34