<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>マクロ - プログラミングで遊ブログ</title>
	<atom:link href="https://lemon818.com/tag/%E3%83%9E%E3%82%AF%E3%83%AD/feed/" rel="self" type="application/rss+xml" />
	<link>https://lemon818.com</link>
	<description>現役システムエンジニアが趣味でプログラミングする自由気ままなブログ</description>
	<lastBuildDate>Tue, 28 Jun 2022 23:43:49 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0</generator>
<atom:link rel="hub" href="https://pubsubhubbub.appspot.com"/><atom:link rel="hub" href="https://pubsubhubbub.superfeedr.com"/>	<item>
		<title>Excelでできる！複数のファイル名を一括置換するツール！</title>
		<link>https://lemon818.com/filename_bulk_replace/</link>
		
		<dc:creator><![CDATA[Take]]></dc:creator>
		<pubDate>Sat, 05 Jun 2021 01:52:34 +0000</pubDate>
				<category><![CDATA[Excel]]></category>
		<category><![CDATA[業務で役立つExcelマクロ]]></category>
		<category><![CDATA[vba]]></category>
		<category><![CDATA[エクセル]]></category>
		<category><![CDATA[ファイル名]]></category>
		<category><![CDATA[マクロ]]></category>
		<category><![CDATA[一括置換]]></category>
		<category><![CDATA[複数]]></category>
		<guid isPermaLink="false">https://lemon818.com/?p=4644</guid>

					<description><![CDATA[&#160; 「手作業でファイル名をちまちま変更するの超めんどくさくないですか？」 &#160; 私はある日、お客さんから「100ファイル近くのExcelファイルのファイル名を手作業で変更してくれ～」っていう依頼がありま…]]></description>
										<content:encoded><![CDATA[<p>&nbsp;</p>
<div class="sc_frame_wrap solid yellow">
<div class="sc_frame ">
<div class="sc_frame_text"><span style="color: #000000;">「<strong>手作業でファイル名をちまちま変更するの</strong><span style="color: #ff0000;"><strong>超めんどくさくない</strong></span><strong>ですか？</strong>」</span></div>
</div>
</div>
<p>&nbsp;</p>
<p>私はある日、お客さんから「100ファイル近くのExcelファイルのファイル名を手作業で変更してくれ～」っていう依頼がありました。</p>
<p>具体的には「ファイル名_v1.2.xlsx」→「ファイル名_v1.3.xlsx」にしてくれ～～っていう内容です。</p>
<p>&nbsp;</p>
<p>もちろん、ファイル数が少ないなら手でやったほうが速いと思いますが、</p>
<p>毎回、100ファイル近くを手作業でやるのにどれだけ時間がかかるか・・</p>
<p>しかし仕事をしていれば、<strong><span style="color: #ff0000;">少なからずそんなめんどくさい作業をすることがある</span></strong>と思います。</p>
<p>&nbsp;</p>
<p>そこでこの度はExcel をつかって<span style="color: #0000ff;"><strong>ボタン一つ</strong></span><strong><span style="color: #0000ff;">でファイル名を一括置換するツール</span>を作りました！</strong></p>
<p>&nbsp;</p>
<p><img fetchpriority="high" decoding="async" src="https://lemon818.com/wp/wp-content/uploads/2021/06/replaceFile.gif" alt="" width="1106" height="452" class="aligncenter wp-image-5753" /></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>このツールは Excel Vba というプログラミングで作成されています。</p>
<p>この記事では「<strong>Excelでできる複数のファイル名を一括置換するツール</strong>」の使い方と作り方をご紹介します！</p>
<p>&nbsp;</p>
<div>
<p>※ いつものようにつくったのも共有しときますので、よければどうぞ（ブック保護、シート保護はかかってます）。</p>
<p>※ 2022/06 機能追加しました！！50000ファイルまで置換可能！実行中画面の追加実装など！よければインストールどうぞ！</p>
<p>&nbsp;</p>
<div class="button frame block green"><a class="midium" href="https://lemon818.com/wp/wp-content/uploads/2022/06/replaceFileName_ver1.1.xlsm">Excelでできる複数のファイル名を一括置換するツール</a></div>
<p>&nbsp;</p>
<h2>ファイル名を一括置換するツールの使い方</h2>
</div>
<p>この「Excelでできる複数のファイル名を一括置換するツール」では以下の用に使います。</p>
<p>&nbsp;</p>
<div class="sc_frame_wrap orange">
<div class="sc_frame ">
<div class="sc_frame_text">
<div class="sc_designlist ol square solid orange">
<ol>
<li>「検索ボタン」を押下しリネームしたいファイル一覧を取得します。</li>
<li>ファイル名（置換後）列にファイル名を入力します。</li>
<li>「一括置換」ボタンでファイル名が変更できます。</li>
</ol>
</div>
</div>
</div>
</div>
<p>&nbsp;</p>
<h3>「検索ボタン」を押下してリネームしたいファイル一覧を取得</h3>
<p>まず「検索ボタン」を押下します。</p>
<p>&nbsp;</p>
<p><img decoding="async" src="https://lemon818.com/wp/wp-content/uploads/2021/06/excel-rename1.png" alt="" width="664" height="242" class="aligncenter size-full wp-image-5758" srcset="https://lemon818.com/wp/wp-content/uploads/2021/06/excel-rename1.png 664w, https://lemon818.com/wp/wp-content/uploads/2021/06/excel-rename1-300x109.png 300w" sizes="(max-width: 664px) 100vw, 664px" /></p>
<p>&nbsp;</p>
<p>すると下記のようなダイアログが表示されるので、ファイル名を変換したいファイルが配架されているフォルダを選択します。</p>
<p>このときファイル名が表示されませんが、問題ありません。</p>
<p>&nbsp;</p>
<p><img decoding="async" src="https://lemon818.com/wp/wp-content/uploads/2021/06/excel-rename2.png" alt="" width="941" height="530" class="aligncenter size-full wp-image-5759" srcset="https://lemon818.com/wp/wp-content/uploads/2021/06/excel-rename2.png 941w, https://lemon818.com/wp/wp-content/uploads/2021/06/excel-rename2-300x169.png 300w, https://lemon818.com/wp/wp-content/uploads/2021/06/excel-rename2-768x433.png 768w" sizes="(max-width: 941px) 100vw, 941px" /></p>
<p>&nbsp;</p>
<p>これにてB列とＣ列にファイル名とフォルダ名がそれぞれ出力されます。</p>
<p>ファイル名、フォルダ名を手入力しても問題ないですが、<span style="color: #ff0000;"><strong>入力し間違えないようにする必要があります。</strong></span></p>
<p>&nbsp;</p>
<h3>ファイル名（置換後）列にファイル名を入力</h3>
<p>ファイル名（置換後）列にファイル名を入力します。</p>
<p>このツールではファイル名（置換前）→ファイル名（置換後）のようにファイルがリネーム（名前が変更）されます。</p>
<p>&nbsp;</p>
<p><img loading="lazy" decoding="async" src="https://lemon818.com/wp/wp-content/uploads/2021/06/excel-rename3.png" alt="" width="1031" height="441" class="aligncenter wp-image-5760" srcset="https://lemon818.com/wp/wp-content/uploads/2021/06/excel-rename3.png 1204w, https://lemon818.com/wp/wp-content/uploads/2021/06/excel-rename3-300x128.png 300w, https://lemon818.com/wp/wp-content/uploads/2021/06/excel-rename3-1024x438.png 1024w, https://lemon818.com/wp/wp-content/uploads/2021/06/excel-rename3-768x329.png 768w" sizes="auto, (max-width: 1031px) 100vw, 1031px" /></p>
<p>&nbsp;</p>
<p>ファイル名（置換後）列に入力後、チェック列（E列）の値がOKになっていれば問題なしです。</p>
<p>なおチェック列がOK以外の値の場合は<span style="color: #ff0000;"><strong>ファイル名置換処理が実行されません。</strong></span></p>
<p>&nbsp;</p>
<h4>チェック列がＯK以外のケース</h4>
<p>チェック列では入力した値に誤りがあるかどうかをチェックします。</p>
<div class="sc_frame_wrap orange">
<div class="sc_frame ">
<div class="sc_frame_text">
<div class="sc_designlist ol square solid orange">
<ol>
<li><span style="color: #ff0000;">ファイル名（置換後）列に重複している項目がないか</span></li>
<li><span style="color: #3366ff;">ファイル名（置換前）列とファイル名（置換後）列で同じ値がないか</span></li>
</ol>
</div>
</div>
</div>
</div>
<p>&nbsp;</p>
<p>もし上記項目を満たした場合、下記画面のようにエラーメッセージが表示されるようになっています。</p>
<p>&nbsp;</p>
<p><img loading="lazy" decoding="async" src="https://lemon818.com/wp/wp-content/uploads/2021/06/excel-rename6.png" alt="" width="785" height="200" class="aligncenter size-full wp-image-5765" srcset="https://lemon818.com/wp/wp-content/uploads/2021/06/excel-rename6.png 785w, https://lemon818.com/wp/wp-content/uploads/2021/06/excel-rename6-300x76.png 300w, https://lemon818.com/wp/wp-content/uploads/2021/06/excel-rename6-768x196.png 768w" sizes="auto, (max-width: 785px) 100vw, 785px" /></p>
<p>&nbsp;</p>
<p>※ なおこのエラーメッセージはH列（非表示列）に入力していますので、H列は消さないようにお願いします！</p>
<p>&nbsp;</p>
<p><img loading="lazy" decoding="async" src="https://lemon818.com/wp/wp-content/uploads/2021/06/excel-rename7.png" alt="" width="461" height="278" class="aligncenter size-full wp-image-5767" srcset="https://lemon818.com/wp/wp-content/uploads/2021/06/excel-rename7.png 461w, https://lemon818.com/wp/wp-content/uploads/2021/06/excel-rename7-300x181.png 300w" sizes="auto, (max-width: 461px) 100vw, 461px" /></p>
<p>&nbsp;</p>
<h3>「一括置換」ボタンでファイル名が変更</h3>
<p>最後に「一括置換」ボタンを押下します。</p>
<p>すると下記のような処理完了メッセージが表示されます。</p>
<p>&nbsp;</p>
<p><img loading="lazy" decoding="async" src="https://lemon818.com/wp/wp-content/uploads/2021/06/excel-rename4.png" alt="" width="883" height="492" class="aligncenter size-full wp-image-5761" srcset="https://lemon818.com/wp/wp-content/uploads/2021/06/excel-rename4.png 883w, https://lemon818.com/wp/wp-content/uploads/2021/06/excel-rename4-300x167.png 300w, https://lemon818.com/wp/wp-content/uploads/2021/06/excel-rename4-768x428.png 768w" sizes="auto, (max-width: 883px) 100vw, 883px" /></p>
<p>&nbsp;</p>
<p>フォルダを確認しに行くと<strong><span style="color: #3366ff;">ファイル名が一括置換されていることが確認できます！</span></strong></p>
<p>&nbsp;</p>
<p><img loading="lazy" decoding="async" src="https://lemon818.com/wp/wp-content/uploads/2021/06/excel-rename5.png" alt="" width="813" height="408" class="aligncenter size-full wp-image-5762" srcset="https://lemon818.com/wp/wp-content/uploads/2021/06/excel-rename5.png 813w, https://lemon818.com/wp/wp-content/uploads/2021/06/excel-rename5-300x151.png 300w, https://lemon818.com/wp/wp-content/uploads/2021/06/excel-rename5-768x385.png 768w" sizes="auto, (max-width: 813px) 100vw, 813px" /></p>
<p>&nbsp;</p>
<p>以上がこのツールの使い方です。</p>
<p>続いてはこのツールの使い方を説明します。</p>
<p>&nbsp;</p>
<div>
<h2>ファイル名を一括置換するツールの作り方（Excel Vba）</h2>
<p><strong><span style="color: #ff0000;">ここからプログラミングの少し難しい話になります！</span></strong></p>
<p>&nbsp;</p>
<p>今回は「<strong>Excelでできる複数のファイル名を一括置換するツール</strong>」を作るうえで下記のような Excel VBA コードを作成しました。</p>
</div>
<p>このコードを Excel の開発タブ→ Visual Basic で表示されるエディタに貼り付ければOKです。</p>
<p>※ 開発タブがない場合は下記ページを参考にしてください。</p>
<div class="sc_getpost"><a class="clearfix" href="https://lemon818.com/vba_start/" ><div class="sc_getpost_thumb post-box-thumbnail__wrap"><img loading="lazy" decoding="async" src="https://lemon818.com/wp/wp-content/uploads/2018/10/8e3b6f7cb40cde9b9f4399d6869f96f9-1.png" width="702" height="356"></div><div class="title">３分でできる！Excel VBA の開発を行うために必要な事前準備</div><div class="date">2018.11.14</div><div class="substr">いまから 「vba を始めたいけれど、Excel で何をどうやってすればいいかわからない」という悩みはありませんか？ 本ページでは vba をどうやってはじめるか３分以内に終わる方法を解説します。 v...</div></a></div>
<p>&nbsp;</p>
<div>
<pre class="crayon-plain-tag">'=================================================================================
' 複数ファイル名を一括置換マクロ
'=================================================================================

Const SEARCH_WORD = "\*.*"

Const CELL_PRINT_COL = 2
Const CELL_PRINT_ROW = 2
Const CELL_SEARCH_WORD = "B3"


' ファイル一覧取得
Sub getFileList()

    Application.ScreenUpdating = False

    Dim folderName
    Dim lastRow
    
    folderName = getFolderName()
    fileList = Dir(folderName &amp; SEARCH_WORD)
    
    ' 最終行取得
    If Cells(CELL_PRINT_ROW + 1, CELL_PRINT_COL) = "" Then
        lastRow = CELL_PRINT_ROW + 1
    Else
        lastRow = Cells(CELL_PRINT_ROW, CELL_PRINT_COL).End(xlDown).Row
    End If
    
    Do While fileList &lt;&gt; ""
                
        Cells(lastRow, CELL_PRINT_COL) = folderName
        Cells(lastRow, CELL_PRINT_COL + 1) = fileList
        lastRow = lastRow + 1
        fileList = Dir()
        
    Loop
    
    Application.ScreenUpdating = True
    
End Sub


' ファイル名を一括置換
Sub replaceFile()

    Application.ScreenUpdating = False

    Dim folderName
    Dim beforeFile
    Dim afterFile
    Dim i
    
    On Error Resume Next
    i = CELL_PRINT_ROW + 1
    
    While Cells(i, CELL_PRINT_COL) &lt;&gt; ""
    
        folderName = Cells(i, CELL_PRINT_COL)
        beforeFile = folderName &amp; "\" &amp; Cells(i, CELL_PRINT_COL + 1)
        afterFile = folderName &amp; "\" &amp; Cells(i, CELL_PRINT_COL + 2)
        
        If check(beforeFile, afterFile) And Cells(i, CELL_PRINT_COL + 3) = "OK" Then
            Name beforeFile As afterFile
            Cells(i, CELL_PRINT_COL + 4) = "OK"
        Else
            Cells(i, CELL_PRINT_COL + 4) = "NG"
        End If
    
        i = i + 1
    
    Wend
    
    Application.ScreenUpdating = True
    
    MsgBox "ファイル名を一括置換しました！"
    
End Sub

' 初期化
Sub reset()

    Application.ScreenUpdating = False
    
    Range(Cells(CELL_PRINT_ROW + 1, CELL_PRINT_COL), Cells(Rows.Count, CELL_PRINT_COL + 2)).ClearContents
    Range(Cells(CELL_PRINT_ROW + 1, CELL_PRINT_COL + 4), Cells(Rows.Count, CELL_PRINT_COL + 4)).ClearContents
     
    Application.ScreenUpdating = True

End Sub


' 入力精査
Private Function check(ByVal beforeFile, ByVal afterFile) As Boolean

    If Dir(beforeFile) &lt;&gt; "" And Dir(afterFile) = "" Then
        check = True
    Else
        check = False
    End If
    
End Function


' ダイアログでフォルダ名取得
Private Function getFolderName()

    Dim folderPath As Variant
    
    With Application.FileDialog(msoFileDialogFolderPicker)
        If .Show = 0 Then
            End
        End If
        folderPath = .SelectedItems(1)
    End With
    
    getFolderName = folderPath
  
End Function</pre>
&nbsp;</p>
<p>とりあえず上をコピーすればOKですが、それだとあまりに味気ないので説明します。</p>
<p>まずこのプログラムは以下のような機能があります。</p>
</div>
<p>&nbsp;</p>
<div class="sc_frame_wrap block blue">
<div class="sc_frame_title">このプログラムの機能</div>
<div class="sc_frame ">
<div class="sc_frame_text">
<div class="sc_designlist ol square solid blue">
<ol>
<li>getFileList() ・・・ （リネームする）ファイル一覧を取得</li>
<li>replaceFile() ・・・ ファイル名を一括置換</li>
<li>reset() ・・・  出力シートを初期化</li>
</ol>
</div>
</div>
</div>
</div>
<p>&nbsp;</p>
<p>今回のツールでの「（リネームする）ファイル一覧を取得」と「ファイル名を一括置換」する方法について説明します。</p>
<p>※ reset（）については割愛します。</p>
<p>&nbsp;</p>
<h3>Excel で（リネームする）ファイル一覧を取得・・・getFileList()</h3>
<p>ファイル一覧を取得する方法は下記になります。</p>
<div class="sc_frame_wrap orange">
<div class="sc_frame ">
<div class="sc_frame_text">
<div class="sc_designlist ol square solid orange">
<ol>
<li>ダイアログでフォルダ名取得</li>
<li>取得したフォルダ名をもとにフォルダ下のファイル一覧を取得</li>
</ol>
</div>
</div>
</div>
</div>
<p>&nbsp;</p>
<p>フォルダ名取得用のダイアログ画面は Application.FileDialog(msoFileDialogFolderPicker)で取得します。</p>
<p>戻り値として選択したフォルダ名（フルパス）で取得できます。</p>
<p>&nbsp;</p>
<p>その取得したフォルダ名から Dir 関数でファイル一覧を取得し、シートに出力するだけです。</p>
<p>&nbsp;</p><pre class="crayon-plain-tag">' ファイル一覧取得
Sub getFileList()

    Application.ScreenUpdating = False

    Dim folderName
    Dim lastRow
    
    folderName = getFolderName()
    fileList = Dir(folderName &amp; SEARCH_WORD)
    
    ' 最終行取得
    If Cells(CELL_PRINT_ROW + 1, CELL_PRINT_COL) = "" Then
        lastRow = CELL_PRINT_ROW + 1
    Else
        lastRow = Cells(CELL_PRINT_ROW, CELL_PRINT_COL).End(xlDown).Row
    End If
    
    Do While fileList &lt;&gt; ""
                
        Cells(lastRow, CELL_PRINT_COL) = folderName
        Cells(lastRow, CELL_PRINT_COL + 1) = fileList
        lastRow = lastRow + 1
        fileList = Dir()
        
    Loop
    
    Application.ScreenUpdating = True
    
End Sub

' ダイアログでフォルダ名取得
Private Function getFolderName()

    Dim folderPath As Variant
    
    With Application.FileDialog(msoFileDialogFolderPicker)
        If .Show = 0 Then
            End
        End If
        folderPath = .SelectedItems(1)
    End With
    
    getFolderName = folderPath
  
End Function</pre><p>
&nbsp;</p>
<p>&nbsp;</p>
<h3>Excel でファイル名を一括置換・・・replaceFile()</h3>
<p>シートに記述されている置換前後のファイル名を取得し、それらを Name 関数で取得します。</p>
<p>&nbsp;</p>
<div class="sc_frame_wrap block orange">
<div class="sc_frame_title">ポイント！</div>
<div class="sc_frame ">
<div class="sc_frame_text">ファイル名の変更 ・・・ Name 置換前のファイル名 As 置換後のファイル名</div>
</div>
</div>
<p>&nbsp;</p>
<p>このリネーム（ファイル名を変更する）処理は<span style="color: #3366ff;"><strong>一行ずつ</strong></span>実行されます。</p>
<p>このファイル名変更処理をシートの表の（B列）に値が実行されるまで実行します。</p>
<p>&nbsp;</p><pre class="crayon-plain-tag">' ファイル名を一括置換
Sub replaceFile()

    Application.ScreenUpdating = False

    Dim folderName
    Dim beforeFile
    Dim afterFile
    Dim i
    
    On Error Resume Next
    i = CELL_PRINT_ROW + 1
    
    While Cells(i, CELL_PRINT_COL) &lt;&gt; ""
    
        folderName = Cells(i, CELL_PRINT_COL)
        beforeFile = folderName &amp; "\" &amp; Cells(i, CELL_PRINT_COL + 1)
        afterFile = folderName &amp; "\" &amp; Cells(i, CELL_PRINT_COL + 2)
        
        If check(beforeFile, afterFile) And Cells(i, CELL_PRINT_COL + 3) = "OK" Then
            Name beforeFile As afterFile
            Cells(i, CELL_PRINT_COL + 4) = "OK"
        Else
            Cells(i, CELL_PRINT_COL + 4) = "NG"
        End If
    
        i = i + 1
    
    Wend
    
    Application.ScreenUpdating = True
    
    MsgBox "ファイル名を一括置換しました！"
    
End Sub

' 入力精査
Private Function check(ByVal beforeFile, ByVal afterFile) As Boolean

    If Dir(beforeFile) &lt;&gt; "" And Dir(afterFile) = "" Then
        check = True
    Else
        check = False
    End If
    
End Function</pre><p>
&nbsp;</p>
<p>また上記処理では入力したファイル名が正しいかどうかを精査する check 関数も実行しております。</p>
<p>この check関数は下記条件すべてを満たす場合に True、それ以外は False を返します。</p>
<p>&nbsp;</p>
<div class="sc_frame_wrap orange">
<div class="sc_frame ">
<div class="sc_frame_text">
<div class="sc_designlist ol square solid orange">
<ol>
<li>置換前のファイル（B列）が存在するか</li>
<li>置換後のファイル（C列）が存在しないか</li>
<li>チェック列（E列）がOKであるか</li>
</ol>
</div>
</div>
</div>
</div>
<p>&nbsp;</p>
<div>
<h2>最後に</h2>
<p>&nbsp;</p>
<p>いかがでしたでしょうか？</p>
<p>この記事では「<strong>Excelでできる複数のファイル名を一括置換するツール</strong>」の使い方と作り方について解説しました。</p>
</div>
<p>&nbsp;</p>
<p>このツールさえあれば<span style="color: #3366ff;"><strong>どれだけファイル数が多くてもファイル名の一括置換がラクラクできます！</strong></span></p>
<div>
<p>この記事が読者の何かのお役に立てれば幸いです。</p>
<p>ではでは。</p>
</div>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>エクセルで遊べるテトリスの作り方【Vba ゲーム】</title>
		<link>https://lemon818.com/excel-tetoris/</link>
		
		<dc:creator><![CDATA[Take]]></dc:creator>
		<pubDate>Wed, 20 Feb 2019 12:12:17 +0000</pubDate>
				<category><![CDATA[Excel]]></category>
		<category><![CDATA[自作ゲーム]]></category>
		<category><![CDATA[pickup]]></category>
		<category><![CDATA[vba]]></category>
		<category><![CDATA[ゲーム]]></category>
		<category><![CDATA[テトリス]]></category>
		<category><![CDATA[マクロ]]></category>
		<guid isPermaLink="false">https://lemon818.com/?p=3830</guid>

					<description><![CDATA[エクセルでテトリスを作りました！！ &#160; 前からつくってみたいなあと思っていたゲームの一つで、これもなかなか時間がかかりました。 仕事終わってからだらだら作っていたら一週間くらいかかりました（工数はだいたい１人日…]]></description>
										<content:encoded><![CDATA[<p><span style="color: #ff6600;"><strong>エクセルでテトリスを作りました！！</strong></span></p>
<p><img loading="lazy" decoding="async" src="https://lemon818.com/wp/wp-content/uploads/2019/02/tetoriasu.gif" alt="" width="474" height="470" class="aligncenter wp-image-4465" /></p>
<p>&nbsp;</p>
<p>前からつくってみたいなあと思っていたゲームの一つで、これもなかなか時間がかかりました。</p>
<p>仕事終わってからだらだら作っていたら一週間くらいかかりました（工数はだいたい１人日～２人日くらい）。</p>
<p>&nbsp;</p>
<p>ですが、思ったより簡単に作れました。エクセルで過去にオセロや将棋などつくりましたが、</p>
<p>毎回思うことがゲーム作りに必要なものは<span style="color: #ff6600;"><strong>根気</strong></span>と<span style="color: #ff6600;"><strong>気合</strong></span>です。</p>
<p>&nbsp;</p>
<p>この記事では、作成したテトリスについて、その操作方法と作成方法についてご紹介します。</p>
<p>※ 作成方法したエクセルのテトリスについては下記からダウンロード可能ですので、ぜひ遊んでみてください。</p>
<p>&nbsp;</p>
<div class="button frame block blue"><a href="https://lemon818.com/wp/wp-content/uploads/2019/02/tetoris.xlsm" class="midium">エクセルのテトリスをダウンロードする</a></div>
<h2>操作方法</h2>
<p>作成したテトリスの操作方法について説明します。</p>
<p>&nbsp;</p>
<h3>ゲームの開始と終了</h3>
<p>&nbsp;</p>
<p>テトリスを開始するときは「START」ボタンをクリックしますが、ゲームを終了する場合はキーボードの「Home」キーを押下します。</p>
<div class="sc_frame_wrap inline blue">
<div class="sc_frame_title">ゲームの開始と終了</div>
<div class="sc_frame ">
<div class="sc_frame_text">
<div class="sc_designlist ol square solid blue">
<ol>
<li>「Start」ボタン : テトリスの開始</li>
<li>キーボードの「Home」 : テトリスの終了</li>
</ol>
</div>
</div>
</div>
</div>
<p><img loading="lazy" decoding="async" src="https://lemon818.com/wp/wp-content/uploads/2019/02/excel-tetoris-2.png" alt="" width="627" height="549" class="aligncenter wp-image-3846" srcset="https://lemon818.com/wp/wp-content/uploads/2019/02/excel-tetoris-2.png 801w, https://lemon818.com/wp/wp-content/uploads/2019/02/excel-tetoris-2-300x263.png 300w, https://lemon818.com/wp/wp-content/uploads/2019/02/excel-tetoris-2-768x672.png 768w" sizes="auto, (max-width: 627px) 100vw, 627px" /></p>
<h3>キーボード操作</h3>
<p>&nbsp;</p>
<div class="sc_frame_wrap inline blue">
<div class="sc_frame_title">操作方法</div>
<div class="sc_frame ">
<div class="sc_frame_text">
<div class="sc_designlist ol square solid blue">
<ol>
<li>「←」「↑」「↓」「→」キー : ブロックの移動</li>
<li>「Ctrl」キー : ブロックの回転</li>
<li>「Home」キー : テトリスの終了</li>
</ol>
</div>
</div>
</div>
</div>
<p>&nbsp;</p>
<p>後は、いつものテトリスです。ブロックを一行そろえて消すだけです。</p>
<p>またテトリスには、一行ずつ消すよりもまとめて消したほうがお得というルールがありますね。今回それを下記式で表しています。</p>
<p>&nbsp;</p>
<div class="sc_frame_wrap inline blue">
<div class="sc_frame ">
<div class="sc_frame_text">Score = Score + 100 + [消した行の数] * 300</div>
</div>
</div>
<p>&nbsp;</p>
<p>テトリスを作った割にテトリスのポイントのつけ方がよくわからなかったので、こんな感じで付けました。</p>
<p>Score とは点数のことで、「Score = Score + ~ 」とは、もともとの Score の値にどんどん足していきますよということです。</p>
<p>例えば削除した行が１行なら Score = 400点、2行なら700点つきます（答えを知っている人は教えてほしいです）。</p>
<p>&nbsp;</p>
<h3>終了条件</h3>
<p>&nbsp;</p>
<p>前述しましたがゲームを終了する場合はキーボードの「Home」キーを押下しますが、それ以外にもテトリスが続行できない場合も終了するようにしています。</p>
<p>要はブロックが天井を突き抜けた状態です。このときは強制的にテトリスが終了します。</p>
<p>&nbsp;</p>
<p><img loading="lazy" decoding="async" src="https://lemon818.com/wp/wp-content/uploads/2019/02/excel-tetoris-3.png" alt="" width="664" height="513" class="aligncenter wp-image-3847" srcset="https://lemon818.com/wp/wp-content/uploads/2019/02/excel-tetoris-3.png 806w, https://lemon818.com/wp/wp-content/uploads/2019/02/excel-tetoris-3-300x232.png 300w, https://lemon818.com/wp/wp-content/uploads/2019/02/excel-tetoris-3-768x594.png 768w" sizes="auto, (max-width: 664px) 100vw, 664px" /></p>
<h2>作成方法</h2>
<p>&nbsp;</p>
<p>Excel vba で作成しています。要はプログラミングして作成しました。</p>
<p>実際のテトリスの作成手順は下記の５つになります。<span style="color: #ff6600;"><strong>この５つができればテトリスは作れます。</strong></span></p>
<p>&nbsp;</p>
<div class="sc_frame_wrap inline blue">
<div class="sc_frame_title">作成手順</div>
<div class="sc_frame ">
<div class="sc_frame_text">
<div class="sc_designlist ol square solid blue">
<ol>
<li>下準備（定数と変数の定義 &amp; レイアウトの作成）</li>
<li>ブロックを描く</li>
<li>ブロックの移動、回転</li>
<li>ブロックが一行並べば消す</li>
<li>テトリスの終了条件をつける</li>
</ol>
</div>
</div>
</div>
</div>
<p>&nbsp;</p>
<p>これだけみれば、「案外簡単そうだなあ」と思われる人もいるかもしれませんが、案外めんどくさいです。</p>
<p>特にめんどくさいのが、ブロックの移動、回転の処理です。では順をおって説明します。</p>
<p>&nbsp;</p>
<h3>メイン関数</h3>
<p>&nbsp;</p>
<p>作成したテトリスのメイン関数です。</p>
<p>処理概要を説明しますと、まず定数、変数の定義とレイアウト作成などの下準備を行います。</p>
<p>その後 Do While True ~ をつかって無限ループさせ、ブロックを落とすようにします。</p>
<p>その無限ループの処理の中で下記の処理を行います。</p>
<p>&nbsp;</p>
<div class="sc_frame_wrap inline blue">
<div class="sc_frame_title">無限ループ内処理</div>
<div class="sc_frame ">
<div class="sc_frame_text">
<div class="sc_designlist ol square solid blue">
<ol>
<li>0.3 秒ごとに一行したにブロックが移動</li>
<li>その0.3秒のうちの0.1秒ごとにキーボードのイベントを取得</li>
<li>ブロックが一番下までいけば、一行そろっているブロックがあるか確認し、あれば消してスコアをつける</li>
<li>その後、ブロックが天井より上かどうか確認（ゲームオーバーであるか）</li>
</ol>
</div>
</div>
</div>
</div>
<p>&nbsp;</p>
<p>これだけです。天井という言い方が正しいかはわかりませんが、</p>
<p>ブロックが指定したセルの行よりあれば終了するようにしています（先ほど貼ったやつです）。</p>
<p>&nbsp;</p>
<p><img loading="lazy" decoding="async" src="https://lemon818.com/wp/wp-content/uploads/2019/02/excel-tetoris-3.png" alt="" width="591" height="457" class="aligncenter wp-image-3847" srcset="https://lemon818.com/wp/wp-content/uploads/2019/02/excel-tetoris-3.png 806w, https://lemon818.com/wp/wp-content/uploads/2019/02/excel-tetoris-3-300x232.png 300w, https://lemon818.com/wp/wp-content/uploads/2019/02/excel-tetoris-3-768x594.png 768w" sizes="auto, (max-width: 591px) 100vw, 591px" /></p>
<h5>ソースコード</h5>
<pre class="crayon-plain-tag">'メイン
Sub Tetris()
    
    Dim x As Integer
    Dim y As Integer
    Dim n As Integer
    Dim i As Integer
    Dim Block As Integer: Block = 0
    Dim Next_Block As Integer: Next_Block = 0
    
    ' 初期化
    Call setting(x, y)
    
    Do While True
        x = START_X
        y = START_Y
        ROTATE_NUM = 0
        
        Call setBlock(Block, Next_Block)
    
        For y = START_Y To BOTTOM_Y
            For n = 1 To 3
                Call setEvent(x, y, Block)
                Application.Wait [Now() +  "00:00:00.10"]
            Next n
            
            ' ブロックを移動できない状態ならブロックを削除
            If DropBlock(x, y, Block) = False Then
                Call checkRowBlock
                Exit For
            End If
        Next y
        Call checkgame
    Loop
    
End Sub</pre>
&nbsp;</p>
<p>次にメイン関数の処理をプロシージャにわけて説明します。</p>
<p>&nbsp;</p>
<h3>下準備（定数と変数の定義 &amp; レイアウトの作成）</h3>
<p>&nbsp;</p>
<h4>定数と変数の定義</h4>
<p>&nbsp;</p>
<p>まず初めに定数と変数の定義します。</p>
<p>記事作成での都合上、最初に定数と変数の定義を最初にきていますが、プログラミングをしている段階では、必要なたびにどんどん定義しています（当たり前ですが、）。</p>
<p>&nbsp;</p>
<p>定数については、レイアウトの位置情報（何行何列目のセルに描くか）とブロックの色について定義づけしています（固定なので）。</p>
<p>また、変数については、ブロックの先頭セルと、テトリスのスコア、ブロックの回転数について定義しています。これらは後で説明しますのでいまは適当に流していただいて問題ありません。</p>
<p>&nbsp;</p>
<h5>ソースコード</h5>
<pre class="crayon-plain-tag">'シート名
Public Const TETRIS_SHEET As String = "Tetris"

'テトリス位置
Public Const BOTTOM_Y As Integer = 27
Public Const BOTTOM_X_LEFT As Integer = 3
Public Const BOTTOM_X_RIGHT As Integer = 15
Public Const START_X As Integer = 8
Public Const START_Y As Integer = 4

'ブロックの先頭位置
Public TOP_X_L As Integer
Public TOP_X_R As Integer
Public TOP_Y As Integer

'ブロックの先頭位置
Public Const NEXT_START_X As Integer = 23
Public Const NEXT_START_Y As Integer = 7

' スコア
Public Const SCORE_POSITION_X As Integer = 24
Public Const SCORE_POSITION_Y As Integer = 12
Public Score As Integer

' 回転数
Public ROTATE_NUM As Integer

'色の定義 0 → 白 | 1 → 黒
Public Const NO_COLOR As Integer = 0
Public Const BLOCK1_COLOR As Integer = 3
Public Const BLOCK2_COLOR As Integer = 8
Public Const BLOCK3_COLOR As Integer = 5
Public Const BLOCK4_COLOR As Integer = 50
Public Const BLOCK5_COLOR As Integer = 46
Public Const BLOCK6_COLOR As Integer = 29
Public Const BLOCK7_COLOR As Integer = 27</pre>
&nbsp;</p>
<h4>テトリスのレイアウト</h4>
<p>テトリスの枠のレイアウトを vba で書くようにします。</p>
<p>これはもし間違えてシート上のセルを消してしまった場合も問題ないようにするためです。</p>
<p>作成するレイアウトは、下記画像の<span style="color: #3366ff;"><strong>青矢印</strong></span>の箇所（テトリスの周りのブロックと、次のテトリスを表示させる黒枠の箇所）です。</p>
<p>&nbsp;</p>
<p><img loading="lazy" decoding="async" src="https://lemon818.com/wp/wp-content/uploads/2019/02/excel-tetoris-4.png" alt="" width="456" height="370" class="aligncenter wp-image-3852" srcset="https://lemon818.com/wp/wp-content/uploads/2019/02/excel-tetoris-4.png 687w, https://lemon818.com/wp/wp-content/uploads/2019/02/excel-tetoris-4-300x244.png 300w" sizes="auto, (max-width: 456px) 100vw, 456px" /></p>
<p>&nbsp;</p>
<p>また、もともとブロックが描いてある場合もいったん消してきれいにするようにしています。</p>
<p>下記はソースコードですが、setting プロシージャがメイン関数で、その中に ①テトリスの周りのブロックと、②次のテトリスを表示させる黒枠の箇所のレイアウトの設定を行っています。</p>
<p>&nbsp;</p>
<h5>ソースコード</h5>
<pre class="crayon-plain-tag">Option Explicit

' 初期化
Sub setting(ByRef x As Integer, ByRef y As Integer)

    x = START_X
    y = START_Y
    Score = 0
    ROTATE_NUM = 0
    
    Application.ScreenUpdating = False
    
    Call settingTetris

    Call settingNextBlock
    
    Worksheets(TETRIS_SHEET).Cells(SCORE_POSITION_Y, SCORE_POSITION_X).Value = Score

End Sub

Private Sub settingTetris()

    Application.ScreenUpdating = False
    
    With Worksheets(TETRIS_SHEET).Range(Cells(START_Y, BOTTOM_X_LEFT), Cells(BOTTOM_Y, BOTTOM_X_LEFT))
        .Interior.color = RGB(192, 192, 192)
        .Borders.LineStyle = xlContinuous
    End With
    
    With Worksheets(TETRIS_SHEET).Range(Cells(START_Y, BOTTOM_X_RIGHT), Cells(BOTTOM_Y, BOTTOM_X_RIGHT))
        .Interior.color = RGB(192, 192, 192)
        .Borders.LineStyle = xlContinuous
    End With

    With Worksheets(TETRIS_SHEET).Range(Cells(BOTTOM_Y, BOTTOM_X_LEFT), Cells(BOTTOM_Y, BOTTOM_X_RIGHT))
        .Interior.color = RGB(192, 192, 192)
        .Borders.LineStyle = xlContinuous
    End With

    Worksheets(TETRIS_SHEET).Range(Cells(1, BOTTOM_X_LEFT + 1), Cells(BOTTOM_Y - 1, BOTTOM_X_RIGHT - 1)).ClearFormats
    
    Application.ScreenUpdating = True

End Sub

Sub settingNextBlock()

    Application.ScreenUpdating = False
    
    Worksheets(TETRIS_SHEET).Range(Cells(START_Y + 1, NEXT_START_X - 1), Cells(START_Y + 4, NEXT_START_X + 3)).ClearFormats
    
    Application.ScreenUpdating = True
    
End Sub</pre>
&nbsp;</p>
<h3>ブロックを描く</h3>
<p>&nbsp;</p>
<p>テトリスのブロックは下記の７種類あるそうです。なので、これを描くようにします。</p>
<p>&nbsp;</p>
<p><img loading="lazy" decoding="async" src="https://lemon818.com/wp/wp-content/uploads/2019/02/excel-tetoris-5.png" alt="" width="522" height="116" class="aligncenter wp-image-3861" srcset="https://lemon818.com/wp/wp-content/uploads/2019/02/excel-tetoris-5.png 624w, https://lemon818.com/wp/wp-content/uploads/2019/02/excel-tetoris-5-300x67.png 300w" sizes="auto, (max-width: 522px) 100vw, 522px" /></p>
<p>&nbsp;</p>
<p>今回これらのブロックに名前をつけています。左から１、２ .... ７という数値をつけそれを Block という変数にもたせるようにしています。</p>
<p>ブロックの描き方についてはただセルに色と枠線をつけるだけです。</p>
<p>また、セルに入れる処理を毎回書くのが面倒だったので、paintColor プロシージャにまとめ、座標情報（何行何列目に入力するか）のみで描けるようにしてあります。</p>
<p>&nbsp;</p>
<h5>ソースコード</h5>
<pre class="crayon-plain-tag">Option Explicit

' ランダムにブロックを取得
Sub getRandBlock(ByVal x As Integer, ByVal y As Integer, ByRef Block As Integer)
    
    Select Case Block
        Case 1
            Call makeBlock1(x, y)
        Case 2
            Call makeBlock2(x, y)
        Case 3
            Call makeBlock3(x, y)
        Case 4
            Call makeBlock4(x, y)
        Case 5
            Call makeBlock5(x, y)
        Case 6
            Call makeBlock6(x, y)
        Case 7
            Call makeBlock7(x, y)
    End Select

    Call setTopPlace(x, y, Block)

End Sub

' 正方形　ブロック作成
Private Sub makeBlock1(ByVal x As Integer, ByVal y As Integer)
    
    Const color As Integer = BLOCK1_COLOR
    
    Call paintColor(color, x + 1, y)
    Call paintColor(color, x + 2, y)
    Call paintColor(color, x + 1, y - 1)
    Call paintColor(color, x + 2, y - 1)
    
End Sub

' 長方形　ブロック作成
Private Sub makeBlock2(ByVal x As Integer, ByVal y As Integer)
    
    Const color As Integer = BLOCK2_COLOR
    
    If ROTATE_NUM = 0 Then
        Call paintColor(color, x + 2, y)
        Call paintColor(color, x + 2, y - 1)
        Call paintColor(color, x + 2, y - 2)
        Call paintColor(color, x + 2, y - 3)
    Else
        Call paintColor(color, x, y - 2)
        Call paintColor(color, x + 1, y - 2)
        Call paintColor(color, x + 2, y - 2)
        Call paintColor(color, x + 3, y - 2)
    End If
    
End Sub

' L字　ブロック作成
Private Sub makeBlock3(ByVal x As Integer, ByVal y As Integer)
    
    Const color As Integer = BLOCK3_COLOR
    
    If ROTATE_NUM = 1 Then
        Call paintColor(color, x, y)
        Call paintColor(color, x, y - 1)
        Call paintColor(color, x, y - 2)
        Call paintColor(color, x + 1, y - 2)
    ElseIf ROTATE_NUM = 2 Then
        Call paintColor(color, x, y - 1)
        Call paintColor(color, x + 1, y - 1)
        Call paintColor(color, x + 2, y - 1)
        Call paintColor(color, x + 2, y)
    ElseIf ROTATE_NUM = 3 Then
        Call paintColor(color, x, y)
        Call paintColor(color, x + 1, y)
        Call paintColor(color, x + 1, y - 1)
        Call paintColor(color, x + 1, y - 2)
    Else
        Call paintColor(color, x, y)
        Call paintColor(color, x + 1, y)
        Call paintColor(color, x + 2, y)
        Call paintColor(color, x, y - 1)
    End If
    
End Sub

' 逆L字　ブロック作成
Private Sub makeBlock4(ByVal x As Integer, ByVal y As Integer)
    
    Const color As Integer = BLOCK4_COLOR
    
    If ROTATE_NUM = 1 Then
        Call paintColor(color, x, y)
        Call paintColor(color, x, y - 1)
        Call paintColor(color, x, y - 2)
        Call paintColor(color, x + 1, y)
    ElseIf ROTATE_NUM = 2 Then
        Call paintColor(color, x, y)
        Call paintColor(color, x, y - 1)
        Call paintColor(color, x + 1, y - 1)
        Call paintColor(color, x + 2, y - 1)
    ElseIf ROTATE_NUM = 3 Then
        Call paintColor(color, x, y - 2)
        Call paintColor(color, x + 1, y)
        Call paintColor(color, x + 1, y - 1)
        Call paintColor(color, x + 1, y - 2)
    Else
        Call paintColor(color, x, y)
        Call paintColor(color, x + 1, y)
        Call paintColor(color, x + 2, y)
        Call paintColor(color, x + 2, y - 1)
    End If
    
End Sub

' 山　ブロック作成
Private Sub makeBlock5(ByVal x As Integer, ByVal y As Integer)
    
    Const color As Integer = BLOCK5_COLOR
    
    If ROTATE_NUM = 1 Then
        Call paintColor(color, x + 1, y)
        Call paintColor(color, x + 1, y - 1)
        Call paintColor(color, x + 1, y - 2)
        Call paintColor(color, x + 2, y - 1)
    ElseIf ROTATE_NUM = 2 Then
        Call paintColor(color, x, y - 1)
        Call paintColor(color, x + 1, y - 1)
        Call paintColor(color, x + 2, y - 1)
        Call paintColor(color, x + 1, y)
    ElseIf ROTATE_NUM = 3 Then
        Call paintColor(color, x, y - 1)
        Call paintColor(color, x + 1, y)
        Call paintColor(color, x + 1, y - 1)
        Call paintColor(color, x + 1, y - 2)
    Else
        Call paintColor(color, x, y - 1)
        Call paintColor(color, x + 1, y - 1)
        Call paintColor(color, x + 1, y - 2)
        Call paintColor(color, x + 2, y - 1)
    End If
    
End Sub

' Z字　ブロック作成
Private Sub makeBlock6(ByVal x As Integer, ByVal y As Integer)
    
    Const color As Integer = BLOCK6_COLOR
    
    If ROTATE_NUM = 1 Then
        Call paintColor(color, x, y)
        Call paintColor(color, x, y - 1)
        Call paintColor(color, x + 1, y - 1)
        Call paintColor(color, x + 1, y - 2)
    Else
        Call paintColor(color, x, y - 1)
        Call paintColor(color, x + 1, y)
        Call paintColor(color, x + 1, y - 1)
        Call paintColor(color, x + 2, y)
    End If
    
End Sub

' 逆Z字　ブロック作成
Private Sub makeBlock7(ByVal x As Integer, ByVal y As Integer)
    
    Const color As Integer = BLOCK7_COLOR
    
    If ROTATE_NUM = 1 Then
        Call paintColor(color, x, y - 1)
        Call paintColor(color, x, y - 2)
        Call paintColor(color, x + 1, y)
        Call paintColor(color, x + 1, y - 1)
    Else
        Call paintColor(color, x, y)
        Call paintColor(color, x + 1, y)
        Call paintColor(color, x + 1, y - 1)
        Call paintColor(color, x + 2, y - 1)
    End If
    
End Sub

' セルの色つける
Private Sub paintColor(ByVal color As Integer, ByVal x As Integer, ByVal y As Integer)

    Application.ScreenUpdating = False
    
    With Worksheets(TETRIS_SHEET).Cells(y, x)
        .Borders.LineStyle = xlContinuous
        .Interior.ColorIndex = color
    End With
    Application.ScreenUpdating = True
    
End Sub</pre>
そしてブロックを描く機能をつくれば、今度はそのブロックをランダムに取得するようにします。</p>
<p>具体的には 1 ~ 7 のうちのランダムな数値を取得し、取得した値からブロックを生成するようにします。</p>
<p>例えば 3 という数値が取得したらブロック３を出力するようにします。</p>
<h5>ソースコード</h5>
<pre class="crayon-plain-tag">' 落とすブロックのセット
Private Sub setBlock(ByRef Block As Integer, ByRef Next_Block As Integer)
        
    Randomize
    
    Call settingNextBlock

    If Block = 0 Then
        Block = Int(7 * Rnd + 1)
    Else
        Block = Next_Block
    End If

    Next_Block = Int(7 * Rnd + 1)
    
    Select Case Next_Block
        Case 1
            Call getRandBlock(NEXT_START_X - 1, NEXT_START_Y, Next_Block)
        Case 2
            Call getRandBlock(NEXT_START_X - 1, NEXT_START_Y + 1, Next_Block)
        Case 5
            Call getRandBlock(NEXT_START_X, NEXT_START_Y + 1, Next_Block)
        Case Else
            Call getRandBlock(NEXT_START_X, NEXT_START_Y, Next_Block)
    End Select
    
    Call getRandBlock(START_X, START_Y, Block)
    
End Sub</pre>
&nbsp;</p>
<p>またブロックの削除処理もつくっています。</p>
<p>これはブロックを移動させるときに必要な処理です（後で説明します）。</p>
<p>ブロックの削除の仕方ですが、簡単に指定した行と列の書式を削除しているだけです。</p>
<p>&nbsp;</p>
<h5>ソースコード</h5>
<pre class="crayon-plain-tag">Option Explicit

' ランダムにブロックを取得
Sub deleteRandBlock(ByVal x As Integer, ByVal y As Integer, ByRef Block As Integer)
    
    Select Case Block
        Case 1
            Call deleteBlock1(x, y)
        Case 2
            Call deleteBlock2(x, y)
        Case 3
            Call deleteBlock3(x, y)
        Case 4
            Call deleteBlock4(x, y)
        Case 5
            Call deleteBlock5(x, y)
        Case 6
            Call deleteBlock6(x, y)
        Case 7
            Call deleteBlock7(x, y)
    End Select

End Sub

' 正方形　ブロック削除
Private Sub deleteBlock1(ByVal x As Integer, ByVal y As Integer)
    
    Const color As Integer = 3
    
    Call deleteFormat(x + 1, y)
    Call deleteFormat(x + 2, y)
    Call deleteFormat(x + 1, y - 1)
    Call deleteFormat(x + 2, y - 1)
    
End Sub

' 長方形　ブロック削除
Private Sub deleteBlock2(ByVal x As Integer, ByVal y As Integer)
    
    Const color As Integer = 8
    
    If ROTATE_NUM = 0 Then
        Call deleteFormat(x + 2, y)
        Call deleteFormat(x + 2, y - 1)
        Call deleteFormat(x + 2, y - 2)
        Call deleteFormat(x + 2, y - 3)
    Else
        Call deleteFormat(x, y - 2)
        Call deleteFormat(x + 1, y - 2)
        Call deleteFormat(x + 2, y - 2)
        Call deleteFormat(x + 3, y - 2)
    End If
    
End Sub

' L字　ブロック削除
Private Sub deleteBlock3(ByVal x As Integer, ByVal y As Integer)
    
    Const color As Integer = 5
    
    If ROTATE_NUM = 1 Then
        Call deleteFormat(x, y)
        Call deleteFormat(x, y - 1)
        Call deleteFormat(x, y - 2)
        Call deleteFormat(x + 1, y - 2)
    ElseIf ROTATE_NUM = 2 Then
        Call deleteFormat(x, y - 1)
        Call deleteFormat(x + 1, y - 1)
        Call deleteFormat(x + 2, y - 1)
        Call deleteFormat(x + 2, y)
    ElseIf ROTATE_NUM = 3 Then
        Call deleteFormat(x, y)
        Call deleteFormat(x + 1, y)
        Call deleteFormat(x + 1, y - 1)
        Call deleteFormat(x + 1, y - 2)
    Else
        Call deleteFormat(x, y)
        Call deleteFormat(x + 1, y)
        Call deleteFormat(x + 2, y)
        Call deleteFormat(x, y - 1)
    End If
    
End Sub

' 逆L字　ブロック削除
Private Sub deleteBlock4(ByVal x As Integer, ByVal y As Integer)
    
    Const color As Integer = 50
    
    If ROTATE_NUM = 1 Then
        Call deleteFormat(x, y)
        Call deleteFormat(x, y - 1)
        Call deleteFormat(x, y - 2)
        Call deleteFormat(x + 1, y)
    ElseIf ROTATE_NUM = 2 Then
        Call deleteFormat(x, y)
        Call deleteFormat(x, y - 1)
        Call deleteFormat(x + 1, y - 1)
        Call deleteFormat(x + 2, y - 1)
    ElseIf ROTATE_NUM = 3 Then
        Call deleteFormat(x, y - 2)
        Call deleteFormat(x + 1, y)
        Call deleteFormat(x + 1, y - 1)
        Call deleteFormat(x + 1, y - 2)
    Else
        Call deleteFormat(x, y)
        Call deleteFormat(x + 1, y)
        Call deleteFormat(x + 2, y)
        Call deleteFormat(x + 2, y - 1)
    End If
    
End Sub

' 山　ブロック削除
Private Sub deleteBlock5(ByVal x As Integer, ByVal y As Integer)
    
    Const color As Integer = 46
    
    If ROTATE_NUM = 1 Then
        Call deleteFormat(x + 1, y)
        Call deleteFormat(x + 1, y - 1)
        Call deleteFormat(x + 1, y - 2)
        Call deleteFormat(x + 2, y - 1)
    ElseIf ROTATE_NUM = 2 Then
        Call deleteFormat(x, y - 1)
        Call deleteFormat(x + 1, y - 1)
        Call deleteFormat(x + 2, y - 1)
        Call deleteFormat(x + 1, y)
    ElseIf ROTATE_NUM = 3 Then
        Call deleteFormat(x, y - 1)
        Call deleteFormat(x + 1, y)
        Call deleteFormat(x + 1, y - 1)
        Call deleteFormat(x + 1, y - 2)
    Else
        Call deleteFormat(x, y - 1)
        Call deleteFormat(x + 1, y - 1)
        Call deleteFormat(x + 1, y - 2)
        Call deleteFormat(x + 2, y - 1)
    End If
    
End Sub

' Z字　ブロック削除
Private Sub deleteBlock6(ByVal x As Integer, ByVal y As Integer)
    
    Const color As Integer = 29
    
    If ROTATE_NUM = 1 Then
        Call deleteFormat(x, y)
        Call deleteFormat(x, y - 1)
        Call deleteFormat(x + 1, y - 1)
        Call deleteFormat(x + 1, y - 2)
    Else
        Call deleteFormat(x, y - 1)
        Call deleteFormat(x + 1, y)
        Call deleteFormat(x + 1, y - 1)
        Call deleteFormat(x + 2, y)
    End If
    
End Sub

' 逆Z字　ブロック削除
Private Sub deleteBlock7(ByVal x As Integer, ByVal y As Integer)
    
    Const color As Integer = 27
    
    If ROTATE_NUM = 1 Then
        Call deleteFormat(x, y - 1)
        Call deleteFormat(x, y - 2)
        Call deleteFormat(x + 1, y)
        Call deleteFormat(x + 1, y - 1)
    Else
        Call deleteFormat(x, y)
        Call deleteFormat(x + 1, y)
        Call deleteFormat(x + 1, y - 1)
        Call deleteFormat(x + 2, y - 1)
    End If
    
End Sub

' セルの書式の削除
Private Sub deleteFormat(ByVal x As Integer, ByVal y As Integer)
    
    Application.ScreenUpdating = False
    With Worksheets(TETRIS_SHEET).Cells(y, x)
        .ClearFormats
    End With
    Application.ScreenUpdating = True
End Sub</pre>
&nbsp;</p>
<h3>ブロックの移動、回転</h3>
<p>&nbsp;</p>
<p>ブロックの移動は「←」「↑」「↓」「→」のキーボード入力で行っています。</p>
<p>「←」「↑」「↓」「→」のキーボード入力のイベントを検知したときに、下記プロシージャが呼び出されるようにしてあります。</p>
<p>ブロックの移動についてですが、移動前のブロックの書式を削除→次のブロックに色をつけるというふたつの処理を行っています。</p>
<p>前章「ブロックを描く」で説明しました、「ブロックを描く」処理と「ブロックを削除」処理の二つを呼び出して処理させています。</p>
<p>またブロックを移動させるときの注意点は<span style="color: #ff6600;"><strong>移動範囲</strong></span>です。</p>
<p>当然ですがブロックが壁を貫いたり、底を突き抜けて動いていたら<strong>テトリスになりません。</strong></p>
<p>&nbsp;</p>
<p><img loading="lazy" decoding="async" src="https://lemon818.com/wp/wp-content/uploads/2019/02/excel-tetoris-6.png" alt="" width="259" height="449" class="aligncenter wp-image-3862" srcset="https://lemon818.com/wp/wp-content/uploads/2019/02/excel-tetoris-6.png 425w, https://lemon818.com/wp/wp-content/uploads/2019/02/excel-tetoris-6-173x300.png 173w" sizes="auto, (max-width: 259px) 100vw, 259px" /></p>
<p>&nbsp;</p>
<p>ですので今回はブロックごとに<strong>先頭セル</strong>をもたせてあり、<strong>先頭セル</strong>の次の行ももしくは列のセルの色が白色（背景色）でならブロックが移動できるようにしています。</p>
<h5>ソースコード</h5>
<pre class="crayon-plain-tag">Option Explicit

#If VBA7 Then
'32bit PC
Private Declare Sub Sleep Lib "kernel32.dll" (ByVal ms As Long)
Private Declare Function GetAsyncKeyState Lib "user32.dll" (ByVal vKey As Long) As Long
#Else
'64bit PC
Private Declare PtrSafe Sub Sleep Lib "kernel32.dll" (ByVal ms As LongPtr)
Private Declare PtrSafe Function GetAsyncKeyState Lib "user32.dll" (ByVal vKey As LongPtr) As Long
#End If

' キーボードイベント検知
Sub setEvent(ByRef x As Integer, ByRef y As Integer, ByRef Block As Integer)
    ' 左矢印
    If GetAsyncKeyState(vbKeyLeft) &lt;&gt; 0 Then
        Call moveLeft(x, y, Block)
    End If
    ' 右矢印
    If GetAsyncKeyState(vbKeyRight) &lt;&gt; 0 Then
        Call moveRight(x, y, Block)
    End If
    ' 下矢印
    If GetAsyncKeyState(vbKeyDown) &lt;&gt; 0 Then
        Call moveDown(x, y, Block)
    End If
    ' Ctrl 回転
    If GetAsyncKeyState(vbKeyControl) &lt;&gt; 0 Then
        Call rotateBlock(x, y, Block)
    End If
    ' Homeボタン 終了
    If GetAsyncKeyState(vbKeyHome) &lt;&gt; 0 Then
        MsgBox "Game Fin!" &amp; Chr(13) &amp; "Score : " &amp; Score
        End
    End If
End Sub

' 右に移動
Private Sub moveRight(ByRef x As Integer, ByRef y As Integer, ByRef Block As Integer)
    
    If TOP_X_R &lt;= BOTTOM_X_RIGHT And checkColor_Right(TOP_X_R, y, Block) = True Then
        Call deleteRandBlock(x, y, Block)
        x = x + 1
        Call getRandBlock(x, y, Block)
    End If
End Sub

' 左に移動
Private Sub moveLeft(ByRef x As Integer, ByRef y As Integer, ByRef Block As Integer)
    
    If TOP_X_L &gt; BOTTOM_X_LEFT And checkColor_Left(TOP_X_L, y, Block) = True Then
        Call deleteRandBlock(x, y, Block)
        x = x - 1
        Call getRandBlock(x, y, Block)
    End If
End Sub

' 下に移動
Private Sub moveDown(ByRef x As Integer, ByRef y As Integer, ByRef Block As Integer)
    
    If TOP_Y &lt; BOTTOM_Y And checkColor_Down(x, TOP_Y, Block) = True Then
        Call deleteRandBlock(x, y, Block)
        y = y + 1
        Call getRandBlock(x, y, Block)
    End If
End Sub

' 自然にブロックが落ちる動き
Function DropBlock(ByRef x As Integer, ByRef y As Integer, ByRef Block As Integer) As Boolean

    If TOP_Y &lt; BOTTOM_Y And checkColor_Down(TOP_X_R, TOP_Y, Block) = True Then
        Call deleteRandBlock(x, y, Block)
        Call getRandBlock(x, y + 1, Block)
        DropBlock = True
    Else
        DropBlock = False
    End If

    
End Function

' 隣の色の確認 置ける箇所ならTrue 左方向
Function checkColor_Left(ByVal x As Integer, ByVal y As Integer, ByRef Block As Integer) As Boolean

    checkColor_Left = False
    
    Select Case Block
        Case 1
            If checkNoColor(TOP_X_L - 1, TOP_Y) = True _
            And checkNoColor(TOP_X_L - 1, TOP_Y - 1) = True _
            Then
                checkColor_Left = True
            End If
        Case 2
            Select Case ROTATE_NUM
                Case 0
                    If checkNoColor(TOP_X_L - 1, TOP_Y) = True _
                    And checkNoColor(TOP_X_L - 1, TOP_Y - 1) = True _
                    And checkNoColor(TOP_X_L - 1, TOP_Y - 2) = True _
                    And checkNoColor(TOP_X_L - 1, TOP_Y - 3) = True _
                    Then
                        checkColor_Left = True
                    End If
                Case 1
                    If checkNoColor(TOP_X_L - 1, TOP_Y) = True _
                    Then
                        checkColor_Left = True
                    End If
            End Select
        Case 3
            Select Case ROTATE_NUM
                Case 0
                    If checkNoColor(TOP_X_L - 1, TOP_Y) = True _
                    And checkNoColor(TOP_X_L - 1, TOP_Y - 1) = True _
                    Then
                        checkColor_Left = True
                    End If
                Case 1
                    If checkNoColor(TOP_X_L - 1, TOP_Y) = True _
                    And checkNoColor(TOP_X_L - 1, TOP_Y - 1) = True _
                    And checkNoColor(TOP_X_L - 1, TOP_Y - 2) = True _
                    Then
                        checkColor_Left = True
                    End If
                Case 2
                    If checkNoColor(TOP_X_L + 1, TOP_Y) = True _
                    And checkNoColor(TOP_X_L - 1, TOP_Y - 1) = True _
                    Then
                        checkColor_Left = True
                    End If
                Case 3
                    If checkNoColor(TOP_X_L - 1, TOP_Y) = True _
                    And checkNoColor(TOP_X_L, TOP_Y - 1) = True _
                    And checkNoColor(TOP_X_L, TOP_Y - 2) = True _
                    Then
                        checkColor_Left = True
                    End If
            End Select
        Case 4
            Select Case ROTATE_NUM
                Case 0
                    If checkNoColor(TOP_X_L - 1, TOP_Y) = True _
                    And checkNoColor(TOP_X_L + 1, TOP_Y - 1) = True _
                    Then
                        checkColor_Left = True
                    End If
                Case 1
                    If checkNoColor(TOP_X_L - 1, TOP_Y) = True _
                    And checkNoColor(TOP_X_L - 1, TOP_Y - 1) = True _
                    And checkNoColor(TOP_X_L - 1, TOP_Y - 2) = True _
                    Then
                        checkColor_Left = True
                    End If
                Case 2
                    If checkNoColor(TOP_X_L - 1, TOP_Y) = True _
                    And checkNoColor(TOP_X_L - 1, TOP_Y - 1) = True _
                    Then
                        checkColor_Left = True
                    End If
                Case 3
                    If checkNoColor(TOP_X_L, TOP_Y) = True _
                    And checkNoColor(TOP_X_L, TOP_Y - 1) = True _
                    And checkNoColor(TOP_X_L - 1, TOP_Y - 2) = True _
                    Then
                        checkColor_Left = True
                    End If
            End Select
        Case 5
            Select Case ROTATE_NUM
                Case 0
                    If checkNoColor(TOP_X_L - 1, TOP_Y) = True _
                    And checkNoColor(TOP_X_L, TOP_Y - 1) = True _
                    Then
                        checkColor_Left = True
                    End If
                Case 1
                    If checkNoColor(TOP_X_L - 1, TOP_Y) = True _
                    And checkNoColor(TOP_X_L - 1, TOP_Y - 1) = True _
                    And checkNoColor(TOP_X_L - 1, TOP_Y - 2) = True _
                    Then
                        checkColor_Left = True
                    End If
                Case 2
                    If checkNoColor(TOP_X_L, TOP_Y) = True _
                    And checkNoColor(TOP_X_L - 1, TOP_Y - 1) = True _
                    Then
                        checkColor_Left = True
                    End If
                Case 3
                    If checkNoColor(TOP_X_L, TOP_Y) = True _
                    And checkNoColor(TOP_X_L - 1, TOP_Y - 1) = True _
                    And checkNoColor(TOP_X_L, TOP_Y - 2) = True _
                    Then
                        checkColor_Left = True
                    End If
            End Select
        Case 6
            Select Case ROTATE_NUM
                Case 0
                    If checkNoColor(TOP_X_L, TOP_Y) = True _
                    And checkNoColor(TOP_X_L - 1, TOP_Y - 1) = True _
                    Then
                        checkColor_Left = True
                    End If
                Case 1
                    If checkNoColor(TOP_X_L - 1, TOP_Y) = True _
                    And checkNoColor(TOP_X_L - 1, TOP_Y - 1) = True _
                    And checkNoColor(TOP_X_L, TOP_Y - 2) = True _
                    Then
                        checkColor_Left = True
                    End If
            End Select
        Case 7
            Select Case ROTATE_NUM
                Case 0
                    If checkNoColor(TOP_X_L - 1, TOP_Y) = True _
                    And checkNoColor(TOP_X_L, TOP_Y - 1) = True _
                    Then
                        checkColor_Left = True
                    End If
                Case 1
                    If checkNoColor(TOP_X_L, TOP_Y) = True _
                    And checkNoColor(TOP_X_L - 1, TOP_Y - 1) = True _
                    And checkNoColor(TOP_X_L - 1, TOP_Y - 2) = True _
                    Then
                        checkColor_Left = True
                    End If
            End Select
    End Select

End Function

' 隣の色の確認 置ける箇所ならTrue 右方向
Function checkColor_Right(ByVal x As Integer, ByVal y As Integer, ByRef Block As Integer) As Boolean

    checkColor_Right = False
    
    Select Case Block
        Case 1
            If checkNoColor(TOP_X_R + 1, TOP_Y) = True _
            And checkNoColor(TOP_X_R + 1, TOP_Y - 1) = True _
            Then
                checkColor_Right = True
            End If
        Case 2
            Select Case ROTATE_NUM
                Case 0
                    If checkNoColor(TOP_X_R + 1, TOP_Y) = True _
                    And checkNoColor(TOP_X_R + 1, TOP_Y - 1) = True _
                    And checkNoColor(TOP_X_R + 1, TOP_Y - 2) = True _
                    And checkNoColor(TOP_X_R + 1, TOP_Y - 3) = True _
                    Then
                        checkColor_Right = True
                    End If
                Case 1
                    If checkNoColor(TOP_X_R, TOP_Y + 1) = True _
                    And checkNoColor(TOP_X_L - 1, TOP_Y) = True _
                    And checkNoColor(TOP_X_R + 1, TOP_Y) = True _
                    Then
                        checkColor_Right = True
                    End If
            End Select
        Case 3
            Select Case ROTATE_NUM
                Case 0
                    If checkNoColor(TOP_X_R + 1, TOP_Y) = True _
                    And checkNoColor(TOP_X_R - 1, TOP_Y - 1) = True _
                    Then
                        checkColor_Right = True
                    End If
                Case 1
                    If checkNoColor(TOP_X_R, TOP_Y) = True _
                    And checkNoColor(TOP_X_R, TOP_Y - 1) = True _
                    And checkNoColor(TOP_X_R + 1, TOP_Y - 2) = True _
                    Then
                        checkColor_Right = True
                    End If
                Case 2
                    If checkNoColor(TOP_X_R + 1, TOP_Y) = True _
                    And checkNoColor(TOP_X_R + 1, TOP_Y - 1) = True _
                    Then
                        checkColor_Right = True
                    End If
                Case 3
                    If checkNoColor(TOP_X_R + 1, TOP_Y) = True _
                    And checkNoColor(TOP_X_R + 1, TOP_Y - 1) = True _
                    And checkNoColor(TOP_X_R + 1, TOP_Y - 2) = True _
                    Then
                        checkColor_Right = True
                    End If
            End Select
        Case 4
            Select Case ROTATE_NUM
                Case 0
                    If checkNoColor(TOP_X_R + 1, TOP_Y) = True _
                    And checkNoColor(TOP_X_R + 1, TOP_Y - 1) = True _
                    Then
                        checkColor_Right = True
                    End If
                Case 1
                    If checkNoColor(TOP_X_R + 1, TOP_Y) = True _
                    And checkNoColor(TOP_X_R, TOP_Y - 1) = True _
                    And checkNoColor(TOP_X_R, TOP_Y - 2) = True _
                    Then
                        checkColor_Right = True
                    End If
                Case 2
                    If checkNoColor(TOP_X_R - 1, TOP_Y) = True _
                    And checkNoColor(TOP_X_R + 1, TOP_Y - 1) = True _
                    Then
                        checkColor_Right = True
                    End If
                Case 3
                    If checkNoColor(TOP_X_R + 1, TOP_Y) = True _
                    And checkNoColor(TOP_X_R + 1, TOP_Y - 1) = True _
                    And checkNoColor(TOP_X_R + 1, TOP_Y - 2) = True _
                    Then
                        checkColor_Right = True
                    End If
            End Select
        Case 5
            Select Case ROTATE_NUM
                Case 0
                    If checkNoColor(TOP_X_R + 1, TOP_Y) = True _
                    And checkNoColor(TOP_X_R, TOP_Y - 1) = True _
                    Then
                        checkColor_Right = True
                    End If
                Case 1
                    If checkNoColor(TOP_X_R, TOP_Y) = True _
                    And checkNoColor(TOP_X_R + 1, TOP_Y - 1) = True _
                    And checkNoColor(TOP_X_R, TOP_Y - 2) = True _
                    Then
                        checkColor_Right = True
                    End If
                Case 2
                    If checkNoColor(TOP_X_R, TOP_Y) = True _
                    And checkNoColor(TOP_X_R + 1, TOP_Y - 1) = True _
                    Then
                        checkColor_Right = True
                    End If
                Case 3
                    If checkNoColor(TOP_X_R + 1, TOP_Y) = True _
                    And checkNoColor(TOP_X_R + 1, TOP_Y - 1) = True _
                    And checkNoColor(TOP_X_R + 1, TOP_Y - 2) = True _
                    Then
                        checkColor_Right = True
                    End If
            End Select
        Case 6
            Select Case ROTATE_NUM
                Case 0
                    If checkNoColor(TOP_X_R, TOP_Y - 1) = True _
                    And checkNoColor(TOP_X_R + 1, TOP_Y) = True _
                    Then
                        checkColor_Right = True
                    End If
                Case 1
                    If checkNoColor(TOP_X_R, TOP_Y) = True _
                    And checkNoColor(TOP_X_R + 1, TOP_Y - 1) = True _
                    And checkNoColor(TOP_X_R + 1, TOP_Y - 2) = True _
                    Then
                        checkColor_Right = True
                    End If
            End Select
        Case 7
            Select Case ROTATE_NUM
                Case 0
                    If checkNoColor(TOP_X_R, TOP_Y) = True _
                    And checkNoColor(TOP_X_R + 1, TOP_Y - 1) = True _
                    Then
                        checkColor_Right = True
                    End If
                Case 1
                    If checkNoColor(TOP_X_R + 1, TOP_Y) = True _
                    And checkNoColor(TOP_X_R + 1, TOP_Y - 1) = True _
                    And checkNoColor(TOP_X_R, TOP_Y - 2) = True _
                    Then
                        checkColor_Right = True
                    End If
            End Select
    End Select

End Function


' 隣の色の確認 置ける箇所ならTrue 下方向
Function checkColor_Down(ByVal x As Integer, ByVal y As Integer, ByRef Block As Integer) As Boolean

    checkColor_Down = False
    
    Select Case Block
        Case 1
            If checkNoColor(TOP_X_L, TOP_Y + 1) = True _
            And checkNoColor(TOP_X_R, TOP_Y + 1) = True _
            Then
                checkColor_Down = True
            End If
        Case 2
            Select Case ROTATE_NUM
                Case 0
                    If checkNoColor(TOP_X_L, TOP_Y + 1) = True _
                    Then
                        checkColor_Down = True
                    End If
                Case 1
                    If checkNoColor(TOP_X_L, TOP_Y + 1) = True _
                    And checkNoColor(TOP_X_L + 1, TOP_Y + 1) = True _
                    And checkNoColor(TOP_X_L + 2, TOP_Y + 1) = True _
                    And checkNoColor(TOP_X_R, TOP_Y + 1) = True _
                    Then
                        checkColor_Down = True
                    End If
            End Select
        Case 3
            Select Case ROTATE_NUM
                Case 0
                    If checkNoColor(TOP_X_L, TOP_Y + 1) = True _
                    And checkNoColor(TOP_X_L + 1, TOP_Y + 1) = True _
                    And checkNoColor(TOP_X_R, TOP_Y + 1) = True _
                    Then
                        checkColor_Down = True
                    End If
                Case 1
                    If checkNoColor(TOP_X_L, TOP_Y + 1) = True _
                    And checkNoColor(TOP_X_R, TOP_Y - 1) = True _
                    Then
                        checkColor_Down = True
                    End If
                Case 2
                    If checkNoColor(TOP_X_L, TOP_Y) = True _
                    And checkNoColor(TOP_X_L + 1, TOP_Y) = True _
                    And checkNoColor(TOP_X_R, TOP_Y + 1) = True _
                    Then
                        checkColor_Down = True
                    End If
                Case 3
                    If checkNoColor(TOP_X_L, TOP_Y + 1) = True _
                    And checkNoColor(TOP_X_R, TOP_Y + 1) = True _
                    Then
                        checkColor_Down = True
                    End If
            End Select
        Case 4
            Select Case ROTATE_NUM
                Case 0
                    If checkNoColor(TOP_X_L, TOP_Y + 1) = True _
                    And checkNoColor(TOP_X_L + 1, TOP_Y + 1) = True _
                    And checkNoColor(TOP_X_R, TOP_Y + 1) = True _
                    Then
                        checkColor_Down = True
                    End If
                Case 1
                    If checkNoColor(TOP_X_L, TOP_Y + 1) = True _
                    And checkNoColor(TOP_X_R, TOP_Y + 1) = True _
                    Then
                        checkColor_Down = True
                    End If
                Case 2
                    If checkNoColor(TOP_X_L, TOP_Y + 1) = True _
                    And checkNoColor(TOP_X_L + 1, TOP_Y) = True _
                    And checkNoColor(TOP_X_R, TOP_Y) = True _
                    Then
                        checkColor_Down = True
                    End If
                Case 3
                    If checkNoColor(TOP_X_L, TOP_Y - 1) = True _
                    And checkNoColor(TOP_X_R, TOP_Y + 1) = True _
                    Then
                        checkColor_Down = True
                    End If
            End Select
        Case 5
            Select Case ROTATE_NUM
                Case 0
                    If checkNoColor(TOP_X_L, TOP_Y + 1) = True _
                    And checkNoColor(TOP_X_L + 1, TOP_Y + 1) = True _
                    And checkNoColor(TOP_X_R, TOP_Y + 1) = True _
                    Then
                        checkColor_Down = True
                    End If
                Case 1
                    If checkNoColor(TOP_X_L, TOP_Y + 1) = True _
                    And checkNoColor(TOP_X_R, TOP_Y) = True _
                    Then
                        checkColor_Down = True
                    End If
                Case 2
                    If checkNoColor(TOP_X_L, TOP_Y) = True _
                    And checkNoColor(TOP_X_L + 1, TOP_Y + 1) = True _
                    And checkNoColor(TOP_X_R, TOP_Y) = True _
                    Then
                        checkColor_Down = True
                    End If
                Case 3
                    If checkNoColor(TOP_X_L, TOP_Y) = True _
                    And checkNoColor(TOP_X_R, TOP_Y + 1) = True _
                    Then
                        checkColor_Down = True
                    End If
            End Select
        Case 6
            Select Case ROTATE_NUM
                Case 0
                    If checkNoColor(TOP_X_L, TOP_Y) = True _
                    And checkNoColor(TOP_X_L + 1, TOP_Y + 1) = True _
                    And checkNoColor(TOP_X_R, TOP_Y + 1) = True _
                    Then
                        checkColor_Down = True
                    End If
                Case 1
                    If checkNoColor(TOP_X_L, TOP_Y + 1) = True _
                    And checkNoColor(TOP_X_R, TOP_Y) = True _
                    Then
                        checkColor_Down = True
                    End If
            End Select
        Case 7
            Select Case ROTATE_NUM
                Case 0
                    If checkNoColor(TOP_X_L, TOP_Y + 1) = True _
                    And checkNoColor(TOP_X_L + 1, TOP_Y + 1) = True _
                    And checkNoColor(TOP_X_R, TOP_Y) = True _
                    Then
                        checkColor_Down = True
                    End If
                Case 1
                    If checkNoColor(TOP_X_L, TOP_Y) = True _
                    And checkNoColor(TOP_X_R, TOP_Y + 1) = True _
                    Then
                        checkColor_Down = True
                    End If
            End Select
    End Select

End Function

' 指定セルの色がデフォルトの色か デフォルトの色ならTrue
Public Function checkNoColor(ByVal x As Integer, ByVal y As Integer) As Boolean
    
    Dim color
    
    color = Worksheets(TETRIS_SHEET).Cells(y, x).Interior.color
    
    If color = RGB(255, 255, 255) Then
        checkNoColor = True
    Else
        checkNoColor = False
    End If

End Function</pre>
&nbsp;</p>
<p>つぎにブロックの回転ですが（これ結構悩んだんですが）一番早いのがブロックごとにパターンをつくってセルに描くことです。</p>
<p>順に説明しますと、ブロックの回転は「Ctrl」キーで回転するようにしていますが、</p>
<p>その「Ctrl」キーの入力イベントを検知した後、「Ctrl」キーが押された回数を変数 ROTATE_NUM にもたせます。</p>
<p>&nbsp;</p>
<p>例えば「Ctrl」キーが２回入力されれば ROTATE_NUM = 2となります。そして、ブロック名が「2」で ROTATE_NUM = 2 なら「こう描く！」と決めておきます。</p>
<p>※具体的には下記表のようにパターン化します。</p>
<p>&nbsp;</p>
<p><img loading="lazy" decoding="async" src="https://lemon818.com/wp/wp-content/uploads/2019/02/excel-tetoris-8.png" alt="" width="321" height="486" class="aligncenter wp-image-3865" srcset="https://lemon818.com/wp/wp-content/uploads/2019/02/excel-tetoris-8.png 419w, https://lemon818.com/wp/wp-content/uploads/2019/02/excel-tetoris-8-198x300.png 198w" sizes="auto, (max-width: 321px) 100vw, 321px" /></p>
<p>&nbsp;</p>
<p>あとは上記表の組み合わせをガリガリ書くだけです（これが書き間違えなどあって大変でしたね、、、）。</p>
<h5>ソースコード</h5>
<pre class="crayon-plain-tag">Option Explicit

' ブロック 回転
Sub rotateBlock(ByRef x As Integer, ByRef y As Integer, ByRef Block As Integer)
    
    Dim tmp_ROTATE_NUM
    
    Call deleteRandBlock(x, y, Block)
    
    Select Case Block
        Case 2, 6, 7
            If ROTATE_NUM = 0 Then
                tmp_ROTATE_NUM = ROTATE_NUM + 1
            Else
                tmp_ROTATE_NUM = 0
            End If
        Case 3, 4, 5
            If ROTATE_NUM &lt;&gt; 3 Then
                tmp_ROTATE_NUM = ROTATE_NUM + 1
            Else
                tmp_ROTATE_NUM = 0
            End If
    End Select
    
    ' 回転できるか確認
    If checkRotateBlock(x, y, Block) = True Then
        
        ' 端っこで回転させる場合、ブロックをずらす
        If Block = 2 And TOP_X_R = BOTTOM_X_RIGHT - 1 And ROTATE_NUM = 0 Then x = BOTTOM_X_RIGHT - 4
        If Block = 2 And TOP_X_R &lt;= BOTTOM_X_LEFT + 2 And ROTATE_NUM = 0 Then x = BOTTOM_X_LEFT + 1
        If Block = 3 And TOP_X_R = BOTTOM_X_RIGHT - 1 And ROTATE_NUM = 1 Then x = BOTTOM_X_RIGHT - 3
        If Block = 4 And TOP_X_R = BOTTOM_X_RIGHT - 1 And ROTATE_NUM = 1 Then x = BOTTOM_X_RIGHT - 3
        If Block = 6 And TOP_X_R = BOTTOM_X_RIGHT - 1 And ROTATE_NUM = 1 Then x = BOTTOM_X_RIGHT - 3
        If Block = 7 And TOP_X_R = BOTTOM_X_RIGHT - 1 And ROTATE_NUM = 1 Then x = BOTTOM_X_RIGHT - 3
        
        ROTATE_NUM = tmp_ROTATE_NUM
        Call getRandBlock(x, y, Block)
        Call setTopPlace(x, y, Block)
    Else
        Call getRandBlock(x, y, Block)
    End If
    
End Sub

' ブロックが回転できるか できるなら True
Function checkRotateBlock(ByVal x As Integer, ByVal y As Integer, ByRef Block As Integer)
    
    checkRotateBlock = False
    
    Select Case Block
        Case 1
            checkRotateBlock = checkRotateBlock1(x, y)
        Case 2
            checkRotateBlock = checkRotateBlock2(x, y)
        Case 3
            checkRotateBlock = checkRotateBlock3(x, y)
        Case 4
            checkRotateBlock = checkRotateBlock4(x, y)
        Case 5
            checkRotateBlock = checkRotateBlock5(x, y)
        Case 6
            checkRotateBlock = checkRotateBlock6(x, y)
        Case 7
            checkRotateBlock = checkRotateBlock7(x, y)
    End Select

End Function

' 正方形　ブロックが回転できるか できるなら True
Function checkRotateBlock1(ByVal x As Integer, ByVal y As Integer) As Boolean
    
    checkRotateBlock1 = True
    
    Const color As Integer = BLOCK1_COLOR
    
    If checkNoColor(color, x + 1, y) = False _
    Or checkNoColor(x + 2, y) = False _
    Or checkNoColor(x + 1, y - 1) = False _
    Or checkNoColor(x + 2, y - 1) = False _
    Then
        checkRotateBlock1 = False
    End If

End Function

' 長方形　ブロックが回転できるか できるなら True
Function checkRotateBlock2(ByVal x As Integer, ByVal y As Integer)

    checkRotateBlock2 = True
    
    Const color As Integer = BLOCK2_COLOR
    
    If ROTATE_NUM = 1 Then
        If checkNoColor(x + 2, y) = False _
        Or checkNoColor(x + 2, y - 1) = False _
        Or checkNoColor(x + 2, y - 2) = False _
        Or checkNoColor(x + 2, y - 3) = False _
        Then
            checkRotateBlock2 = False
        End If

    Else
        If checkNoColor(x, y - 2) = False _
        Or checkNoColor(x + 1, y - 2) = False _
        Or checkNoColor(x + 2, y - 2) = False _
        Or checkNoColor(x + 3, y - 2) = False _
        Then
            checkRotateBlock2 = False
        End If

    End If
    
End Function

' L字　ブロックが回転できるか できるなら True
Function checkRotateBlock3(ByVal x As Integer, ByVal y As Integer)
    
    checkRotateBlock3 = True
    
    Const color As Integer = BLOCK3_COLOR
    
    If ROTATE_NUM = 0 Then
        If checkNoColor(x, y) = False _
        Or checkNoColor(x, y - 1) = False _
        Or checkNoColor(x, y - 2) = False _
        Or checkNoColor(x + 1, y - 2) = False _
        Then
            checkRotateBlock3 = False
        End If

    ElseIf ROTATE_NUM = 1 Then
        If checkNoColor(x, y - 1) = False _
        Or checkNoColor(x + 1, y - 1) = False _
        Or checkNoColor(x + 2, y - 1) = False _
        Or checkNoColor(x + 2, y) = False _
        Then
            checkRotateBlock3 = False
        End If

    ElseIf ROTATE_NUM = 2 Then
        If checkNoColor(x, y) = False _
        Or checkNoColor(x + 1, y) = False _
        Or checkNoColor(x + 1, y - 1) = False _
        Or checkNoColor(x + 1, y - 2) = False _
        Then
            checkRotateBlock3 = False
        End If

    Else
        If checkNoColor(x, y) = False _
        Or checkNoColor(x + 1, y) = False _
        Or checkNoColor(x + 2, y) = False _
        Or checkNoColor(x, y - 1) = False _
        Then
            checkRotateBlock3 = False
        End If

    End If
    
End Function

' 逆L字　ブロックが回転できるか できるなら True
Function checkRotateBlock4(ByVal x As Integer, ByVal y As Integer)
    
    checkRotateBlock4 = True
    
    Const color As Integer = BLOCK4_COLOR
    
    If ROTATE_NUM = 0 Then
        If checkNoColor(x, y) = False _
        Or checkNoColor(x, y - 1) = False _
        Or checkNoColor(x, y - 2) = False _
        Or checkNoColor(x + 1, y) = False _
        Then
            checkRotateBlock4 = False
        End If

    ElseIf ROTATE_NUM = 1 Then
        If checkNoColor(x, y) = False _
        Or checkNoColor(x, y - 1) = False _
        Or checkNoColor(x + 1, y - 1) = False _
        Or checkNoColor(x + 2, y - 1) = False _
        Then
            checkRotateBlock4 = False
        End If
    ElseIf ROTATE_NUM = 2 Then
        If checkNoColor(x, y - 2) = False _
        Or checkNoColor(x + 1, y) = False _
        Or checkNoColor(x + 1, y - 1) = False _
        Or checkNoColor(x + 1, y - 2) = False _
        Then
            checkRotateBlock4 = False
        End If
    Else
        If checkNoColor(x, y) = False _
        Or checkNoColor(x + 1, y) = False _
        Or checkNoColor(x + 2, y) = False _
        Or checkNoColor(x + 2, y - 1) = False _
        Then
            checkRotateBlock4 = False
        End If
    End If
    
End Function

' 山　ブロックが回転できるか できるなら True
Function checkRotateBlock5(ByVal x As Integer, ByVal y As Integer)
    
    checkRotateBlock5 = True
    
    Const color As Integer = BLOCK5_COLOR
    
    If ROTATE_NUM = 0 Then
        If checkNoColor(x + 1, y) = False _
        Or checkNoColor(x + 1, y - 1) = False _
        Or checkNoColor(x + 1, y - 2) = False _
        Or checkNoColor(x + 2, y - 1) = False _
        Then
            checkRotateBlock5 = False
        End If
    ElseIf ROTATE_NUM = 1 Then
        If checkNoColor(x, y - 1) = False _
        Or checkNoColor(x + 1, y - 1) = False _
        Or checkNoColor(x + 2, y - 1) = False _
        Or checkNoColor(x + 1, y) = False _
        Then
            checkRotateBlock5 = False
        End If
    ElseIf ROTATE_NUM = 2 Then
        If checkNoColor(x, y - 1) = False _
        Or checkNoColor(x + 1, y) = False _
        Or checkNoColor(x + 1, y - 1) = False _
        Or checkNoColor(x + 1, y - 2) = False _
        Then
            checkRotateBlock5 = False
        End If
    Else
        If checkNoColor(x, y - 1) = False _
        Or checkNoColor(x + 1, y - 1) = False _
        Or checkNoColor(x + 1, y - 2) = False _
        Or checkNoColor(x + 2, y - 1) = False _
        Then
            checkRotateBlock5 = False
        End If
    End If
    
End Function

' Z字　ブロックが回転できるか できるなら True
Function checkRotateBlock6(ByVal x As Integer, ByVal y As Integer)
    
    checkRotateBlock6 = True
    
    Const color As Integer = BLOCK6_COLOR
    
    If ROTATE_NUM = 0 Then
        If checkNoColor(x, y) = False _
        Or checkNoColor(x, y - 1) = False _
        Or checkNoColor(x + 1, y - 1) = False _
        Or checkNoColor(x + 1, y - 2) = False _
        Then
            checkRotateBlock6 = False
        End If
    Else
        If checkNoColor(x, y - 1) = False _
        Or checkNoColor(x + 1, y) = False _
        Or checkNoColor(x + 1, y - 1) = False _
        Or checkNoColor(x + 2, y) = False _
        Then
            checkRotateBlock6 = False
        End If
    End If
    
End Function

' 逆Z字　ブロックが回転できるか できるなら True
Function checkRotateBlock7(ByVal x As Integer, ByVal y As Integer)
    
    checkRotateBlock7 = True
    
    Const color As Integer = BLOCK7_COLOR
    
    If ROTATE_NUM = 0 Then
        If checkNoColor(x, y - 1) = False _
        Or checkNoColor(x, y - 2) = False _
        Or checkNoColor(x + 1, y) = False _
        Or checkNoColor(x + 1, y - 1) = False _
        Then
            checkRotateBlock7 = False
        End If
    Else
        If checkNoColor(x, y) = False _
        Or checkNoColor(x + 1, y) = False _
        Or checkNoColor(x + 1, y - 1) = False _
        Or checkNoColor(x + 2, y - 1) = False _
        Then
            checkRotateBlock7 = False
        End If
        
    End If
    
End Function</pre>
&nbsp;</p>
<h3>ブロックが一行並べば消す</h3>
<p>&nbsp;</p>
<p>ブロックの移動が完了すれば次は、ブロックが一行そろっているか確認し、そろっている場合はその行を削除してスコアに加算します。</p>
<p><img loading="lazy" decoding="async" src="https://lemon818.com/wp/wp-content/uploads/2019/02/excel-tetoris-7.png" alt="" width="589" height="202" class="aligncenter wp-image-3863" srcset="https://lemon818.com/wp/wp-content/uploads/2019/02/excel-tetoris-7.png 669w, https://lemon818.com/wp/wp-content/uploads/2019/02/excel-tetoris-7-300x103.png 300w" sizes="auto, (max-width: 589px) 100vw, 589px" /></p>
<p>&nbsp;</p>
<h5>ソースコード</h5>
<pre class="crayon-plain-tag">' ブロックの1列ブロックが削除できるところか
Sub checkRowBlock()
    
    Dim x, y, n: n = BOTTOM_Y
    Dim nocolor As Integer
    Dim flag
    Dim Score_num: Score_num = 0
    Dim cellcolor1, cellcolor2
    
    ' そろっている行をグレーにする
    For y = BOTTOM_Y - 1 To START_Y Step -1
        nocolor = 0
        For x = BOTTOM_X_LEFT + 1 To BOTTOM_X_RIGHT - 1 Step 1
            If checkNoColor(x, y) = True Then
              nocolor = nocolor + 1
            End If
        Next x
        If nocolor = BOTTOM_X_RIGHT - BOTTOM_X_LEFT - 1 Then
            Exit For
        ElseIf nocolor = 0 Then
            flag = 1
            For x = BOTTOM_X_LEFT + 1 To BOTTOM_X_RIGHT - 1 Step 1
                With Worksheets(TETRIS_SHEET).Cells(y, x)
                    .Interior.color = RGB(192, 192, 192)
                    .Borders.LineStyle = xlContinuous
                End With
            Next x
        End If
    Next y
    
    ' グレーの行を削除
    If flag = 1 Then
        Application.ScreenUpdating = False
        For y = BOTTOM_Y - 1 To START_Y Step -1
            cellcolor1 = Worksheets(TETRIS_SHEET).Cells(y, BOTTOM_X_LEFT + 1).Interior.color
            If cellcolor1 = RGB(192, 192, 192) Then
                Score_num = Score_num + 1
                For n = y To START_Y Step -1
                    For x = BOTTOM_X_LEFT + 1 To BOTTOM_X_RIGHT - 1
                        With Worksheets(TETRIS_SHEET).Cells(n, x)
                            .ClearFormats
                            .Interior.color = Worksheets(TETRIS_SHEET).Cells(n - 1, x).Interior.color
                        End With
                        If checkNoColor(x, n - 1) = False Then
                            With Worksheets(TETRIS_SHEET).Cells(n, x)
                                .Borders.LineStyle = xlContinuous
                            End With
                        End If
                    Next x
                Next n
            End If
            cellcolor1 = Worksheets(TETRIS_SHEET).Cells(y, BOTTOM_X_LEFT + 1).Interior.color
            If cellcolor1 = RGB(192, 192, 192) Then
                y = y + 1
            End If
        Next y
        Application.ScreenUpdating = False
     End If
     
     If Score_num &gt; 0 Then
         Score = Score + 100 + Score_num * 300
     End If
     
     Worksheets(TETRIS_SHEET).Cells(SCORE_POSITION_Y, SCORE_POSITION_X).Value = Score
      
End Sub</pre>
&nbsp;</p>
<h3>テトリスの終了条件をつける</h3>
<p>&nbsp;</p>
<p>あとは終了条件だけですね。ある行を天井と決め、その行よりも一つ上の行を端から端までセルの書式を確認し、ブロックがあれば（セルに色がついてあれば）メッセージを表示させ終了させます。</p>
<p>&nbsp;</p>
<h5>ソースコード</h5>
<pre class="crayon-plain-tag">' ゲーム終了
Private Sub checkgame()
    
    Dim x

    For x = BOTTOM_X_LEFT + 1 To BOTTOM_X_RIGHT - 1
        If checkNoColor(x, START_Y - 1) = False Then
          MsgBox "Game Over!" &amp; Chr(13) &amp; "Score : " &amp; Score
          End
        End If
    Next x

End Sub</pre>
&nbsp;</p>
<h2>最後に</h2>
<p>いかがでしょうか？</p>
<p>長々と失礼しました。この記事49000文字近くまでいってました、、、まあ大半がソースコードですが。</p>
<p>これ思ったより作るのめんどくさかったですね。。けれどゲームをつくるのってやっぱり楽しいです。</p>
<p>&nbsp;</p>
<p>皆様もよければぜひ参考に作ってみてください！</p>
<p>最後にこのテトリスが気になる人もしくは作成したテトリスを参考にしたい人がいれば、ぜひダウンロードしてみてください！</p>
<p>&nbsp;</p>
<div class="button frame block blue"><a href="https://lemon818.com/wp/wp-content/uploads/2019/02/tetoris.xlsm" class="midium">エクセルのテトリスをダウンロードする</a></div>
<div></div>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>【Excel vba】名簿から自動で席替えしてくれるマクロの作り方！！</title>
		<link>https://lemon818.com/excel-sekigae/</link>
		
		<dc:creator><![CDATA[Take]]></dc:creator>
		<pubDate>Wed, 06 Feb 2019 13:28:02 +0000</pubDate>
				<category><![CDATA[Excel]]></category>
		<category><![CDATA[vba]]></category>
		<category><![CDATA[マクロ]]></category>
		<category><![CDATA[名簿]]></category>
		<category><![CDATA[席替え]]></category>
		<guid isPermaLink="false">https://lemon818.com/?p=3767</guid>

					<description><![CDATA[はじめに 名簿リストから席を自動で決定してくれる Excel マクロの作り方についてご紹介します。 このマクロは席数を自分で指定できるようにしてあるので、例えば 4 × 4 や ５× 6 の席でも自由に変えて席替えできる…]]></description>
										<content:encoded><![CDATA[<h2>はじめに</h2>
<p><strong>名簿リストから席を自動で決定してくれる Excel マクロの作り方</strong>についてご紹介します。</p>
<p>このマクロは<strong></strong><span style="color: #ff6600;"><strong>席数を自分で指定できるようにしてある</strong></span>ので、例えば 4 × 4 や ５× 6 の席でも自由に変えて席替えできることができます。</p>
<p>小学校や中学、高校では、気分転換のためかなんとなくか目的はさまざまですが、とにかく定期的に席替えが行われると思います。</p>
<p>そんなとき毎回くじ作ってそのくじを引いてみんなでギャーギャー騒いで...って楽しいんですが、<strong>教員の方は早く帰りたいのかもしれません。</strong></p>
<p>そんな教師はあんまり見たことがないんですが、少なからず席替えに時間を割くことがほとんどなくなりますので「いいな」と思われた人はぜひ使ってみてください。</p>
<p>※実際に作成した Excel マクロは下記のボタンからダウンロードできます。</p>
<div class="button frame block green"><a href="https://lemon818.com/wp/wp-content/uploads/2019/02/excel-zasekihyo.xlsm" class="midium">Excel 座席表自動作成マクロをダウンロードする</a></div>
<p>&nbsp;</p>
<h2>操作方法</h2>
<p>（１）<strong>名前を入力します。</strong>名前の左に番号が付いていますが、番号はなんでもいいです。</p>
<p>ダウンロードしたマクロには番号を自動で入力する関数を埋め込んでいますが、消して出席番号とかにしていただいても問題ありません。</p>
<p><img loading="lazy" decoding="async" src="https://lemon818.com/wp/wp-content/uploads/2019/02/zasekihyo-3.png" alt="" width="224" height="232" class="aligncenter wp-image-3784" /></p>
<p>（２）<strong>席の行数と列数</strong>を入力します。値は整数を入力してください。また「チェック」というセルで入力チェックを行ってます。</p>
<p>例えば、名簿シートに１０人で座席数が６（行数 : 2  列数 : 3）の場合は、４人分席が不足していますね。こういうときは「チェック」欄に座席数が不足しています。というメッセージが表示されます。</p>
<p>&nbsp;</p>
<p><img loading="lazy" decoding="async" src="https://lemon818.com/wp/wp-content/uploads/2019/02/zasekihyo-4.png" alt="" width="585" height="466" class="aligncenter wp-image-3785" /></p>
<p>（３）「座席表を作成」ボタンを押下します。すると別シート「座席表」に結果が表示されます。</p>
<p><img loading="lazy" decoding="async" src="https://lemon818.com/wp/wp-content/uploads/2019/02/zasekihyo-5.png" alt="" width="243" height="68" class="aligncenter size-full wp-image-3786" /></p>
<h2>座席表自動作成マクロの作り方</h2>
<p>座席表自動生成マクロの作成方法についてご紹介します。今回は Excel vba をつかって作成しています。</p>
<div class="sc_frame_wrap block green">
<div class="sc_frame_title">シート構成</div>
<div class="sc_frame ">
<div class="sc_designlist li fa_angle_o green">
<ul>
<li>「名簿」シート : 名簿を入力するシート</li>
<li>「座席表」シート : 座席表が出力されるシート</li>
</ul>
</div>
</div>
</div>
<p>シートは上記２種類から構成されます。要は入力用シートとして「名簿」、出力用シートとして「座席表」の２種類です。</p>
<h5>「名簿」シート</h5>
<p><img loading="lazy" decoding="async" src="https://lemon818.com/wp/wp-content/uploads/2019/02/zasekihyo-2.png" alt="" width="675" height="477" class="aligncenter wp-image-3773" srcset="https://lemon818.com/wp/wp-content/uploads/2019/02/zasekihyo-2.png 825w, https://lemon818.com/wp/wp-content/uploads/2019/02/zasekihyo-2-300x212.png 300w, https://lemon818.com/wp/wp-content/uploads/2019/02/zasekihyo-2-768x543.png 768w" sizes="auto, (max-width: 675px) 100vw, 675px" /></p>
<p>入力欄は「名前」「行数」「列数」の三つです。</p>
<p>「番号」はいちおうつけていますが、<strong>処理上は何の関係もありません。</strong>つまりどんな番号が入力されても問題なく動作できます。</p>
<p>ダウンロード用の Excel マクロは自動で入力できるようにしていますが、上書きして出席番号にしていただいても何の問題ありません。</p>
<h5>「座席表」シート</h5>
<p>初期はまっさらなシートです。処理が完了した後に座席表が書き出されます。</p>
<p>&nbsp;</p>
<h3>処理内容（Excel vba）</h3>
<div class="sc_frame_wrap block blue">
<div class="sc_frame_title">処理の概要</div>
<div class="sc_frame ">
<div class="sc_frame_text">
<div class="sc_designlist ol square solid blue">
<ol>
<li>名簿シートの読み込み</li>
<li>座席表シートへの書き込み</li>
<li>教卓の書き込み</li>
</ol>
</div>
</div>
</div>
</div>
<h4>名簿シートの読み込み</h4>
<p>これはシンプルな処理で、「名簿」シートのB列２行目からセルの値が空白になるまで読み込んで配列に入力しているだけです。</p><pre class="crayon-plain-tag">'名簿シートの読み込み
Private Sub readNameList(ByRef NameList() As String)
    
    Const x As Integer = 2
    Const y As Integer = 3
    
    Dim Name As String
    Dim index As Integer: index = 0

    Do
        ReDim Preserve NameList(index)
        Name = Worksheets(NAMELISTSHEET).Cells(y + index, x).Value
        NameList(index) = Name
        index = index + 1
    Loop Until Name = ""
    
    If UBound(NameList) = 0 Then
        MsgBox "名前を入力してください"
        End
    End If
    
    ReDim Preserve NameList(UBound(NameList) - 1)

End Sub</pre><p>
<h4>座席表シートへの書き込み</h4>
<p>「名簿」シートから取得した名前のデータを含む配列をランダムに取り出して、取り出した値を「座席表」シートに書き込むだけの処理です。</p>
<p>ソースコードを見ると複雑そうに見えますがやっていることはシンプルです。</p>
<div class="sc_frame_wrap inline orange">
<div class="sc_frame ">
<div class="sc_frame_text">
<div class="sc_designlist ol square solid orange">
<ol>
<li>「名簿」シートのエラー処理（座数の行数と列数  :　数値以外 or 0のとき or 空白ならエラー)</li>
<li>  エラー処理が終わったら「座席表」シートをいったん初期化</li>
<li>「名簿」シートから取得した名前を「座席表」シートに書き込む</li>
</ol>
</div>
</div>
</div>
</div>
<pre class="crayon-plain-tag">' 座席シート表への書き込み
Private Sub writeZasekihyo(ByRef NameList() As String)

    Const x As Integer = 2
    Const y As Integer = 4
    Dim RandList() As Integer
    Dim Name As String
    Dim i, j, k
    j = 0
    k = 0
    
    Dim Max_Row
    Dim Max_Column
    
    Max_Row = Worksheets(NAMELISTSHEET).Cells(2, 4).Value
    Max_Column = Worksheets(NAMELISTSHEET).Cells(2, 5).Value
 
    If Max_Row = "" Or Max_Column = "" Then
        MsgBox "行と列を入力してください"
        End
    End If
    
    If Max_Row &lt; 1 Or Max_Column &lt; 1 _
        Or IsNumeric(Max_Row) = False Or IsNumeric(Max_Column) = False Then
        MsgBox "行と列には 1 以上の数値を入力してください"
        End
    End If
    
    
    ' 乱数作成
    Call calRandomArray(RandList, UBound(NameList) + 1)
    
    ' 初期化
    Application.ScreenUpdating = False
    
    Call defaultSetting(x, y - 2, Max_Row)
    
    ' 座席表へ書き込み
    For i = 0 To Max_Row * Max_Column - 1
        If i &lt;= UBound(RandList) Then
            Name = NameList(RandList(i))
        Else
            Name = ""
        End If
        Call writeZasekihyo_oneChair(k + x, j + y, Name)
        If k = Max_Column - 1 Then
            k = 0
            j = j + 1
            If j = Max_Row Then
                Exit For
            End If
        Else
            k = k + 1
        End If
    Next i

    Application.ScreenUpdating = True
    
End Sub

' 乱数生成
Sub calRandomArray(ByRef arr() As Integer, ByVal MAX_NUM As Integer)

    Dim i, rand As Integer
    Dim num() As Boolean
    ReDim num(MAX_NUM)
    ReDim arr(MAX_NUM)
    
    Randomize
    For i = 0 To MAX_NUM - 1
        Do
        rand = Int(Rnd() * MAX_NUM)
        Loop Until num(rand) = False
        arr(i) = rand
        num(rand) = True
    Next i

    ReDim Preserve arr(UBound(arr) - 1)
    
End Sub

' 初期化
Private Sub defaultSetting(ByVal x As Integer, ByVal y As Integer, ByVal Max_Row As Integer)

    Worksheets(ZASEKISHEET).Select
    
    With Worksheets(ZASEKISHEET).Range(Cells(y, x), Cells(y + 100, x + Max_Row + 100))
        .ClearContents
        .Borders.LineStyle = xlLineStyleNone
    End With
    
    Worksheets(ZASEKISHEET).Rows("2:2").Select
    Selection.Delete Shift:=xlUp

    Worksheets(ZASEKISHEET).Range("A1").Select

End Sub

' 座席のセルの書式
Private Sub writeZasekihyo_oneChair(ByVal x As Integer, ByVal y As Integer, ByVal Name As String)
    
    With Worksheets(ZASEKISHEET).Cells(y, x)
        .Value = Name
        .Borders.LineStyle = xlContinuous
        .RowHeight = 50
        .ColumnWidth = 20
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlCenter
        .Font.Size = 20
    End With
        
End Sub</pre>
<h4>教卓の書き込み</h4>
<p>最後に教卓を書き込む処理です。２行目に固定で、列は座席表の列数の値に応じて書き込まれます。</p>
<p>席の列数を2で割ったときの剰余計算（余りを算出）をして、その値が1の場合（つまり奇数のとき）は、席の列数を２で割った値を整数に変換し、得られた値 + 2を書き込み先の列番号とします。</p>
<p>席の列数を剰余計算をした値が0の場合（つまり偶数のとき）、席の列数を２で割った値を整数に変換し、得られた値 + 1 と得られた値 + 2 を書き込み先の列番号（事前に二つのセルを結合させるように処理）とします。</p>
<p>例えば席の列数が７（奇数）の場合、7 ÷ 2 = 3.5 を整数にして 3にします。そして 3 + 2 = 5 列目のセルに書き込むようにします。</p>
<p>また席の列数が８（偶数）の場合、8 ÷ 2 = 4とし、4 + 1 = 5 列目と 4 + 2 = 6 列目を書き込み先の列番号とします。</p>
<p>最後の + 2 ですが、これは<strong>B列（つまりセルの２列目）から座席表を書き込むように処理するため</strong>につけています。</p><pre class="crayon-plain-tag">' 教卓を描く
Private Sub writeKyotaku()

    Const x As Integer = 2
    Const y As Integer = 4
    Dim kyotaku As Integer
    Dim Max_Column As Integer
    
    Max_Column = Worksheets(NAMELISTSHEET).Cells(2, 5).Value
    kyotaku = Int(Max_Column / 2)
        
    If Int(Max_Column / 2) = 0 Then
        kyotaku = 0
        With Worksheets(ZASEKISHEET).Cells(2, kyotaku + x)
            .Value = "教卓"
            .Borders.LineStyle = xlContinuous
            .RowHeight = 50
            .ColumnWidth = 20
            .HorizontalAlignment = xlCenter
            .VerticalAlignment = xlCenter
            .Font.Size = 20
        End With
    Else
        If Max_Column Mod 2 = 1 Then
            kyotaku = Int(Max_Column / 2)
            With Worksheets(ZASEKISHEET).Cells(2, kyotaku + x)
                .Value = "教卓"
                .Borders.LineStyle = xlContinuous
                .RowHeight = 50
                .ColumnWidth = 20
                .HorizontalAlignment = xlCenter
                .VerticalAlignment = xlCenter
                .Font.Size = 20
            End With
        Else
            kyotaku = Int(Max_Column / 2) - 1
            With Worksheets(ZASEKISHEET).Range(Cells(2, kyotaku + x), Cells(2, kyotaku + x + 1))
                .Merge
                .Value = "教卓"
                .Borders.LineStyle = xlContinuous
                .RowHeight = 50
                .ColumnWidth = 20
                .HorizontalAlignment = xlCenter
                .VerticalAlignment = xlCenter
                .Font.Size = 20
            End With
        End If
    End If

    
End Sub</pre><p>
&nbsp;</p>
<h2>まとめ</h2>
<p>いかがでしょうか？</p>
<p>かなりざっくりした説明だったかもしれませんので、もし作るのがめんどくさいと感じた方は下記ボタンからダウンロードしてみてください。</p>
<div class="button frame block green"><a href="https://lemon818.com/wp/wp-content/uploads/2019/02/excel-zasekihyo.xlsm" class="midium">Excel 座席表自動作成マクロをダウンロードする</a></div>
<p>&nbsp;</p>
<p><span></span></p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>【Excel vba】表のセル最終行・最終列番号の取得する処理・関数を紹介！コピペでOK！</title>
		<link>https://lemon818.com/excel-vba-last-row-col/</link>
		
		<dc:creator><![CDATA[Take]]></dc:creator>
		<pubDate>Sat, 10 Nov 2018 11:46:18 +0000</pubDate>
				<category><![CDATA[Excel]]></category>
		<category><![CDATA[vba]]></category>
		<category><![CDATA[プロシージャ]]></category>
		<category><![CDATA[マクロ]]></category>
		<category><![CDATA[最終列]]></category>
		<category><![CDATA[最終行]]></category>
		<category><![CDATA[関数]]></category>
		<guid isPermaLink="false">https://lemon818.com/?p=1020</guid>

					<description><![CDATA[Excel Vba で表の最終行する処理を知りたい！ &#160; このページでは Excel の表の最終行・最終列番号を取得する処理について Excel Vba の関数（プロシージャ）として簡単にまとめましたので記述し…]]></description>
										<content:encoded><![CDATA[<div class="sc_frame_wrap solid yellow">
<div class="sc_frame ">
<div class="sc_frame_text">
<p><strong><span style="color: #000000;">Excel Vba で表の最終行する処理を知りたい！</span></strong></p>
</div>
</div>
</div>
<p>&nbsp;</p>
<p>このページでは Excel の表の最終行・最終列番号を取得する処理について</p>
<p>Excel Vba の関数（プロシージャ）として簡単にまとめましたので記述します。</p>
<p>&nbsp;</p>
<div class="voice clearfix left n_bottom">
<div class="icon">
<p><img decoding="async" src="https://lemon818.com/wp/wp-content/uploads/2019/07/hukurou.jpg" /></p>
<div class="name"></div>
</div>
<div class="text sc_balloon left white">よく使う処理の前なのによく忘れるので備忘録に残しています！</div>
</div>
<p>&nbsp;</p>
<p>この処理はかなり簡単な関数（プロシージャ）としてまとめてます。</p>
<div class="sc_designlist ol square solid blue">
<ol>
<li>セル番号を指定（たとえば B2 という形式）</li>
<li>最終行・列を取得（B列の最終行・列をゲット！）</li>
</ol>
</div>
<p>&nbsp;</p>
<p>こんな感じで<span style="color: #3366ff;"><strong>すごく簡単な処理</strong></span>になっています。</p>
<p>また<strong><span style="background-color: #ffff00;">この処理はコピーすればすぐに実装できる</span></strong>ようにしています。</p>
<p>ですので、よければ必要な方はぜひコピペして使ってください。</p>
<p>&nbsp;</p>
<div class="sc_frame_wrap block blue">
<div class="sc_frame_title">この記事でわかること！</div>
<div class="sc_frame ">
<div class="sc_frame_text">
<div class="sc_designlist ol square solid blue">
<ol>
<li>表の最終行番号を取得する処理について</li>
<li>表の最終列番号を取得する処理について</li>
</ol>
</div>
</div>
</div>
</div>
<p>&nbsp;</p>
<h2><span style="font-family: helvetica, arial, sans-serif; font-size: 20px;">Excel の表の最終行番号を取得</span><span style="font-family: helvetica, arial, sans-serif; font-size: 20px;">する処理について</span></h2>
<p>Excel の表の最終行番号を取得する方法は２通りあります。</p>
<p>&nbsp;</p>
<div class="sc_designlist ol square solid blue">
<ol>
<li>セルの元々の位置から空白セルの位置まで下って移動</li>
<li>セルの一番下から空白セルの位置まで上って移動</li>
</ol>
</div>
<p>&nbsp;</p>
<p>「<span style="color: #ff0000;"><strong>は？どういうこと？？</strong></span>」と思われた方もいるかもしれません。</p>
<p>これがどういうことなのかわかりやすく説明します。</p>
<p>&nbsp;</p>
<h3>セルの元々の位置から空白セルの位置まで下って移動</h3>
<p>まずは最終行取得のために「セルの元々の位置から空白セルの位置まで下って移動」する方法についてです。</p>
<p>&nbsp;</p>
<p>まずはこんな感じの１~５まで含まれる１列の表があるとします。</p>
<p>ここの一番上のセル番号（B2）を選択します。</p>
<p>&nbsp;</p>
<p><img loading="lazy" decoding="async" src="https://lemon818.com/wp/wp-content/uploads/2018/11/excel-last-row-1.png" alt="" width="346" height="237" class="aligncenter size-full wp-image-5600" srcset="https://lemon818.com/wp/wp-content/uploads/2018/11/excel-last-row-1.png 346w, https://lemon818.com/wp/wp-content/uploads/2018/11/excel-last-row-1-300x205.png 300w" sizes="auto, (max-width: 346px) 100vw, 346px" /></p>
<p>&nbsp;</p>
<p>そしてキーボードの「↓」キーを入力すれば表の一番下のセルに移動できます。</p>
<p>&nbsp;</p>
<p><img loading="lazy" decoding="async" src="https://lemon818.com/wp/wp-content/uploads/2018/11/excel-last-row-2.png" alt="" width="352" height="249" class="aligncenter size-full wp-image-5601" srcset="https://lemon818.com/wp/wp-content/uploads/2018/11/excel-last-row-2.png 352w, https://lemon818.com/wp/wp-content/uploads/2018/11/excel-last-row-2-300x212.png 300w" sizes="auto, (max-width: 352px) 100vw, 352px" /></p>
<p>&nbsp;</p>
<p>いわばこの処理を Excel VBAで実現すればOKっていうことです。</p>
<p>実際の Excel Vba での最終行のセル番号を取得する処理は下記になります。</p><pre class="crayon-plain-tag">Sub test()

    Debug.Print getLastRow("B2")
    
End Sub

Function getLastRow(nowCell As String)
    
    getLastRow = Range(nowCell).End(xlDown).row

End Function</pre><p>
&nbsp;</p>
<p>上記は B2セル（表の一番上）から End(xlDown) と下っていくこと処理です。</p>
<p>今回はこの処理を getLastRow プロシージャとしてまとめました。</p>
<p>この関数（プロシージャ）の引数と戻り値は下記になります。</p>
<p>&nbsp;</p>
<table style="height: 116px; width: 78.0905%; border-collapse: collapse;" border="1" align="center">
<tbody>
<tr style="height: 24px; background-color: #0099e6;">
<td style="width: 24.8693%; height: 24px;"><span style="font-family: helvetica, arial, sans-serif;"><strong>項目名</strong></span></td>
<td style="width: 46.4305%; height: 24px;"><span style="font-family: helvetica, arial, sans-serif;"><strong>値</strong></span></td>
<td style="width: 28.7001%; height: 24px;"><span style="font-family: helvetica, arial, sans-serif;"><strong>説明</strong></span></td>
</tr>
<tr style="height: 23px;">
<td style="width: 24.8693%; height: 23px; background-color: #fffcd9;"><span style="font-family: helvetica, arial, sans-serif;"><strong>プロシージャ名</strong></span></td>
<td style="width: 46.4305%; height: 23px;"><span style="font-family: helvetica, arial, sans-serif;"><strong>getLastRow</strong></span></td>
<td style="width: 28.7001%; height: 23px;"><span style="font-family: helvetica, arial, sans-serif;"><strong>最終行番号取得</strong></span></td>
</tr>
<tr style="height: 23px;">
<td style="width: 24.8693%; height: 23px; background-color: #fffcd9;"><span style="font-family: helvetica, arial, sans-serif;"><strong>戻り値</strong></span></td>
<td style="width: 46.4305%; height: 23px;"><span style="font-family: helvetica, arial, sans-serif;"><strong>指定したシート名の列の最終行番号</strong></span></td>
<td style="width: 28.7001%; height: 23px;"><span style="font-family: helvetica, arial, sans-serif;"><strong>-</strong></span></td>
</tr>
<tr style="height: 23px;">
<td style="width: 24.8693%; height: 23px; background-color: #fffcd9;"><span style="font-family: helvetica, arial, sans-serif;"><strong>第１引数</strong></span></td>
<td style="width: 46.4305%; height: 23px;"><strong>nowCell</strong></td>
<td style="width: 28.7001%; height: 23px;"><span style="font-family: helvetica, arial, sans-serif;"><strong>セル番号（A1、B2）等</strong></span></td>
</tr>
</tbody>
</table>
<p>&nbsp;</p>
<h3>セルの一番下から空白セルの位置まで上って移動</h3>
<p>次に「セルの一番下から空白セルの位置まで上って移動」する方法についてです。</p>
<p>&nbsp;</p>
<p>先程と同じようにこんな感じの１~５まで含まれる１列の表があるとします。</p>
<p>&nbsp;</p>
<p><img loading="lazy" decoding="async" src="https://lemon818.com/wp/wp-content/uploads/2018/11/excel-last-row-1.png" alt="" width="346" height="237" class="aligncenter size-full wp-image-5600" srcset="https://lemon818.com/wp/wp-content/uploads/2018/11/excel-last-row-1.png 346w, https://lemon818.com/wp/wp-content/uploads/2018/11/excel-last-row-1-300x205.png 300w" sizes="auto, (max-width: 346px) 100vw, 346px" /></p>
<p>&nbsp;</p>
<p>これと同じ列の一番下のセルを選択します。</p>
<p>&nbsp;</p>
<p><img loading="lazy" decoding="async" src="https://lemon818.com/wp/wp-content/uploads/2018/11/excel-last-row-3.png" alt="" width="470" height="177" class="aligncenter size-full wp-image-5603" srcset="https://lemon818.com/wp/wp-content/uploads/2018/11/excel-last-row-3.png 470w, https://lemon818.com/wp/wp-content/uploads/2018/11/excel-last-row-3-300x113.png 300w" sizes="auto, (max-width: 470px) 100vw, 470px" /></p>
<p>&nbsp;</p>
<p>そしてキーボードの「↑」キーを入力すれば<strong>表の</strong>一番下のセルに移動できます。</p>
<p>&nbsp;</p>
<p><img loading="lazy" decoding="async" src="https://lemon818.com/wp/wp-content/uploads/2018/11/excel-last-row-4.png" alt="" width="260" height="306" class="aligncenter size-full wp-image-5602" srcset="https://lemon818.com/wp/wp-content/uploads/2018/11/excel-last-row-4.png 260w, https://lemon818.com/wp/wp-content/uploads/2018/11/excel-last-row-4-255x300.png 255w" sizes="auto, (max-width: 260px) 100vw, 260px" /></p>
<p>&nbsp;</p>
<p>この処理を Excel VBAで実現すればOKっていうことです。</p>
<p>実際の Excel Vba での最終行のセル番号を取得する処理は下記になります。</p><pre class="crayon-plain-tag">Sub test() 

　　　Debug.Print getLastRow2("B2") 

End Sub

Function getLastRow2(row As String)

    getMaxRow = Cells(Rows.Count, Range(nowCell).Column).End(xlUp).row

End Function</pre><p>
&nbsp;</p>
<p>先程よりは少しややこしいです。</p>
<p>処理について順に説明します（例として B2セルの最終行を取得する場合を説明します）。</p>
<div class="sc_designlist ol square solid blue">
<ol>
<li>B2 セルから B2の列番号を取得を数字として取得します（たとえば B 列ならば「2」となります）。</li>
<li>次に Excel の最大行数 Rows.Count を取得し、これらを組み合わせてB列の最終セル番号（Cells(Rows.Count, Range(nowCell).Column)）とを取得します。</li>
<li>最後にB列の最終セル番号をEnd(xlUp) と上っていく処理にすればOKです。</li>
</ol>
</div>
<p>&nbsp;</p>
<p>今回はこの処理を getLastRow2 プロシージャとしてまとめました。</p>
<p>この関数（プロシージャ）の引数と戻り値は下記になります。</p>
<p>&nbsp;</p>
<table style="height: 116px; width: 78.0905%; border-collapse: collapse;" border="1" align="center">
<tbody>
<tr style="height: 24px; background-color: #0099e6;">
<td style="width: 33.3333%; height: 24px;"><span style="font-family: helvetica, arial, sans-serif;"><strong>項目名</strong></span></td>
<td style="width: 37.9665%; height: 24px;"><span style="font-family: helvetica, arial, sans-serif;"><strong>値</strong></span></td>
<td style="width: 28.7001%; height: 24px;"><span style="font-family: helvetica, arial, sans-serif;"><strong>説明</strong></span></td>
</tr>
<tr style="height: 23px;">
<td style="width: 33.3333%; height: 23px; background-color: #fffcd9;"><span style="font-family: helvetica, arial, sans-serif;"><strong>プロシージャ名</strong></span></td>
<td style="width: 37.9665%; height: 23px;"><span style="font-family: helvetica, arial, sans-serif;"><strong>getLastRow2</strong></span></td>
<td style="width: 28.7001%; height: 23px;"><span style="font-family: helvetica, arial, sans-serif;"><strong>最終行番号取得</strong></span></td>
</tr>
<tr style="height: 23px;">
<td style="width: 33.3333%; height: 23px; background-color: #fffcd9;"><span style="font-family: helvetica, arial, sans-serif;"><strong>戻り値</strong></span></td>
<td style="width: 37.9665%; height: 23px;"><span style="font-family: helvetica, arial, sans-serif;"><strong>指定したシート名の列の最終行番号</strong></span></td>
<td style="width: 28.7001%; height: 23px;"><span style="font-family: helvetica, arial, sans-serif;"><strong>-</strong></span></td>
</tr>
<tr style="height: 23px;">
<td style="width: 33.3333%; height: 23px; background-color: #fffcd9;"><span style="font-family: helvetica, arial, sans-serif;"><strong>第１引数</strong></span></td>
<td style="width: 37.9665%; height: 23px;"><strong>nowCell</strong></td>
<td style="width: 28.7001%; height: 23px;"><span style="font-family: helvetica, arial, sans-serif;"><strong>セル番号（A1、B2）等</strong></span></td>
</tr>
</tbody>
</table>
<p>&nbsp;</p>
<h3>二つの処理の違いについて</h3>
<p>「<span style="color: #ff0000;"><strong>これ結局何が違うの？</strong></span>」ってなると思います。</p>
<p>これらの違いは Excel の行の中に<strong><span style="color: #3366ff;">空白があるかどうか</span></strong>であり、</p>
<p>空白があると最終行の値が変わってきます。</p>
<p>&nbsp;</p>
<p><img loading="lazy" decoding="async" src="https://lemon818.com/wp/wp-content/uploads/2018/11/excel-last-row-5.png" alt="" width="508" height="376" class="aligncenter wp-image-5604" srcset="https://lemon818.com/wp/wp-content/uploads/2018/11/excel-last-row-5.png 484w, https://lemon818.com/wp/wp-content/uploads/2018/11/excel-last-row-5-300x222.png 300w" sizes="auto, (max-width: 508px) 100vw, 508px" /></p>
<p>&nbsp;</p>
<p>例えば表の上から列番号を確認するとExcelの処理では<strong>空白セル直前のセル行番号</strong>になりますが、一番下から確認すると<strong>一番下のセル行番号</strong>になります。</p>
<p>&nbsp;</p>
<p>これらのように違いがあるため、使い分けする必要があります。</p>
<p>&nbsp;</p>
<div class="sc_frame_wrap block orange">
<div class="sc_frame_title">ポイント！</div>
<div class="sc_frame ">
<div class="sc_frame_text">Excel 行の間に<span style="color: #3366ff;"><strong>空白セルがあるかどうか</strong></span>で取得できる値が変わる！</div>
</div>
</div>
<p>&nbsp;</p>
<h2><span style="font-family: helvetica, arial, sans-serif; font-size: 20px;">Excel の</span><span style="font-family: helvetica, arial, sans-serif; font-size: 20px;">表の最終列番号を取得する処理について</span></h2>
<p>次にExcel の表の最終列番号を取得する方法は２通りあります。</p>
<p>これは前章で説明した「最終行番号取得のノウハウ」と特に変わりありません。</p>
<p>&nbsp;</p>
<div class="sc_designlist ol square solid blue">
<ol>
<li>セルの元々の位置から空白セルの位置まで右に移動して移動</li>
<li>セルの一番右から空白セルの位置まで右に移動</li>
</ol>
</div>
<p>&nbsp;</p>
<p>こちらも「<span style="color: #ff0000;"><strong>は？どういうこと？？</strong></span>」と思われた方もいるかもしれません。</p>
<p>これがどういうことなのかわかりやすく説明します。</p>
<p>&nbsp;</p>
<h3>セルの元々の位置から空白セルの位置まで右に移動して移動</h3>
<p>まずは「セルの元々の位置から空白セルの位置まで右に移動して移動」について説明します。</p>
<p>こんな感じの１~５まで含まれる１行の表があるとします。</p>
<p>ここの一番左のセル番号（B2）を選択します。</p>
<p>&nbsp;</p>
<p><img loading="lazy" decoding="async" src="https://lemon818.com/wp/wp-content/uploads/2018/11/excel-last-col-1.png" alt="" width="538" height="156" class="aligncenter size-full wp-image-5607" srcset="https://lemon818.com/wp/wp-content/uploads/2018/11/excel-last-col-1.png 538w, https://lemon818.com/wp/wp-content/uploads/2018/11/excel-last-col-1-300x87.png 300w" sizes="auto, (max-width: 538px) 100vw, 538px" /></p>
<p>&nbsp;</p>
<p>そしてキーボードの「→」キーを入力すれば表の一番右のセルに移動できます。</p>
<p>&nbsp;</p>
<p><img loading="lazy" decoding="async" src="https://lemon818.com/wp/wp-content/uploads/2018/11/excel-last-col-2.png" alt="" width="552" height="141" class="aligncenter size-full wp-image-5608" srcset="https://lemon818.com/wp/wp-content/uploads/2018/11/excel-last-col-2.png 552w, https://lemon818.com/wp/wp-content/uploads/2018/11/excel-last-col-2-300x77.png 300w" sizes="auto, (max-width: 552px) 100vw, 552px" /></p>
<p>&nbsp;</p>
<p>この処理を Excel VBAで実現すればOKっていうことです。</p>
<p>実際の Excel Vba での最終列のセル番号を取得する処理は下記になります。</p><pre class="crayon-plain-tag">Sub test() 

    Debug.Print getLastCol("B2") 

End Sub

Function getLastCol(nowCell As String)

    getMaxCol = Range(nowCell).End(xlToRight).Column

End Function</pre><p>
&nbsp;</p>
<p>上記は B2セル（表の一番左）から End(xlToRight) と右にいくこと処理です。</p>
<p>今回はこの処理を getLastCol プロシージャとしてまとめました。</p>
<p>この関数（プロシージャ）の引数と戻り値は下記になります。</p>
<p>&nbsp;</p>
<table style="height: 116px; width: 78.0905%; border-collapse: collapse;" border="1" align="center">
<tbody>
<tr style="height: 24px; background-color: #0099e6;">
<td style="width: 33.3333%; height: 24px;"><span style="font-family: helvetica, arial, sans-serif;"><strong>項目名</strong></span></td>
<td style="width: 37.9665%; height: 24px;"><span style="font-family: helvetica, arial, sans-serif;"><strong>値</strong></span></td>
<td style="width: 28.7001%; height: 24px;"><span style="font-family: helvetica, arial, sans-serif;"><strong>説明</strong></span></td>
</tr>
<tr style="height: 23px;">
<td style="width: 33.3333%; height: 23px; background-color: #fcfce6;"><span style="font-family: helvetica, arial, sans-serif; color: #000000;"><strong>プロシージャ名</strong></span></td>
<td style="width: 37.9665%; height: 23px;"><span style="font-family: helvetica, arial, sans-serif;"><strong>getLastCol</strong></span></td>
<td style="width: 28.7001%; height: 23px;"><span style="font-family: helvetica, arial, sans-serif;"><strong>最終列番号取得</strong></span></td>
</tr>
<tr style="height: 23px;">
<td style="width: 33.3333%; height: 23px; background-color: #fcfce6;"><span style="font-family: helvetica, arial, sans-serif; color: #000000;"><strong>戻り値</strong></span></td>
<td style="width: 37.9665%; height: 23px;"><span style="font-family: helvetica, arial, sans-serif;"><strong>指定したシート名の列の最終列番号</strong></span></td>
<td style="width: 28.7001%; height: 23px;"><span style="font-family: helvetica, arial, sans-serif;"><strong>-</strong></span></td>
</tr>
<tr style="height: 23px;">
<td style="width: 33.3333%; height: 23px; background-color: #fcfce6;"><span style="font-family: helvetica, arial, sans-serif; color: #000000;"><strong>第１引数</strong></span></td>
<td style="width: 37.9665%; height: 23px;"><strong>nowCell </strong></td>
<td style="width: 28.7001%; height: 23px;"><span style="font-family: helvetica, arial, sans-serif;"><strong>セル番号（A1、B2）等</strong></span></td>
</tr>
</tbody>
</table>
<p>&nbsp;</p>
<h3>セルの一番右から空白セルの位置まで右に移動</h3>
<p>まずは「セルの一番右から空白セルの位置まで右に移動」について説明します。</p>
<p>まずはこんな感じの１~５まで含まれる１行の表があるとします。</p>
<p>&nbsp;</p>
<p><img loading="lazy" decoding="async" src="https://lemon818.com/wp/wp-content/uploads/2018/11/excel-last-col-1.png" alt="" width="538" height="156" class="aligncenter size-full wp-image-5607" srcset="https://lemon818.com/wp/wp-content/uploads/2018/11/excel-last-col-1.png 538w, https://lemon818.com/wp/wp-content/uploads/2018/11/excel-last-col-1-300x87.png 300w" sizes="auto, (max-width: 538px) 100vw, 538px" /></p>
<p>&nbsp;</p>
<p>ここで B列の最終行を選択します。</p>
<p>&nbsp;</p>
<p><img loading="lazy" decoding="async" src="https://lemon818.com/wp/wp-content/uploads/2018/11/excel-last-col-3.png" alt="" width="382" height="187" class="aligncenter size-full wp-image-5609" srcset="https://lemon818.com/wp/wp-content/uploads/2018/11/excel-last-col-3.png 382w, https://lemon818.com/wp/wp-content/uploads/2018/11/excel-last-col-3-300x147.png 300w" sizes="auto, (max-width: 382px) 100vw, 382px" /></p>
<p>&nbsp;</p>
<p>そしてキーボードの「←」キーを入力すれば表の一番右のセルに移動できます。</p>
<p>&nbsp;</p>
<p><img loading="lazy" decoding="async" src="https://lemon818.com/wp/wp-content/uploads/2018/11/excel-last-col-4.png" alt="" width="541" height="134" class="aligncenter wp-image-5610" srcset="https://lemon818.com/wp/wp-content/uploads/2018/11/excel-last-col-4.png 696w, https://lemon818.com/wp/wp-content/uploads/2018/11/excel-last-col-4-300x74.png 300w" sizes="auto, (max-width: 541px) 100vw, 541px" /></p>
<p>&nbsp;</p>
<p>この処理を Excel VBAで実現すればOKっていうことです。</p>
<p>実際の Excel Vba での最終列のセル番号を取得する処理は下記になります。</p>
<p>最終行のセル番号は下記になります。</p><pre class="crayon-plain-tag">Sub test() 

    Debug.Print getLastCol2("B2") 

End Sub

Function getLastCol2(nowCell As String)

    getLastCol2 = Cells(Range(nowCell).row, Columns.Count).End(xlToLeft).Column

End Function</pre><p>
&nbsp;</p>
<p>処理について順に説明します（例として B2セルの最終列を取得する場合を説明します）。</p>
<div class="sc_designlist ol square solid blue">
<ol>
<li>B2 セルから B2の行番号を取得を数字として取得します（たとえば B 列ならば「2」となります）。</li>
<li>次に Excel の最大列数 Columns.Count を取得し、これらを組み合わせてB列の最終セル番号 Cells(Range(nowCell).row, Columns.Count) とを取得します。</li>
<li>最後にB列の最終セル番号をEnd(xlToLeft) と右にいく処理にすればOKです。</li>
</ol>
</div>
<p>&nbsp;</p>
<p>この関数（プロシージャ）の引数と戻り値は下記になります。</p>
<p>&nbsp;</p>
<table style="height: 116px; width: 78.0905%; border-collapse: collapse;" border="1" align="center">
<tbody>
<tr style="height: 24px; background-color: #0099e6;">
<td style="width: 25.9665%; height: 24px;"><span style="font-family: helvetica, arial, sans-serif;"><strong>項目名</strong></span></td>
<td style="width: 45.3333%; height: 24px;"><span style="font-family: helvetica, arial, sans-serif;"><strong>値</strong></span></td>
<td style="width: 28.7001%; height: 24px;"><span style="font-family: helvetica, arial, sans-serif;"><strong>説明</strong></span></td>
</tr>
<tr style="height: 23px;">
<td style="width: 25.9665%; height: 23px; background-color: #fcfce6;"><span style="font-family: helvetica, arial, sans-serif; color: #000000;"><strong>プロシージャ名</strong></span></td>
<td style="width: 45.3333%; height: 23px;"><span style="font-family: helvetica, arial, sans-serif;"><strong>getLastCol2</strong></span></td>
<td style="width: 28.7001%; height: 23px;"><span style="font-family: helvetica, arial, sans-serif;"><strong>最終列番号取得</strong></span></td>
</tr>
<tr style="height: 23px;">
<td style="width: 25.9665%; height: 23px; background-color: #fcfce6;"><span style="font-family: helvetica, arial, sans-serif; color: #000000;"><strong>戻り値</strong></span></td>
<td style="width: 45.3333%; height: 23px;"><span style="font-family: helvetica, arial, sans-serif;"><strong>指定したシート名の列の最終列番号</strong></span></td>
<td style="width: 28.7001%; height: 23px;"><span style="font-family: helvetica, arial, sans-serif;"><strong>-</strong></span></td>
</tr>
<tr style="height: 23px;">
<td style="width: 25.9665%; height: 23px; background-color: #fcfce6;"><span style="font-family: helvetica, arial, sans-serif; color: #000000;"><strong>第1引数</strong></span></td>
<td style="width: 45.3333%; height: 23px;"><strong>nowCell </strong></td>
<td style="width: 28.7001%; height: 23px;"><span style="font-family: helvetica, arial, sans-serif;"><strong>セル番号（A1、B2）等</strong></span></td>
</tr>
</tbody>
</table>
<p>&nbsp;</p>
<h3>二つの処理の違いについて</h3>
<p>「<span style="color: #ff0000;"><strong>これ結局何が違うの？</strong></span>」ってなると思います。</p>
<p>これらの違いは Excel の列の中に<strong><span style="color: #3366ff;">空白があるかどうか</span></strong>であり、</p>
<p>空白があると最終列の値が変わってきます。</p>
<p>&nbsp;</p>
<p><img loading="lazy" decoding="async" src="https://lemon818.com/wp/wp-content/uploads/2018/11/excel-last-col-5.png" alt="" width="743" height="239" class="aligncenter size-full wp-image-5611" srcset="https://lemon818.com/wp/wp-content/uploads/2018/11/excel-last-col-5.png 743w, https://lemon818.com/wp/wp-content/uploads/2018/11/excel-last-col-5-300x97.png 300w" sizes="auto, (max-width: 743px) 100vw, 743px" /></p>
<p>&nbsp;</p>
<p>例えば表の上から列番号を確認するとExcelの処理では<strong>空白セル直前のセル行番号</strong>になりますが、一番下から確認すると<strong>一番左のセル行番号</strong>になります。</p>
<p>これらのように違いがあるため、使い分けする必要があります。</p>
<p>&nbsp;</p>
<div class="sc_frame_wrap block orange">
<div class="sc_frame_title">ポイント！</div>
<div class="sc_frame ">
<div class="sc_frame_text">Excel 列の間に<span style="color: #3366ff;"><strong>空白セルがあるかどうか</strong></span>で取得できる値が変わる！</div>
</div>
</div>
<p>&nbsp;</p>
<h2>最後に</h2>
<p>いかがでしたでしょうか？</p>
<p>今回はこんなことを紹介しました！</p>
<div class="sc_designlist ol square solid blue">
<ol>
<li>Excel の表の最終行番号を取得する処理について</li>
<li>Excel の表の最終列番号を取得する処理について</li>
</ol>
</div>
<p>&nbsp;</p>
<p>この記事が開発のお役に立てれば幸いです。</p>
<p>&nbsp;</p>
<p>ではでは。</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Excel マクロとは？初心者でもわかるマクロで「できること」まとめ！</title>
		<link>https://lemon818.com/excelmacro_hajimeyo/</link>
					<comments>https://lemon818.com/excelmacro_hajimeyo/#respond</comments>
		
		<dc:creator><![CDATA[Take]]></dc:creator>
		<pubDate>Sun, 28 Oct 2018 08:47:56 +0000</pubDate>
				<category><![CDATA[Excel]]></category>
		<category><![CDATA[vba]]></category>
		<category><![CDATA[マクロ]]></category>
		<category><![CDATA[作業効率化]]></category>
		<category><![CDATA[初心者]]></category>
		<guid isPermaLink="false">http://lemon818.com/?p=196</guid>

					<description><![CDATA[&#160; 「Excel（エクセル）」というソフトはご存じだと思いますが、 「Excel マクロ（VBA）」はご存じでしょうか？ &#160; Excel マクロ ・・・ Excel にプログラミング機能をもたせたもの…]]></description>
										<content:encoded><![CDATA[<p>&nbsp;</p>
<p>「Excel（エクセル）」というソフトはご存じだと思いますが、</p>
<p>「<strong>Excel マクロ</strong>（<strong>VBA）</strong>」はご存じでしょうか？</p>
<p>&nbsp;</p>
<div class="sc_frame_wrap blue">
<div class="sc_frame ">
<div class="sc_designlist ol square solid blue">
<p><strong>Excel マクロ ・・・ </strong>Excel にプログラミング機能をもたせたもの、自動化させた機能を意味する</p>
<p><strong>VBA ・・・ </strong>プログラミング言語を意味する</p>
</div>
</div>
</div>
<p>&nbsp;</p>
<p>いまは社会の大半の業務資料は Excel で作成されたものばかり。</p>
<p>そんな資料には 「<strong>Excel マクロ</strong>」が必ず含まれています。</p>
<p>&nbsp;</p>
<p>私はシステムエンジニアですが、業務上でそのような資料を扱うことが<strong>ものすごく多い</strong>です。</p>
<p>&nbsp;</p>
<p>「<span style="color: #ff0000;"><strong>Excel なんてどうでもいいやろ～</strong></span>」</p>
<p>そう思われている人ほどぜひこの記事を読んでいただきたいです。</p>
<p>私の経験ですが<span style="color: #0000ff;"><strong>「Excel」ができる人は、人の10倍仕事がはやくできます。</strong></span></p>
<p>私の知っている優秀なエンジニアであり上司の人は、<strong>全員達人レベルで「Excel」ができていました。</strong></p>
<p>&nbsp;</p>
<p>凄い人は Excel で 1か月のカレンダーを５秒から 10 秒くらいで作っていました。</p>
<p>すべての業務（雑務）を Excel 化して自動化している人までいました。</p>
<p>&nbsp;</p>
<p>私はそういう優秀な人たちの背中をみて「<span style="color: #0000ff;"><strong>いつかこんなエンジニアになりたい！</strong></span>」と思い、</p>
<p>独学で Excel を勉強しました。</p>
<p>&nbsp;</p>
<p>この記事では、そんな私が「Excel マクロ（VBA）」の魅力をたっぷりご紹介します！</p>
<div class="sc_frame_wrap block orange">
<div class="sc_frame_title">この記事でわかること！</div>
<div class="sc_frame ">
<div class="sc_designlist ol square solid orange">
<ol>
<li>Excel マクロとは？</li>
<li>VBA を学ぶことのメリット</li>
<li>VBA で作成した作業自動化の具体的をご紹介！</li>
</ol>
</div>
</div>
</div>
<p>&nbsp;</p>
<h2><span style="font-family: helvetica, arial, sans-serif; font-size: 24px;">Excel マクロとは？</span></h2>
<p><span style="font-family: helvetica, arial, sans-serif; font-size: 16px;"><span style="color: #000000;">Excel マクロとは、VBA （Visual Basic dor Application）と呼ばれるプログラミング言語で記述された</span></span></p>
<p><span style="font-family: helvetica, arial, sans-serif; font-size: 16px;"><strong><span style="color: #3366ff;">「</span></strong><strong><span style="color: #3366ff;">Excelでの操作を自動化で実行できる」</span></strong>機能のことです。</span></p>
<p>&nbsp;</p>
<p><span style="font-family: helvetica, arial, sans-serif; font-size: 16px;">Excel マクロを使えるようにするためには VBA を自由自在に使いこなせるようになる必要があります。</span></p>
<p><span style="font-family: helvetica, arial, sans-serif; font-size: 16px;">つまり「Excelマクロ を学ぶ」ことは、「VBA を学ぶ」ことと同義と考えていただいて問題ありません。</span></p>
<p>&nbsp;</p>
<p><span style="font-family: helvetica, arial, sans-serif;"><img loading="lazy" decoding="async" class="aligncenter wp-image-1474" src="https://lemon818.com/wp/wp-content/uploads/2018/10/image_excel_hajimeyo.png" alt="" width="762" height="387" srcset="https://lemon818.com/wp/wp-content/uploads/2018/10/image_excel_hajimeyo.png 1337w, https://lemon818.com/wp/wp-content/uploads/2018/10/image_excel_hajimeyo-300x152.png 300w, https://lemon818.com/wp/wp-content/uploads/2018/10/image_excel_hajimeyo-768x389.png 768w, https://lemon818.com/wp/wp-content/uploads/2018/10/image_excel_hajimeyo-1024x519.png 1024w, https://lemon818.com/wp/wp-content/uploads/2018/10/image_excel_hajimeyo-660x335.png 660w" sizes="auto, (max-width: 762px) 100vw, 762px" /></span></p>
<p>&nbsp;</p>
<h2><span style="font-family: helvetica, arial, sans-serif; font-size: 24px;">VBA を学ぶことのメリット</span></h2>
<p>VBA を学ぶことのメリットについて解説します。</p>
<div class="sc_frame_wrap blue">
<div class="sc_frame ">
<div class="sc_designlist ol square solid blue">
<ol>
<li>Office ソフトでの作業が全て自動化できる</li>
<li>学習コストが低く、すぐに書けるようになる</li>
<li>キャリアアップにつながる</li>
<li>フリーランスとしても生きていける</li>
</ol>
</div>
</div>
</div>
<p>&nbsp;</p>
<h3><span style="font-family: helvetica, arial, sans-serif;">Office ソフトでの作業が全て自動化できる</span></h3>
<p><span style="font-family: helvetica, arial, sans-serif;"><img loading="lazy" decoding="async" class="aligncenter wp-image-1490 size-large" src="https://lemon818.com/wp/wp-content/uploads/2018/11/Excel_1542439986-1024x724.png" alt="" width="665" height="470" srcset="https://lemon818.com/wp/wp-content/uploads/2018/11/Excel_1542439986-1024x724.png 1024w, https://lemon818.com/wp/wp-content/uploads/2018/11/Excel_1542439986-300x212.png 300w, https://lemon818.com/wp/wp-content/uploads/2018/11/Excel_1542439986-768x543.png 768w, https://lemon818.com/wp/wp-content/uploads/2018/11/Excel_1542439986.png 1280w" sizes="auto, (max-width: 665px) 100vw, 665px" /></span></p>
<p>&nbsp;</p>
<p><span style="font-family: helvetica, arial, sans-serif; font-size: 16px;">マイクロソフト製の Office ソフト（Excel、Word、Power Point、Outlook）には<strong>この<span style="color: #000000;">マクロ</span>機能が搭載されています。</strong></span></p>
<p><span style="font-family: helvetica, arial, sans-serif; font-size: 16px;">社会人での資料のほとんどが（私の会社では全てが）が Office ソフトで作成します。</span></p>
<p>&nbsp;</p>
<div class="sc_frame_wrap blue">
<div class="sc_frame ">
<div class="sc_designlist ol square solid blue">
<ol>
<li><span style="font-family: helvetica, arial, sans-serif; font-size: 16px;">スライド発表 → Power Point </span></li>
<li><span style="font-family: helvetica, arial, sans-serif; font-size: 16px;">設計書、データの集計、統計 → Excel </span></li>
<li><span style="font-family: helvetica, arial, sans-serif; font-size: 16px;">報告書 → Word</span></li>
<li><span style="font-family: helvetica, arial, sans-serif; font-size: 16px;">メール → Outlook</span></li>
</ol>
</div>
</div>
</div>
<p>&nbsp;</p>
<p><span style="font-family: helvetica, arial, sans-serif; font-size: 16px;">それら<strong>全てにマクロ機能が搭載</strong>されています。</span></p>
<p><span style="font-family: helvetica, arial, sans-serif; font-size: 16px;">つまりVBA を学べば Office ソフト上での作業が<strong>すべて自動化</strong>できます。</span></p>
<p>そのため<span style="font-family: helvetica, arial, sans-serif; font-size: 16px;">作業効率が飛躍的に向上します。</span></p>
<p>&nbsp;</p>
<p><span style="font-family: helvetica, arial, sans-serif; font-size: 16px; color: #000000;">管理人も雑務はすべて Excel マクロをつかっており、<span style="color: #0000ff;"><strong>雑務時間が大幅に短縮できました。</strong></span></span></p>
<p><span style="font-family: helvetica, arial, sans-serif; font-size: 16px; color: #000000;">どれくらい早くなったかExcel マクロを使う前と使った後で比較したところ、</span></p>
<p><span style="font-family: helvetica, arial, sans-serif; font-size: 16px; color: #000000;">同じ作業でなんと<span style="color: #0000ff;"><strong>４倍</strong></span>早く終わるようになりました。</span></p>
<p>&nbsp;</p>
<h3><span style="font-family: helvetica, arial, sans-serif;">学習コストが低く、すぐに書けるようになる</span></h3>
<p><span style="font-family: helvetica, arial, sans-serif;"><img loading="lazy" decoding="async" class="size-large wp-image-1494 aligncenter" src="https://lemon818.com/wp/wp-content/uploads/2018/11/Easy_1542440197-1024x642.jpg" alt="" width="665" height="417" srcset="https://lemon818.com/wp/wp-content/uploads/2018/11/Easy_1542440197-1024x642.jpg 1024w, https://lemon818.com/wp/wp-content/uploads/2018/11/Easy_1542440197-300x188.jpg 300w, https://lemon818.com/wp/wp-content/uploads/2018/11/Easy_1542440197-768x482.jpg 768w, https://lemon818.com/wp/wp-content/uploads/2018/11/Easy_1542440197.jpg 1280w" sizes="auto, (max-width: 665px) 100vw, 665px" /></span></p>
<p>&nbsp;</p>
<p><span style="font-family: helvetica, arial, sans-serif; font-size: 16px;">一般的なプログラミング言語の難易度は、記述するプログラミング言語が人間により理解しやすいものかどうかで決定します。</span></p>
<p><span style="font-family: helvetica, arial, sans-serif; font-size: 16px;">人間に理解しやすいプログラミング言語を<strong>高級言語</strong>、理解しにくいものを<strong>低級言語</strong>といいます。</span></p>
<p>&nbsp;</p>
<p><span style="font-family: helvetica, arial, sans-serif; font-size: 16px;">一般的に VBA は高級言語に属するプログラミング言語です。</span></p>
<p><span style="font-family: helvetica, arial, sans-serif; font-size: 16px;">つまり<span style="color: #0000ff;"><strong> Excel マクロ（VBA）はそんなに書いていて難しくない（理解しやすい）プログラミング言語</strong></span>なんです。</span></p>
<p>&nbsp;</p>
<p><span style="font-family: helvetica, arial, sans-serif; font-size: 16px;">私は過去にさまざまプログラミング言語（C 、C++、Java、Pythonなど）を勉強してきました。</span></p>
<p><span style="font-family: helvetica, arial, sans-serif;">その中で</span><span style="font-family: helvetica, arial, sans-serif; font-size: 16px;"><span style="color: #0000ff;"><strong>一番簡単だったのが Excel VBA</strong> </span>でした。</span></p>
<p><span style="font-family: helvetica, arial, sans-serif; font-size: 16px;">つまり、プログラミング言語を初めて書く勉強する人にも<strong>学習コストが低く</strong></span></p>
<p><span style="font-family: helvetica, arial, sans-serif; font-size: 16px;">すぐに書けるようになるプログラミング言語です。</span></p>
<p>&nbsp;</p>
<h3><span style="font-family: helvetica, arial, sans-serif;">キャリアアップにつながる</span></h3>
<p>&nbsp;</p>
<p><span style="font-family: helvetica, arial, sans-serif;"><img loading="lazy" decoding="async" class=" wp-image-1492 aligncenter" src="https://lemon818.com/wp/wp-content/uploads/2018/11/b8cfd4d88c9bb4fc5d7dd71557c48f35-1024x1024.jpg" alt="" width="474" height="474" srcset="https://lemon818.com/wp/wp-content/uploads/2018/11/b8cfd4d88c9bb4fc5d7dd71557c48f35-1024x1024.jpg 1024w, https://lemon818.com/wp/wp-content/uploads/2018/11/b8cfd4d88c9bb4fc5d7dd71557c48f35-150x150.jpg 150w, https://lemon818.com/wp/wp-content/uploads/2018/11/b8cfd4d88c9bb4fc5d7dd71557c48f35-300x300.jpg 300w, https://lemon818.com/wp/wp-content/uploads/2018/11/b8cfd4d88c9bb4fc5d7dd71557c48f35-768x768.jpg 768w, https://lemon818.com/wp/wp-content/uploads/2018/11/b8cfd4d88c9bb4fc5d7dd71557c48f35.jpg 1280w" sizes="auto, (max-width: 474px) 100vw, 474px" /></span></p>
<p>&nbsp;</p>
<p><span style="font-family: helvetica, arial, sans-serif; font-size: 16px;">エンジニアでよく見るのが「<span style="color: #ff0000;"><strong>Excel マクロは知っているが、やり方がよくわからない</strong></span>」という人です。</span></p>
<p><span style="font-family: helvetica, arial, sans-serif; font-size: 16px;">プログラミングをしたことがない人は、最初のうちは VBA の書き方を覚えるのに少し時間がかかると思います。</span></p>
<p>&nbsp;</p>
<p><span style="font-family: helvetica, arial, sans-serif; font-size: 16px;">そういった壁を感じてしまいやらない人が結構多く、その</span><span style="font-family: helvetica, arial, sans-serif; font-size: 16px;">ため VBA の書き方を知らない人が多いのです。</span></p>
<p>&nbsp;</p>
<p><span style="font-family: helvetica, arial, sans-serif; font-size: 16px;">そのため Excel マクロの 仕組みさえ理解して VBA を自由自在に書けるようになれば、</span></p>
<p><span style="font-family: helvetica, arial, sans-serif; font-size: 16px;"><span style="color: #0000ff;"><strong>お仕事の依頼がめちゃくちゃもらえます。</strong></span></span></p>
<p>&nbsp;</p>
<p><span style="font-family: helvetica, arial, sans-serif; font-size: 16px;">私は職場では VBA を書ける数少ないプログラマーなので、</span></p>
<p><span style="font-family: helvetica, arial, sans-serif; font-size: 16px;">よく「こういうことしたいんやけど、 Excel マクロで作ってくれへん？」という依頼をもらいます。</span></p>
<p><span style="font-family: helvetica, arial, sans-serif; font-size: 16px;">そういった依頼をこなしていると、「こいつはできるやつ」と上司に評価されるようになります。</span></p>
<p><span style="font-family: helvetica, arial, sans-serif; font-size: 16px;">そうなることで最終的に、<strong>キャリアアップにもつながります。</strong></span></p>
<p>&nbsp;</p>
<h3><span style="font-family: helvetica, arial, sans-serif;">フリーランスとしても生きていける</span></h3>
<p><span style="font-family: helvetica, arial, sans-serif;"><img loading="lazy" decoding="async" class="size-large wp-image-1493 aligncenter" src="https://lemon818.com/wp/wp-content/uploads/2018/11/142096ae7b98426fd9cfdf400d9f3830-1024x681.jpg" alt="" width="665" height="442" srcset="https://lemon818.com/wp/wp-content/uploads/2018/11/142096ae7b98426fd9cfdf400d9f3830-1024x681.jpg 1024w, https://lemon818.com/wp/wp-content/uploads/2018/11/142096ae7b98426fd9cfdf400d9f3830-300x199.jpg 300w, https://lemon818.com/wp/wp-content/uploads/2018/11/142096ae7b98426fd9cfdf400d9f3830-768x511.jpg 768w, https://lemon818.com/wp/wp-content/uploads/2018/11/142096ae7b98426fd9cfdf400d9f3830.jpg 1280w" sizes="auto, (max-width: 665px) 100vw, 665px" /></span></p>
<p>&nbsp;</p>
<p><span style="font-family: helvetica, arial, sans-serif; font-size: 16px;">フリーランスとは、<strong>会社に所属せず個人で自由に仕事を契約する人のこと</strong>です。</span></p>
<p>&nbsp;</p>
<p><span style="font-family: helvetica, arial, sans-serif; font-size: 16px;">会社に所属しないため、職場でのめんどくさい人間関係や朝の決まった出勤などがいっさいなく自由に働くことができます。</span></p>
<p><span style="font-family: helvetica, arial, sans-serif; font-size: 16px;">SE（システムエンジニア）が一番あこがれる職業といっても過言ではありません。</span></p>
<p><span style="font-family: helvetica, arial, sans-serif; font-size: 16px;">その自由に憧れたせいか、よく知り合いの SE は仕事をやめてフリーランスになる人が多いです。</span></p>
<p>&nbsp;</p>
<p><span style="font-family: helvetica, arial, sans-serif; font-size: 16px;">Excel VBA フリーランスの単価は、一般的には<span style="color: #0000ff;"><strong>40万から70万</strong></span>です（多いところは200万というのも見かけました）。</span></p>
<p><span style="font-family: helvetica, arial, sans-serif; font-size: 16px;">案件も多く VBA を勉強することは、<span style="color: #0000ff;"><strong>フリーランスとして一生食べていけます</strong></span>。</span></p>
<p>※ <span style="font-family: helvetica, arial, sans-serif; font-size: 16px;">ネットで「Excel VBA フリーランス」で検索してみてください。様々な案件がヒットすると思います。</span></p>
<p>&nbsp;</p>
<h2><span style="font-family: helvetica, arial, sans-serif; font-size: 24px;">VBA で作成した作業自動化の具体的例</span></h2>
<p><span style="font-family: helvetica, arial, sans-serif; font-size: 16px;">私は VBA で以下の作業を自動化しています。</span></p>
<p>&nbsp;</p>
<div class="sc_frame_wrap block orange">
<div class="sc_frame_title">自動化したものまとめ！</div>
<div class="sc_frame ">
<div class="sc_designlist ol square solid orange">
<ol>
<li><span style="font-family: helvetica, arial, sans-serif; font-size: 16px;">Windows フォルダのサイズをグラフで出力（どれが大きいファイルか一目でわかる！）</span><span style="font-family: helvetica, arial, sans-serif; font-size: 16px;"></span></li>
<li><span style="font-family: helvetica, arial, sans-serif; font-size: 16px;">システムテスト設計書作成の自動化</span><span style="font-family: helvetica, arial, sans-serif; font-size: 16px;"></span></li>
<li><span style="font-family: helvetica, arial, sans-serif; font-size: 16px;">データの自動変換 とファイルの自動出力</span><span style="font-family: helvetica, arial, sans-serif; font-size: 16px;"></span></li>
<li><span style="font-family: helvetica, arial, sans-serif; font-size: 16px;">キーワード 自動組み合わせツール</span><span style="font-family: helvetica, arial, sans-serif; font-size: 16px;"></span></li>
<li><span style="font-family: helvetica, arial, sans-serif; font-size: 16px;">スクリーンショットボタンを押下することで Excel ファイルに自動添付</span><span style="font-family: helvetica, arial, sans-serif; font-size: 16px;"></span></li>
<li><span style="font-family: helvetica, arial, sans-serif; font-size: 16px;">200 枚近くある画像を Excel にすべて同じ行間隔で添付</span><span style="font-family: helvetica, arial, sans-serif; font-size: 16px;"></span></li>
<li><span style="font-family: helvetica, arial, sans-serif; font-size: 16px;">作業工数の自動集計</span><span style="font-family: helvetica, arial, sans-serif; font-size: 16px;"></span></li>
<li><span style="font-family: helvetica, arial, sans-serif; font-size: 16px;">Outlook の毎日送るメールを自動作成（メール作成日を自動挿入） .... など</span></li>
</ol>
</div>
</div>
</div>
<p>&nbsp;</p>
<p><span style="font-family: helvetica, arial, sans-serif; font-size: 16px;">これはほんの一部ですが、仕事では 100 近くのマクロを作成し作業を自動化しています。</span></p>
<p><span style="font-family: helvetica, arial, sans-serif; font-size: 16px;">これのいいところは一度作成する手間をかけると、あとは<strong>コーヒーを飲んでいるうちに作業が終わります</strong>。</span></p>
<p>&nbsp;</p>
<p><span style="font-family: helvetica, arial, sans-serif; font-size: 16px;">他にも<span style="color: #0000ff;"><strong>このサイトでは Excel マクロで作成した様々なツールがあります。</strong></span></span></p>
<p><span style="font-family: helvetica, arial, sans-serif; font-size: 16px;">ぜひそちらも参考にしてみてください。</span></p>
<p>※ Excel で ゲームも作成可能です。よければ以下の記事も参考にしてみてください♪</p>
<div class="sc_getpost"><a class="clearfix" href="https://lemon818.com/excel-tetoris/" ><div class="sc_getpost_thumb post-box-thumbnail__wrap"><img loading="lazy" decoding="async" src="https://lemon818.com/wp/wp-content/uploads/2019/02/excel-tetoris-1-150x150.png" width="150" height="150" alt="エクセルで遊べるテトリスの作り方【Vba ゲーム】"></div><div class="title">エクセルで遊べるテトリスの作り方【Vba ゲーム】</div><div class="date">2019.2.20</div><div class="substr">エクセルでテトリスを作りました！！  前からつくってみたいなあと思っていたゲームの一つで、これもなかなか時間がかかりました。 仕事終わってからだらだら作っていたら一週間くらいかかりました（工数はだいたい１人日～２人日くらい）。  ですが、思ったより簡単に作れました。エクセルで過去にオセロや将棋などつ...</div></a></div>
<p>&nbsp;</p>
<h2><span style="font-family: helvetica, arial, sans-serif; font-size: 24px;">最後に</span></h2>
<p><span style="font-family: helvetica, arial, sans-serif; font-size: 16px;">さていかがでしたでしょうか？</span></p>
<p><span style="font-family: helvetica, arial, sans-serif; font-size: 16px;">私も最初のうちは「Excel って表つくるやつやろ？」と思っていました。</span></p>
<p><span style="font-family: helvetica, arial, sans-serif; font-size: 16px;">しかしExcel VBA を学ぶことで、Excel は <span style="color: #0000ff;"><strong>Windows 上の作業を何でもできる最高のソフト</strong></span>という考えになりました。</span></p>
<p>&nbsp;</p>
<p><span style="font-family: helvetica, arial, sans-serif; font-size: 16px;">ぜひ皆さんも Excel VBA を勉強してみてください。</span></p>
<p><span style="font-family: helvetica, arial, sans-serif; font-size: 16px;">最後までご精読いただきありがとうございます。 </span></p>
]]></content:encoded>
					
					<wfw:commentRss>https://lemon818.com/excelmacro_hajimeyo/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
