2023-01-27に更新

【実況ブログ】 Excel VBAで初めてのプログラミングを覚えようぜ(^~^)?

読了目安:39分

📅2023-01-25 mon 19:15 start

ramen-tabero-futsu2.png
「 Excel を購入しろだぜ」

kifuwarabe-futsu.png
「 してるぜ」

202301_excel_25-1920--sheet.png

ramen-tabero-futsu2.png
「 👆 これを使って、プログラミングを覚えてもらう」

ohkina-hiyoko-futsu2.png
「 現代の Excel は チューリング完全らしいですしね」

kifuwarabe-futsu.png
「 じゃあ Excel で チューリング・マシンを作るのか?
無限のテープをどう表現する?」

ramen-tabero-futsu2.png
「 テープが足りなくなったら 強制終了すればいいだろ」

📖 Turing machine

ramen-tabero-futsu2.png
「 👆 例題は Wikipedia を参考にしようぜ?」

202301_excel_25-2002--StateTable.png

[TuringMachineByExcelVBA.xlsm] file - [StateTable] sheet:

State,Read,Write,Move,Transition
A,White,Orange,>,B
A,Orange,Orange,<,C
B,White,Orange,<,A
B,Orange,Orange,>,B
C,White,Orange,<,B
C,Orange,Orange,>,HALT

ramen-tabero-futsu2.png
「 👆 とりあえず StateMachine シートを作れだぜ。
セルに色も塗れだぜ」

ohkina-hiyoko-futsu2.png
「 このシートが何かの説明はしないの?」

ramen-tabero-futsu2.png
「 手を動かせば あとで分かる」

202301_excel_25-2008--Tape.png

[TuringMachineByExcelVBA.xlsm] file - [Tape] sheet:

A

ramen-tabero-futsu2.png
「 👆 もう1つ、 Tape シートを作れだぜ。
A の1文字だけ入っている」

kifuwarabe-futsu.png
「 これは何なんだぜ?」

ramen-tabero-futsu2.png
「 あとで分かる」

202301_excel_25-2014--OpenCode-1.png

ramen-tabero-futsu2.png
「 空っぽの GUI というシートを作って、
メインメニューから [開発] - [コードの表示] を選べだぜ」

202301_excel_25-2017--VBAEditor.png

ramen-tabero-futsu2.png
「 👆 VBA のエディターが出てくるな」

ohkina-hiyoko-futsu2.png
「 あの 不便なやつね」

ramen-tabero-futsu2.png
「 ほんと 不便だな。 シートに戻るぜ」

202301_excel_25-2021--Button-1.png

ramen-tabero-futsu2.png
「 👆 先に ボタンを置こうぜ?」

202301_excel_25-2024--PutButton-1.png

ramen-tabero-futsu2.png
「 👆 何だか よくわからないが ボタン1_Click という名前はそのまんまで
マクロの保存先を 今作業中のファイルに変えて、 [新規作成(N)] ボタンを押そうぜ?」

kifuwarabe-futsu.png
「 Excel の使い方を記憶してないの わらう」

ohkina-hiyoko-futsu2.png
「 プログラマーは 記憶 ではなく、 読み で進むのよ。
その方が 応用が利くから」

202301_excel_25-2029--CreateButton-1.png

ramen-tabero-futsu2.png
「 👆 ボタンがでけてる。
当たった」

kifuwarabe-futsu.png
「 当たった とか 講師から出てきたらおかしい言葉 わらう」

202301_excel_25-2032--RegisterMacro-1.png

ramen-tabero-futsu2.png
「 👆 ボタンを右クリックして コンテキスト・メニューの [マクロの登録(N)]
クリックしてみようぜ?」

202301_excel_25-2035--Ok-1.png

ramen-tabero-futsu2.png
「 👆 何をすればいいのか分からん。 [OK] ボタンを押してみようぜ?」

202301_excel_25-2037--Code.png

ramen-tabero-futsu2.png
「 👆 ?
何が起こった?」

202301_excel_25-2038--Skeleton-1.png

kifuwarabe-futsu.png
「 👆 なんか コードが増えてるのでは?」

ramen-tabero-futsu2.png
「 よっしゃ!
じゃあ そこに VBA Script (ぶい・びー・えー・すくりぷと)を書けばいいんだぜ」

📅 2023-01-25 wed 20:41

ramen-tabero-futsu2.png
「 🔍 VBA セルに値を入れる で検索」

📖 セルに値を入れる:Excel VBA プログラミング入門

ramen-tabero-futsu2.png
「 👆 よし 分かったぜ」

202301_excel_25-2046--HelloWorld-1.png

Worksheets("GUI").Range("A1").Value = "Hello, world!!"

ramen-tabero-futsu2.png
「 👆 こんな感じに書けば GUI シートの A1 セルに Hello, world!! という文字を
入れてくれそうだな」

ramen-tabero-futsu2.png
「 ボタンを押そうぜ?」

202301_excel_25-2049--ShowHelloWorld-1.png

ramen-tabero-futsu2.png
「 👆 ほら 出た」

kifuwarabe-futsu.png
「 Excelでハローワールドを出力する の実績を解除したな」

ramen-tabero-futsu2.png
「 じゃあ次。
🔍 VBA セルに色を付ける で検索」

📖 セルに色を設定する

ramen-tabero-futsu2.png
「 👆 よし 分かったぜ」

202301_excel_25-2056--BackgroundColor-1.png

Worksheets("GUI").Range("A1").Interior.ColorIndex = 45 ' オレンジ

ramen-tabero-futsu2.png
「 👆 こんな感じに書けば GUI シートの A1 セルの背景色をオレンジ色に
してくれそうだな」

ramen-tabero-futsu2.png
「 ボタンを押そうぜ?」

202301_excel_25-2057--ShowBackgroundColor-1.png

ramen-tabero-futsu2.png
「 👆 A1 セルに色が付いたな」

ohkina-hiyoko-futsu2.png
「 着色の自動化に使えそうねえ」

202301_excel_25-2108--GetColor-1.png

ramen-tabero-futsu2.png
「 👆 じゃあ、 StateTable シートの C2 セルの背景色が何色かとか、取得することはできるのかだぜ?」

ohkina-hiyoko-futsu2.png
「 ググりゃいいんじゃないの?」

ramen-tabero-futsu2.png
「 🔍 VBA セルの色を取得 で検索」

📖 VBA セルの色を取得する (Interior.Color, ColorIndex)

ramen-tabero-futsu2.png
「 👆 よし 分かったぜ」

202301_excel_25-2117--CopyColor-1.png

    Dim backgroundColor As Long
    backgroundColor = Worksheets("StateTable").Range("C2").Interior.color ' 背景色
    Debug.Print (backgroundColor)

    Worksheets("GUI").Range("A1").Interior.color = backgroundColor

ramen-tabero-futsu2.png
「 👆 こう書けば StateTable シートの C2 セルの背景色を、 GUI シートの A1 セルへ コピーできるはずだぜ!」

ramen-tabero-futsu2.png
「 ボタンを押そうぜ?」

202301_excel_25-2120--ExecuteCopyColor-1.png

ramen-tabero-futsu2.png
「 👆 背景色は コピーでけたが……。
Debug.Print( ... ) って何だぜ?」

ohkina-hiyoko-futsu2.png
「 ググりゃいいんじゃないの?」

ramen-tabero-futsu2.png
「 🔍 VBA デバッグプリント で検索」

📖 【エクセルVBA】初心者のうちから知っておくべきDebug.Printの使い方

ramen-tabero-futsu2.png
「 👆 イミディエイト・ウィンドウ に値を表示するらしいぜ」

ramen-tabero-futsu2.png
「 イミディエイト・ウィンドウ って何だぜ?」

ohkina-hiyoko-futsu2.png
「 ググりゃいいんじゃないの?」

ramen-tabero-futsu2.png
「 🔍 VBA イミディエイトウィンドウ で検索」

📖 イミディエイトウィンドウの使い方

ramen-tabero-futsu2.png
「 👆 よし 分かったぜ」

202301_excel_25-2125--Immediate-1.png

ramen-tabero-futsu2.png
「 [Ctrl] + [G] キーを打鍵すると 出てくるウィンドウだぜ」

kifuwarabe-futsu.png
「 そんなウィンドウの出し方、 画面のどこを探しても 無くね?」

ohkina-hiyoko-futsu2.png
「 コンピューター開発者は 私には分かる。だからお前も分かるだろ という脳をしてる人 多いのよ。
伝え、継承する精神を持っている人は リタイア組ぐらいよ」

ramen-tabero-futsu2.png
「 天才が 技術を継承するの 損だしな。
ネット上で 記事書いてるの リタイア組か、 業界が滅ぶ一歩手前で しかたなく 天才のケツ掃除してる人たちの どっちかだよな」

ramen-tabero-futsu2.png
「 しかし 49407 なんて数字出てきても 嬉しくないな」

📅 2023-01-25 wed 21:36

ramen-tabero-futsu2.png
「 じゃあ 次は長めの コンボ(Combo;連続技) やるから よく聴けだぜ」

202301_excel_25-2140--a1-1.png

ramen-tabero-futsu2.png
「 👆 Tape シートの A1 セルに入っている値 A と、その背景色 白色 を取得して……」

202301_excel_25-2142--seek-1.png

ramen-tabero-futsu2.png
「 👆 StateTable シートの State 列に A が、
Read 列に 背景色が白色のセルが
無いかなと探し……」

202301_excel_25-2142--seek-2.png

ramen-tabero-futsu2.png
「 👆 見つけたら……」

202301_excel_25-2142--seek-3.png

ramen-tabero-futsu2.png
「 👆 そのまま 横へスライドし、
Write列 の背景色は オレンジ色、
Move列 は >
Transition列 は B
と いったん覚え……」

202301_excel_25-2149--output-1.png

ramen-tabero-futsu2.png
「 👆 Tape シートを開き、
A1 セルから 1行下にいったところを、 Write 列にあるように オレンジ色 に塗り、
Move列が > とあるように その右側に対して、
Transition列があるように B を書き込もうぜ?」

kifuwarabe-futsu.png
「 長い」

ramen-tabero-futsu2.png
「 いったん TuringMachineByExcelVBA.xlsm ファイルを保存して閉じるぜ。
休憩だぜ」

📅 2023-01-25 wed 22:10 stop

📅 2023-01-25 wed 22:21 restart

ramen-tabero-futsu2.png
「 よっしゃ 再開だぜ!
TuringMachineByExcelVBA.xlsm ファイルを開けて、っと」

202301_excel_25-2222--clear.png

ramen-tabero-futsu2.png
「 👆 消えてる!」

kifuwarabe-futsu.png
「 よく探せだぜ!」

202301_excel_25-2224--project-window-1.png

ramen-tabero-futsu2.png
「 👆 無い、無い、無い、
どこにも 無~い!」

ohkina-hiyoko-futsu2.png
「 ファイルを開けるには、ダブル・クリック するんじゃないの?」

202301_excel_25-2226--double-click.png

ramen-tabero-futsu2.png
「 👆 あ、 有った」

ohkina-hiyoko-futsu2.png
「 じゃあ Tape シートの A1 セルの値 A と背景色 白色 を取得して、
そのタプル(Tuple;組み)が StateTable シートの何行目にあるか探し出して
イミディエイト・ウィンドウに デバッグプリント するところまで やりましょう!」

202301_excel_25-2230--getValue-1.png

Sub ボタン1_Click()
    Dim text As String
    Dim backgroundColor As Long

    text = Worksheets("Tape").Range("A1").Value ' セルの値
    backgroundColor = Worksheets("Tape").Range("A1").Interior.color ' 背景色

    Debug.Print (text)
    Debug.Print (backgroundColor)

    Worksheets("GUI").Range("A1").Value = text
    Worksheets("GUI").Range("A1").Interior.color = backgroundColor
End Sub

ramen-tabero-futsu2.png
「 👆 値と 背景色のコピーは できるようになったが、
次は 探すというやつだな」

ramen-tabero-futsu2.png
「 🔍 VBA For文 で検索」

📖 [Excel で VBA] For 文による繰り返し

ramen-tabero-futsu2.png
「 👆 よし 分かったぜ」

ohkina-hiyoko-futsu2.png
「 For文 が何かの説明は しないのね」

ramen-tabero-futsu2.png
「 手を動かせば 分かるぜ」

202301_excel_25-2240--loop-1.png

ramen-tabero-futsu2.png
「 👆 多分 StateTable シートを 1行目から 7行目まで読むのは こんな雰囲気だろ。
VBA の if 文ってどうやって書くんだったかな?」

kifuwarabe-futsu.png
「 記憶してないの わらう」

ramen-tabero-futsu2.png
「 🔍 VBA if文 で検索」

📖 ExcelのVBA(マクロ)でIf~Then~Elseを使って条件分岐する方法

ramen-tabero-futsu2.png
「 👆 よし 分かったぜ」

202301_excel_25-2249--if-then-else-1.png

ramen-tabero-futsu2.png
「 👆 多分 if文は こんな雰囲気だろ。
Forループを途中で抜けるの VBAで どうやって書くんだったかな?」

ohkina-hiyoko-futsu2.png
「 ググりゃいいんじゃないの?」

kifuwarabe-futsu.png
「 なんにも覚えてないんだな」

ramen-tabero-futsu2.png
「 🔍 VBA break文 で検索」

📖 Excel VBAでFor文を途中で抜ける:Exit

ramen-tabero-futsu2.png
「 👆 よし 分かったぜ」

Sub ボタン1_Click()
    Dim text As String
    Dim backgroundColor As Long

    text = Worksheets("Tape").Range("A1").Value ' セルの値
    backgroundColor = Worksheets("Tape").Range("A1").Interior.color ' 背景色

    Debug.Print (text)
    Debug.Print (backgroundColor)

    Worksheets("GUI").Range("A1").Value = text
    Worksheets("GUI").Range("A1").Interior.color = backgroundColor

    Dim i As Long
    Dim stateText As String
    Dim readBackgroundColor As Long
    Dim writeBackgroundColor As Long
    Dim moveText As String
    Dim transitionText As String

    For i = 2 To 7
        stateText = Worksheets("StateTable").Range("A" & i).Value ' セルの値
        readBackgroundColor = Worksheets("StateTable").Range("B" & i).Interior.color ' 背景色

        ' 一致するか?
        If text = stateText And backgroundColor = readBackgroundColor Then
            writeBackgroundColor = Worksheets("StateTable").Range("C" & i).Interior.color ' 背景色
            moveText = Worksheets("StateTable").Range("D" & i).Value ' セルの値
            transitionText = Worksheets("StateTable").Range("E" & i).Value ' セルの値

            Debug.Print (writeBackgroundColor)
            Debug.Print (moveText)
            Debug.Print (transitionText)

            ' TODO 次の処理へ

            Exit For
        End If
    Next i
End Sub

202301_excel_25-2257--find-1.png

ramen-tabero-futsu2.png
「 👆 取れてるぜ」

📅 2023-01-25 wed 23:00

ohkina-hiyoko-futsu2.png
「 次は、
Tape シートの A1 セルを スタート地点として、
1行 下りたセルの背景色を Write列のいう色に塗って、そこから
Move 列が > だったら その右のセルへ、 Transition 列のいうテキストを入れましょう」

202301_excel_25-2310--write-1.png

ramen-tabero-futsu2.png
「 👆 こんな感じだろ」

202301_excel_25-2314--tape.png

ramen-tabero-futsu2.png
「 👆 Tape シートの2行目を クリアーしておくぜ。
そして GUI シートのボタンを押すぜ」

202301_excel_25-2316--result-1.png

ramen-tabero-futsu2.png
「 👆 色も文字もコピーされたぜ」

kifuwarabe-futsu.png
「 おつ。
長いコンボが決まったな」

ohkina-hiyoko-futsu2.png
「 これで 1クロック よね」

kifuwarabe-futsu.png
「 何だぜ それ」

ramen-tabero-futsu2.png
「 次は 2クロック目に 行ってみようぜ?」

📅 2023-01-25 wed 23:21

202301_excel_25-2321--2th-clock-1.png

ramen-tabero-futsu2.png
「 👆 B2 セルをスタート地点として、 同様に さっきと同じことを やればいいんだぜ」

kifuwarabe-futsu.png
「 嫌だぜ なんでそんなことをするんだぜ?」

ohkina-hiyoko-futsu2.png
「 ラジオ体操 みたいなもんよ。 頭を ほぐしてんのよ」

202301_excel_25-2325--b-white-1.png

ramen-tabero-futsu2.png
「 👆 文字が B で、背景色が 白色 なのは 4行目だな。
下にオレンジ塗って 左へ A を書けばよさそうだな」

202301_excel_25-2337--time-1.png

ramen-tabero-futsu2.png
「 👆 説明するのを忘れていたが、上の行の背景色を、下の行へ 引き継ぐぜ」

kifuwarabe-futsu.png
「 忘れないでくれだぜ」

202301_excel_25-2340--time-b-1.png

ramen-tabero-futsu2.png
「 👆 下にオレンジ塗るぜ」

202301_excel_25-2346--time-c-1.png

ramen-tabero-futsu2.png
「 👆 左に A を書いたら こんな感じだな」

202301_excel_25-2348--Code.png

Sub ボタン1_Click()
    Dim text As String
    Dim backgroundColor As Long
    Dim i As Long
    Dim stateText As String
    Dim readBackgroundColor As Long
    Dim writeBackgroundColor As Long
    Dim moveText As String
    Dim transitionText As String

    ' 1回目の処理
    text = Worksheets("Tape").Range("A1").Value ' セルの値
    backgroundColor = Worksheets("Tape").Range("A1").Interior.color ' 背景色

    For i = 2 To 7
        stateText = Worksheets("StateTable").Range("A" & i).Value ' セルの値
        readBackgroundColor = Worksheets("StateTable").Range("B" & i).Interior.color ' 背景色

        ' 一致するか?
        If text = stateText And backgroundColor = readBackgroundColor Then
            writeBackgroundColor = Worksheets("StateTable").Range("C" & i).Interior.color ' 背景色
            moveText = Worksheets("StateTable").Range("D" & i).Value ' セルの値
            transitionText = Worksheets("StateTable").Range("E" & i).Value ' セルの値

            ' `Tape` シートの A1 セルの下のセルの背景色を Write列のいう色に塗る
            Worksheets("Tape").Range("A2").Interior.color = writeBackgroundColor

            ' Move 列が `>` だったら その右のセルへ、 Transition 列のいうテキストを入れる
            If moveText = ">" Then
                Worksheets("Tape").Range("B2").Value = transitionText
            End If

            Exit For
        End If
    Next i

    ' TODO ★ 同様の2回目の処理
    text = Worksheets("Tape").Range("B2").Value ' セルの値
    backgroundColor = Worksheets("Tape").Range("B2").Interior.color ' 背景色

    ' ★ 上の行の背景色は引き継ぐ
    Worksheets("Tape").Range("A3").Interior.color = Worksheets("Tape").Range("A2").Interior.color
    Worksheets("Tape").Range("B3").Interior.color = Worksheets("Tape").Range("B3").Interior.color

    For i = 2 To 7
        stateText = Worksheets("StateTable").Range("A" & i).Value ' セルの値
        readBackgroundColor = Worksheets("StateTable").Range("B" & i).Interior.color ' 背景色

        ' 一致するか?
        If text = stateText And backgroundColor = readBackgroundColor Then
            writeBackgroundColor = Worksheets("StateTable").Range("C" & i).Interior.color ' 背景色
            moveText = Worksheets("StateTable").Range("D" & i).Value ' セルの値
            transitionText = Worksheets("StateTable").Range("E" & i).Value ' セルの値

            ' `Tape` シートの A1 セルの下のセルの背景色を Write列のいう色に塗る
            Worksheets("Tape").Range("B3").Interior.color = writeBackgroundColor

            ' ★ Move 列が `<` だったら その左のセルへ、 Transition 列のいうテキストを入れる
            If moveText = "<" Then
                Worksheets("Tape").Range("A3").Value = transitionText
            End If

            Exit For
        End If
    Next i

End Sub

ramen-tabero-futsu2.png
「 👆 それを コードにしたら こんな感じだぜ」

kifuwarabe-futsu.png
「 長い」

ohkina-hiyoko-futsu2.png
「 これで 2クロック よね」

kifuwarabe-futsu.png
「 何だぜ それ」

ramen-tabero-futsu2.png
「 次は 3クロック目に 行ってみようぜ?」

kifuwarabe-futsu.png
「 嫌だぜ」

ramen-tabero-futsu2.png
「 今日は ここまでとするが、 3クロック目 行くからな」

📅 2023-01-25 wed 23:51 end

📅2023-01-26 thu 18:53 start

ramen-tabero-futsu2.png
「 VBA でサブルーチンは どうやって書いたらいいんだぜ?」

ohkina-hiyoko-futsu2.png
「 ググりゃいいんじゃないの?」

ramen-tabero-futsu2.png
「 🔍 VBA サブルーチン で検索」

📖 Excel VBA 処理の一部をサブルーチン化するCallステートメント

ramen-tabero-futsu2.png
「 👆 よし 分かったぜ」

202301_excel_26-1901--Subroutine-1.png

ramen-tabero-futsu2.png
「 👆 スケルトン(Skeleton;穴埋めの穴じゃない方)を書こうぜ」

202301_excel_26-1906--MoveCode-1.png

ramen-tabero-futsu2.png
「 👆 スケルトンの中へ コードを こうやって 入れたらいいんじゃないかだぜ?」

202301_excel_26-1911--MovedCode.png

ramen-tabero-futsu2.png
「 👆 つまり こう」

202301_excel_26-1912--Call-1.png

ramen-tabero-futsu2.png
「 👆 移動した跡の所には コール文(Call Statement)を置いておこうぜ?」

Sub ボタン1_Click()

    ' 1回目の処理
    Call On1stClock

    ' 同様の2回目の処理
    Call On2ndClock

End Sub

Private Sub On1stClock()
    ' 1回目のクロック
    Dim text As String
    Dim backgroundColor As Long
    Dim i As Long
    Dim stateText As String
    Dim readBackgroundColor As Long
    Dim writeBackgroundColor As Long
    Dim moveText As String
    Dim transitionText As String

    text = Worksheets("Tape").Range("A1").Value ' セルの値
    backgroundColor = Worksheets("Tape").Range("A1").Interior.color ' 背景色

    For i = 2 To 7
        stateText = Worksheets("StateTable").Range("A" & i).Value ' セルの値
        readBackgroundColor = Worksheets("StateTable").Range("B" & i).Interior.color ' 背景色

        ' 一致するか?
        If text = stateText And backgroundColor = readBackgroundColor Then
            writeBackgroundColor = Worksheets("StateTable").Range("C" & i).Interior.color ' 背景色
            moveText = Worksheets("StateTable").Range("D" & i).Value ' セルの値
            transitionText = Worksheets("StateTable").Range("E" & i).Value ' セルの値

            ' `Tape` シートの A1 セルの下のセルの背景色を Write列のいう色に塗る
            Worksheets("Tape").Range("A2").Interior.color = writeBackgroundColor

            ' Move 列が `>` だったら その右のセルへ、 Transition 列のいうテキストを入れる
            If moveText = ">" Then
                Worksheets("Tape").Range("B2").Value = transitionText
            End If

            Exit For
        End If
    Next i

End Sub

Private Sub On2ndClock()
    ' 2回目のクロック
    Dim text As String
    Dim backgroundColor As Long
    Dim i As Long
    Dim stateText As String
    Dim readBackgroundColor As Long
    Dim writeBackgroundColor As Long
    Dim moveText As String
    Dim transitionText As String

    text = Worksheets("Tape").Range("B2").Value ' セルの値
    backgroundColor = Worksheets("Tape").Range("B2").Interior.color ' 背景色

    ' ★ 上の行の背景色は引き継ぐ
    Worksheets("Tape").Range("A3").Interior.color = Worksheets("Tape").Range("A2").Interior.color
    Worksheets("Tape").Range("B3").Interior.color = Worksheets("Tape").Range("B3").Interior.color

    For i = 2 To 7
        stateText = Worksheets("StateTable").Range("A" & i).Value ' セルの値
        readBackgroundColor = Worksheets("StateTable").Range("B" & i).Interior.color ' 背景色

        ' 一致するか?
        If text = stateText And backgroundColor = readBackgroundColor Then
            writeBackgroundColor = Worksheets("StateTable").Range("C" & i).Interior.color ' 背景色
            moveText = Worksheets("StateTable").Range("D" & i).Value ' セルの値
            transitionText = Worksheets("StateTable").Range("E" & i).Value ' セルの値

            ' `Tape` シートの A1 セルの下のセルの背景色を Write列のいう色に塗る
            Worksheets("Tape").Range("B3").Interior.color = writeBackgroundColor

            ' ★ Move 列が `<` だったら その左のセルへ、 Transition 列のいうテキストを入れる
            If moveText = "<" Then
                Worksheets("Tape").Range("A3").Value = transitionText
            End If

            Exit For
        End If
    Next i
End Sub

ramen-tabero-futsu2.png
「 じゃあ GUI シートのボタンを押そうぜ?」

202301_excel_26-1915--Check.png

ramen-tabero-futsu2.png
「 👆 ちゃんと動いてるな」

kifuwarabe-futsu.png
「 場所を移しただけだしな」

📅2023-01-26 thu 19:16

ohkina-hiyoko-futsu2.png
「 3クロック目も コピー貼り付けして作んの?」

ramen-tabero-futsu2.png
「 嫌になるだろ」

202301_excel_26-1919--OnClock-1.png

ramen-tabero-futsu2.png
「 👆 何回目のクロックでも使えるジェネラル(General)なサブルーチンを作ろうぜ?」

202301_excel_26-1911--MovedCode-diff.png

ramen-tabero-futsu2.png
「 👆 違うところは5か所ぐらいなんだから、ここを違わないようにすればいいわけだぜ」

ramen-tabero-futsu2.png
「 A列の右隣は B列 だが、
A の右は何か尋ねたら B が返ってくるような方法って VBA にあるのかだぜ?」

ohkina-hiyoko-futsu2.png
「 ググりゃいいんじゃないの?」

ramen-tabero-futsu2.png
「 🔍 VBA 列アルファベット変換 で検索」

📖 【ExcelVBA】列名のアルファベットと列番号の数字を相互変換する

ramen-tabero-futsu2.png
「 👆 よし 分かったぜ」

202301_excel_26-2110--General.png

Sub ボタン1_Click()

    ' 1回目の処理
    Call OnClock("A", 1)

    ' 同様の2回目の処理
    Call OnClock("B", 2)

End Sub

Private Sub OnClock(previousFileAlphabet As String, previousRank As Long)
    ' TODO 毎クロック(n回目のクロック)
    Dim previousText As String
    Dim previousBackgroundColor As Long
    Dim previousCell As String
    Dim currentRank As Long
    Dim currentCell As String
    Dim stateText As String
    Dim readBackgroundColor As Long
    Dim writeBackgroundColor As Long
    Dim moveText As String
    Dim transitionText As String
    Dim i As Long

    previousCell = previousFileAlphabet & previousRank
    currentRank = previousRank + 1
    currentCell = previousFileAlphabet & currentRank
    Debug.Print ("--------")
    Debug.Print ("previousFileAlphabet:" & previousFileAlphabet)
    Debug.Print ("previousRank        :" & previousRank)
    Debug.Print ("previousCell        :" & previousCell)
    Debug.Print ("currentRank         :" & currentRank)
    Debug.Print ("currentCell         :" & currentCell)

    ' 開始行の背景色は、次行に引き継ぐ
    If 2 <= previousRank Then
        Dim aBackgroundColor As Long
        Dim bBackgroundColor As Long
        aBackgroundColor = Worksheets("Tape").Range("A" & previousRank).Interior.color
        bBackgroundColor = Worksheets("Tape").Range("B" & previousRank).Interior.color
        Worksheets("Tape").Range("A" & currentRank).Interior.color = aBackgroundColor
        Worksheets("Tape").Range("B" & currentRank).Interior.color = bBackgroundColor
        Debug.Print ("aBackgroundColor:" & aBackgroundColor)
        Debug.Print ("bBackgroundColor:" & bBackgroundColor)
    End If

    previousText = Worksheets("Tape").Range(previousCell).Value                             ' 開始セルの値
    previousBackgroundColor = Worksheets("Tape").Range(previousCell).Interior.color         ' 開始セルの背景色
    Debug.Print ("previousText           :" & previousText)
    Debug.Print ("previousBackgroundColor:" & previousBackgroundColor)

    For i = 2 To 7
        stateText = Worksheets("StateTable").Range("A" & i).Value                           ' 状態テーブルのState値
        readBackgroundColor = Worksheets("StateTable").Range("B" & i).Interior.color        ' 状態テーブルのRead列の背景色
        Debug.Print ("stateText           :" & stateText)
        Debug.Print ("readBackgroundColor :" & readBackgroundColor)

        ' 一致するか?
        If previousText = stateText And previousBackgroundColor = readBackgroundColor Then
            writeBackgroundColor = Worksheets("StateTable").Range("C" & i).Interior.color   ' 状態テーブルのWrite列の背景色
            moveText = Worksheets("StateTable").Range("D" & i).Value                        ' 状態テーブルのMove列の値
            transitionText = Worksheets("StateTable").Range("E" & i).Value                  ' 状態テーブルのTransition列の値
            Debug.Print ("writeBackgroundColor:" & writeBackgroundColor)
            Debug.Print ("moveText            :" & moveText)
            Debug.Print ("transitionText      :" & transitionText)

            ' `Tape` シートの A1 セルの下のセルの背景色を Write列のいう色に塗る
            Worksheets("Tape").Range(currentCell).Interior.color = writeBackgroundColor

            Dim horizontal As Long      ' 水平方向
            If moveText = ">" Then      ' Move 列が `>` だったら その右のセルへ
                horizontal = 1
            ElseIf moveText = "<" Then  ' Move 列が `<` だったら その左のセルへ
                horizontal = -1
            End If
            Debug.Print ("horizontal:" & horizontal)

            ' Transition 列のいうテキストを入れる
            Dim startFileNumber As Integer
            Dim nextFileAlphabet As String
            startFileNumber = Columns(previousFileAlphabet).Column
            nextFileAlphabet = Split(Cells(1, startFileNumber + horizontal).Address, "$")(1)
            Debug.Print ("startFileNumber :" & startFileNumber)
            Debug.Print ("nextFileAlphabet:" & nextFileAlphabet)
            Worksheets("Tape").Range(nextFileAlphabet & currentRank).Value = transitionText

            Exit For
        End If
    Next i
End Sub

ramen-tabero-futsu2.png
「 👆 けっこう 大がかりに 変えることになってしまったぜ」

📅2023-01-26 thu 21:12

kifuwarabe-futsu.png
「 こんなん 何がどう変わったのか 読者 分からんだろ」

202301_excel_26-2114--3rdClock-1.png

ramen-tabero-futsu2.png
「 👆 1クロック目と 2クロック目で違うところは、 スタート地点の列番号と、行番号だけだったということだぜ」

ramen-tabero-futsu2.png
「 このように 2つのサブルーチンの差異が サブルーチンの外に押し出されたものを アーギュメント(Argument;実引数)と呼ぶ」

ohkina-hiyoko-futsu2.png
「 ふーん」

ohkina-hiyoko-futsu2.png
「 3クロック目は どう書くの?」

202301_excel_26-2121--argument-1.png

ramen-tabero-futsu2.png
「 👆 A1 とか B2 というのは、1クロック前に居たセルだぜ。
だから 前の計算結果を もらうといい。
書き直そう」

ramen-tabero-futsu2.png
「 VBA でファンクションは どうやって書いたらいいんだぜ?」

ohkina-hiyoko-futsu2.png
「 ググりゃいいんじゃないの?」

ramen-tabero-futsu2.png
「 🔍 VBA ファンクション で検索」

📖 VBA Functionプロシージャについて ~関数の解説と使用例~

ramen-tabero-futsu2.png
「 👆 よし 分かったぜ」

202301_excel_26-2141--function.png

Sub ボタン1_Click()

    Dim resultCell As String

    ' 1回目の処理
    resultCell = OnClock("A1")

    ' 同様の2回目の処理
    resultCell = OnClock(resultCell)

End Sub

Private Function OnClock(previousCell As String) As String
    ' 毎クロック(n回目のクロック)
    Dim previousText As String
    Dim previousBackgroundColor As Long
    Dim currentRank As Long
    Dim currentCell As String
    Dim stateText As String
    Dim readBackgroundColor As Long
    Dim writeBackgroundColor As Long
    Dim moveText As String
    Dim transitionText As String
    Dim i As Long

    previousFileAlphabet = Split(Cells(1, Range(previousCell).Column).Address, "$")(1)
    previousRank = Range(previousCell).Row
    currentRank = previousRank + 1
    currentCell = previousFileAlphabet & currentRank
    Debug.Print ("--------")
    Debug.Print ("previousCell        :" & previousCell)
    Debug.Print ("previousFileAlphabet:" & previousFileAlphabet)
    Debug.Print ("previousRank        :" & previousRank)
    Debug.Print ("currentRank         :" & currentRank)
    Debug.Print ("currentCell         :" & currentCell)

    ' 開始行の背景色は、次行に引き継ぐ
    If 2 <= previousRank Then
        Dim aBackgroundColor As Long
        Dim bBackgroundColor As Long
        aBackgroundColor = Worksheets("Tape").Range("A" & previousRank).Interior.color
        bBackgroundColor = Worksheets("Tape").Range("B" & previousRank).Interior.color
        Worksheets("Tape").Range("A" & currentRank).Interior.color = aBackgroundColor
        Worksheets("Tape").Range("B" & currentRank).Interior.color = bBackgroundColor
        Debug.Print ("aBackgroundColor:" & aBackgroundColor)
        Debug.Print ("bBackgroundColor:" & bBackgroundColor)
    End If

    previousText = Worksheets("Tape").Range(previousCell).Value                             ' 開始セルの値
    previousBackgroundColor = Worksheets("Tape").Range(previousCell).Interior.color         ' 開始セルの背景色
    Debug.Print ("previousText           :" & previousText)
    Debug.Print ("previousBackgroundColor:" & previousBackgroundColor)

    For i = 2 To 7
        stateText = Worksheets("StateTable").Range("A" & i).Value                           ' 状態テーブルのState値
        readBackgroundColor = Worksheets("StateTable").Range("B" & i).Interior.color        ' 状態テーブルのRead列の背景色
        Debug.Print ("stateText           :" & stateText)
        Debug.Print ("readBackgroundColor :" & readBackgroundColor)

        ' 一致するか?
        If previousText = stateText And previousBackgroundColor = readBackgroundColor Then
            writeBackgroundColor = Worksheets("StateTable").Range("C" & i).Interior.color   ' 状態テーブルのWrite列の背景色
            moveText = Worksheets("StateTable").Range("D" & i).Value                        ' 状態テーブルのMove列の値
            transitionText = Worksheets("StateTable").Range("E" & i).Value                  ' 状態テーブルのTransition列の値
            Debug.Print ("writeBackgroundColor:" & writeBackgroundColor)
            Debug.Print ("moveText            :" & moveText)
            Debug.Print ("transitionText      :" & transitionText)

            ' `Tape` シートの A1 セルの下のセルの背景色を Write列のいう色に塗る
            Worksheets("Tape").Range(currentCell).Interior.color = writeBackgroundColor

            Dim horizontal As Long      ' 水平方向
            If moveText = ">" Then      ' Move 列が `>` だったら その右のセルへ
                horizontal = 1
            ElseIf moveText = "<" Then  ' Move 列が `<` だったら その左のセルへ
                horizontal = -1
            End If
            Debug.Print ("horizontal:" & horizontal)

            ' Transition 列のいうテキストを入れる
            Dim previousFileNumber As Integer
            Dim nextFileAlphabet As String
            Dim nextCell As String
            previousFileNumber = Columns(previousFileAlphabet).Column
            nextFileAlphabet = Split(Cells(1, previousFileNumber + horizontal).Address, "$")(1)
            nextCell = nextFileAlphabet & currentRank
            Debug.Print ("previousFileNumber :" & previousFileNumber)
            Debug.Print ("nextFileAlphabet   :" & nextFileAlphabet)
            Debug.Print ("nextCell           :" & nextCell)
            Worksheets("Tape").Range(nextCell).Value = transitionText

            ' 関数から抜ける
            OnClock = nextCell
            Exit Function

        End If
    Next i
End Function

ramen-tabero-futsu2.png
「 👆 さらに 改造してしまったぜ」

📅2023-01-26 thu 21:43

kifuwarabe-futsu.png
「 こんなん 何がどう変わったのか 読者 分からんだろ」

202301_excel_26-2146--main-1.png

ramen-tabero-futsu2.png
「 👆 このように 前の計算結果を使って また同様に計算するように作るのは よくあるテクニックだぜ。
漸化式(ぜんかしき) とか これだな」

ohkina-hiyoko-futsu2.png
「 さっさと 3クロック目を書きなさいよ」

202301_excel_26-2150--3rdClock-1.png

ramen-tabero-futsu2.png
「 👆 3クロック目は、2クロック目と同じコードで いいはずなんだぜ。
動かしてみよう」

202301_excel_26-2152--error-1.png

ramen-tabero-futsu2.png
「 👆 エラーが出たぜ」

kifuwarabe-futsu.png
「 直せ!」

202301_excel_26-2153--debug-1.png

ramen-tabero-futsu2.png
「 👆 [デバッグ(D)] ボタンを押してみようぜ?」

202301_excel_26-2155--highlight.png

ohkina-hiyoko-futsu2.png
「 エラーがあった行をハイライト(Highlight)してくれてるのかもしれないけど 説明がないから分かんないわねぇ」

ramen-tabero-futsu2.png
「 また ショートカットを打鍵すれば ウィンドウが出てくるのかだぜ?」

ohkina-hiyoko-futsu2.png
「 ググりゃいいんじゃないの?」

ramen-tabero-futsu2.png
「 🔍 VBA スタックトレース で検索」

📖 VBAナメてた

ramen-tabero-futsu2.png
「 👆 行番号とエラーコードしか 分かんないらしいぜ」

ohkina-hiyoko-futsu2.png
「 1994年の環境ねえ」

📅2023-01-26 thu 22:01

ramen-tabero-futsu2.png
「 🔍 VBA 1004 Error で検索」

📖 VBA Error 1004 in Excel

ramen-tabero-futsu2.png
「 👆 無いシート名や、無いアドレスを指定したら 出てくるエラーかも知らん」

202301_excel_26-2207--error.png

ramen-tabero-futsu2.png
「 👆 A列より 左に進もうとして 0列目は無いので エラーになったんだな」

kifuwarabe-futsu.png
「 A列の左に 列を挿入しろだぜ」

ramen-tabero-futsu2.png
「 🔍 VBA 列の挿入 で検索」

📖 VBA 列を挿入する

ramen-tabero-futsu2.png
「 👆 よし 分かったぜ」

202301_excel_26-2215--bugfix-1.png

ramen-tabero-futsu2.png
「 👆 0列にアクセスしようとしたら、
A列の左に 列挿入して、 列番地も左に 1つずらして 1列にアクセスするように変更しよう」

202301_excel_26-2218--3rdClockDone.png

ramen-tabero-futsu2.png
「 👆 3クロック目も 動いたようだな」

📅2023-01-26 thu 22:18

ohkina-hiyoko-futsu2.png
「 じゃあ あと 100クロックぐらい 動かしましょう」

202301_excel_26-2226--For-1.png

ramen-tabero-futsu2.png
「 👆 For 文を使えば 100行書かずに済むから楽だよな。
漸化式の形に作っておけば 繰り返しの回数を指定するだけでいいから 手間要らずだぜ」

202301_excel_26-2229--Error-1.png

ramen-tabero-futsu2.png
「 👆 エラーが出た」

kifuwarabe-futsu.png
「 🔍 VBA 1004 Error で検索しろだぜ」

ohkina-hiyoko-futsu2.png
「 さっきと同じ 1004 番よ」

ramen-tabero-futsu2.png
「 今度は何が無かったんだろな?」

202301_excel_26-2237--empty-previous-cell-1.png

ramen-tabero-futsu2.png
「 👆 previousCell 変数の中身が空文字列だぜ。」

ohkina-hiyoko-futsu2.png
「 A1 とか、何か番地が入っているはずじゃない」

202301_excel_26-2234--reset-button-1.png

ramen-tabero-futsu2.png
「 👆 リセット ボタンを押すと デバッグは止まるぜ」

202301_excel_26-2240--Halt-1.png

ramen-tabero-futsu2.png
「 👆 多分、 HALT (ハルト)が出たら 終了しないといけないんだぜ」

202301_excel_26-2246--success-1.png

            If transitionText = "HALT" Then
                ' 関数から抜ける
                OnClock = "SUCCESS"
                Exit Function
            End If

ramen-tabero-futsu2.png
「 👆 じゃあ HALT (ハルト)が出たときは セル番地ではなく SUCCESS を返すという取り決めにしておこうぜ」

202301_excel_26-2252--exitFor-1.png

        ' 正常終了時はループから抜ける
        If resultCell = "SUCCESS" Then
            Exit For
        End If

ramen-tabero-futsu2.png
「 👆 関数の呼び出し元でも 前判定で resultCell 変数の内容が SUCCESS だったら、ループから抜けるようにすれば コードも短いぜ」

202301_excel_26-2255--play.png

ramen-tabero-futsu2.png
「 👆 なんか 動いた跡があるな」

202301_excel_26-2257--QUOTE-Wikipedia.png

📖 Turing machine

ohkina-hiyoko-futsu2.png
「 👆 Wikipedia のサンプルと違くない?」

202301_excel_26-2308--copy-paint-1.png

ramen-tabero-futsu2.png
「 👆 A列と B列しか 背景色をコピーしてないからな」

kifuwarabe-futsu.png
「 テープの長さと同じ幅だけ コピーしてくれだぜ」

ramen-tabero-futsu2.png
「 じゃあ テープの長さも 数えておかないといけないな」

ramen-tabero-futsu2.png
「 VBA で、自由変数の使い方、または 引数の参照渡しのやり方は どう書くんだろうな?」

ohkina-hiyoko-futsu2.png
「 ググりゃいいんじゃないの?」

ramen-tabero-futsu2.png
「 🔍 VBA 引数の参照渡し で検索」

📖 値渡し(ByVal)、参照渡し(ByRef)について

ramen-tabero-futsu2.png
「 👆 よし 分かったぜ」

kifuwarabe-futsu.png
「 ぜったい そんな記事を ちらっと読んでも 何も分からないけどな」

ramen-tabero-futsu2.png
「 プログラムの組み方は分かってるが VBA での書き方が分からないだけなんで、ちら見で 充分だぜ」

ramen-tabero-futsu2.png
「 あれっ? ByRef tapeWidth As Long と書いても 参照渡しになって無(ね)。
調べても分からないことはある。
自由変数にするか」

ramen-tabero-futsu2.png
「 🔍 VBA グローバル変数 で検索」

📖 変数の適用範囲

ramen-tabero-futsu2.png
「 👆 よし 分かったぜ」

ramen-tabero-futsu2.png
「 でも 書いているうちに tapeWith というスペルミスを見つけて tapeWidth 直したら動くようになった」

202301_excel_26-2354--codeA-1.png
202301_excel_26-2357--codeB-1.png

Sub ボタン1_Click()

    Dim resultCell As String
    Dim tapeWidth As Long
    tapeWidth = 1   ' "A1" セル1つ分

    ' 1回目の処理
    resultCell = OnClock("A1", tapeWidth)

    ' 同様の i 回目の処理
    Dim i As Long
    For i = 2 To 100

        ' 正常終了時はループから抜ける
        If resultCell = "SUCCESS" Then
            Exit For
        End If

        resultCell = OnClock(resultCell, tapeWidth)
    Next i

End Sub

Private Function OnClock(previousCell As String, ByRef tapeWidth) As String
    ' 毎クロック(n回目のクロック)
    Dim previousFileAlphabet As String
    Dim previousRank As Long
    Dim previousText As String
    Dim previousBackgroundColor As Long
    Dim currentRank As Long
    Dim currentCell As String
    Dim stateText As String
    Dim readBackgroundColor As Long
    Dim writeBackgroundColor As Long
    Dim moveText As String
    Dim transitionText As String
    Dim i As Long

    previousFileAlphabet = Split(Cells(1, Range(previousCell).Column).Address, "$")(1)
    previousRank = Range(previousCell).Row
    currentRank = previousRank + 1
    currentCell = previousFileAlphabet & currentRank
    Debug.Print ("--------")
    Debug.Print ("previousCell        :" & previousCell)
    Debug.Print ("previousFileAlphabet:" & previousFileAlphabet)
    Debug.Print ("previousRank        :" & previousRank)
    Debug.Print ("currentRank         :" & currentRank)
    Debug.Print ("currentCell         :" & currentCell)
    Debug.Print ("tapeWidth           :" & tapeWidth)

    ' 開始行の背景色は、次行に引き継ぐ
    If 2 <= previousRank Then
        For i = 1 To (tapeWidth + 1)
            Dim tempFileAlphabet As String
            Dim tempBackgroundColor As Long

            tempFileAlphabet = Split(Cells(1, i).Address, "$")(1)
            tempBackgroundColor = Worksheets("Tape").Range(tempFileAlphabet & previousRank).Interior.color
            Debug.Print ("i                  :" & i)
            Debug.Print ("tempFileAlphabet   :" & tempFileAlphabet)
            Debug.Print ("tempBackgroundColor:" & tempBackgroundColor)
            Worksheets("Tape").Range(tempFileAlphabet & currentRank).Interior.color = tempBackgroundColor
        Next i
    End If

    previousText = Worksheets("Tape").Range(previousCell).Value                             ' 開始セルの値
    previousBackgroundColor = Worksheets("Tape").Range(previousCell).Interior.color         ' 開始セルの背景色
    Debug.Print ("previousText           :" & previousText)
    Debug.Print ("previousBackgroundColor:" & previousBackgroundColor)

    For i = 2 To 7
        stateText = Worksheets("StateTable").Range("A" & i).Value                           ' 状態テーブルのState値
        readBackgroundColor = Worksheets("StateTable").Range("B" & i).Interior.color        ' 状態テーブルのRead列の背景色
        Debug.Print ("stateText           :" & stateText)
        Debug.Print ("readBackgroundColor :" & readBackgroundColor)

        ' 一致するか?
        If previousText = stateText And previousBackgroundColor = readBackgroundColor Then
            writeBackgroundColor = Worksheets("StateTable").Range("C" & i).Interior.color   ' 状態テーブルのWrite列の背景色
            moveText = Worksheets("StateTable").Range("D" & i).Value                        ' 状態テーブルのMove列の値
            transitionText = Worksheets("StateTable").Range("E" & i).Value                  ' 状態テーブルのTransition列の値
            Debug.Print ("writeBackgroundColor:" & writeBackgroundColor)
            Debug.Print ("moveText            :" & moveText)
            Debug.Print ("transitionText      :" & transitionText)

            ' `Tape` シートの A1 セルの下のセルの背景色を Write列のいう色に塗る
            Worksheets("Tape").Range(currentCell).Interior.color = writeBackgroundColor

            Dim horizontal As Long      ' 水平方向
            If moveText = ">" Then      ' Move 列が `>` だったら その右のセルへ
                horizontal = 1
            ElseIf moveText = "<" Then  ' Move 列が `<` だったら その左のセルへ
                horizontal = -1
            End If
            Debug.Print ("horizontal:" & horizontal)

            ' Transition 列のいうテキストを入れる
            Dim previousFileNumber As Integer
            Dim nextFileAlphabet As String
            Dim nextFileNumber As Integer
            Dim nextCell As String
            previousFileNumber = Columns(previousFileAlphabet).Column
            nextFileNumber = previousFileNumber + horizontal
            Debug.Print ("previousFileNumber :" & previousFileNumber)
            Debug.Print ("nextFileNumber     :" & nextFileNumber)

            If nextFileNumber = 0 Then
                Worksheets("Tape").Columns("A").Insert  ' A 列の左に列挿入
                nextFileNumber = nextFileNumber + 1     ' 列番号を 1 つ左へずらす
                tapeWidth = tapeWidth + 1               ' Update
            ElseIf tapeWidth < nextFileNumber Then
                tapeWidth = nextFileNumber              ' Update
            End If

            nextFileAlphabet = Split(Cells(1, nextFileNumber).Address, "$")(1)
            nextCell = nextFileAlphabet & currentRank
            Debug.Print ("nextFileAlphabet   :" & nextFileAlphabet)
            Debug.Print ("nextCell           :" & nextCell)
            Worksheets("Tape").Range(nextCell).Value = transitionText

            If transitionText = "HALT" Then
                ' 関数から抜ける
                OnClock = "SUCCESS"
                Exit Function
            End If

            ' 関数から抜ける
            OnClock = nextCell
            Exit Function

        End If
    Next i
End Function

ramen-tabero-futsu2.png
「 👆 よし tapeWidth も付けたぜ」

202301_excel_27-0000--play.png

ramen-tabero-futsu2.png
「 👆 チューリング・マシンが でけたぜ」

📅2023-01-27 thu 00:01

ohkina-hiyoko-futsu2.png
「 HALT は出力しないのが チューリング・マシンみたいよ?」

ramen-tabero-futsu2.png
「 じゃあ StateTable の Transition 列で HALT を見つけたところで 止めた方がいいのか」

202301_excel_27-0005--moveCode-1.png

ramen-tabero-futsu2.png
「 👆 判定部を 上に持っていけば いいんじゃないかだぜ?」

202301_excel_27-0008--copy-1.png

ohkina-hiyoko-futsu2.png
「 👆 1行 余分に背景色をコピーしてるわよ?」

202301_excel_27-0010--copyB-1.png

ramen-tabero-futsu2.png
「 👆 背景色のコピーを、判定部の後ろに持っていけば いいんじゃないかだぜ?」

202301_excel_27-0012--error.png

ramen-tabero-futsu2.png
「 👆 おっと」

202301_excel_27-0013--define-1.png

ramen-tabero-futsu2.png
「 👆 変数 i は、外側のループの ループ・カウンターとして使ってたか」

202301_excel_27-0016--J-1.png

ramen-tabero-futsu2.png
「 👆 じゃあ 変数 j を作ればいいや」

202301_excel_27-0018--ok.png

ramen-tabero-futsu2.png
「 👆 これで チューリング・マシン は完成だぜ。
一般化してないのと、不具合が残っているのが 残っているかも知れないが……」

kifuwarabe-futsu.png
「 これは何に使うんだぜ?」

ramen-tabero-futsu2.png
「 👆 この例だと 長さ6のオレンジ色のテープを作るために使ったんだぜ。
説明しよう」

202301_excel_27-0028--stateTable.png

ramen-tabero-futsu2.png
「 👆 この StateTable というのが プログラムの命令セットのようなものだぜ。
この6行があると……」

202301_excel_27-0134--A.png

ramen-tabero-futsu2.png
「 👆 最初は A と書かれているが、
A にあんまり意味は無くて 長さ1つの白いテープがそこにあると思えだぜ」

202301_excel_27-0030--tape.png

ramen-tabero-futsu2.png
「 👆 上の図は テープを伸ばしている過程が描かれたもので、
最後の 13行目が 作られたテープだぜ」

ramen-tabero-futsu2.png
「 長さ6つのオレンジ色のテープ が作られて 動作が止まったわけだぜ」

kifuwarabe-futsu.png
「 長さ6つのオレンジ色のテープ なんか 作っても 嬉しくないしなあ」

ramen-tabero-futsu2.png
「 今回はサンプルで オレンジ色1色しかなかったが、
それが 26色あって アルファベットの26文字に対応するとか、
最初から色の塗られたテープがあって チューリング・マシンが動いた後には 別の色のテープが残っているとか、
それが 暗号を解読した答え だったりしたら 嬉しいだろ」

kifuwarabe-futsu.png
「 じゃあ 今回の例題 がシンプルすぎて チューリング・マシン が何たるか 嬉しさが 分からないんだ」

ramen-tabero-futsu2.png
「 プログラミング言語 が誕生する前の プログラミング 方法だぜ」

kifuwarabe-futsu.png
「 私たちが使うのは プログラミング言語 なのだから、懐古好きでもなければ チューリング・マシン を覚えなくてもいいのでは?」

ramen-tabero-futsu2.png
「 一生のうちに 何回か チューリング・マシン とか チューリング完全 の名前を聞くことはあるだろ。
記事を読んで知識として知ってるより 作って知ってる方が 話しに実感がこもるだろ」

kifuwarabe-futsu.png
「 実感 要らないから 実用的で 効率的な練習 無いのかなあ」

ramen-tabero-futsu2.png
「 そんな実用的な Webサイト なら、他人が作るしな 」

ohkina-hiyoko-futsu2.png
「 プログラムも コンピューターも無い時代に ヒトが考えたプログラムを 体験 するぐらいね」

kifuwarabe-futsu.png
「 もっと面白い例題でやってほしい」

参考: 📖 Git Hub / TuringMachineByExcelVBA

<おわり>

ツイッターでシェア
みんなに共有、忘れないようにメモ

むずでょ

光速のアカウント凍結されちゃったんで……。ゲームプログラムを独習中なんだぜ☆電王戦IIに出た棋士もコンピューターもみんな好きだぜ☆▲(パソコン将棋)WCSC29一次予選36位、SDT5予選42位▲(パソコン囲碁)AI竜星戦予選16位

Crieitは誰でも投稿できるサービスです。 是非記事の投稿をお願いします。どんな軽い内容でも投稿できます。

また、「こんな記事が読みたいけど見つからない!」という方は是非記事投稿リクエストボードへ!

有料記事を販売できるようになりました!

こじんまりと作業ログやメモ、進捗を書き残しておきたい方はボード機能をご利用ください。
ボードとは?

コメント