<?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://dokugakuexcel.com/category/%E3%83%9E%E3%82%AF%E3%83%ADvba%E5%BF%9C%E7%94%A8%E3%83%BB%E4%BE%BF%E5%88%A9%E6%8A%80/%E3%82%A8%E3%83%A9%E3%83%BC%E5%AF%BE%E7%AD%96/feed/" rel="self" type="application/rss+xml" />
	<link>https://dokugakuexcel.com</link>
	<description>初心者から中級者までシステム・関数・VBAを伝授します</description>
	<lastBuildDate>Fri, 30 Dec 2022 13:57:56 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://dokugakuexcel.com/wp-content/uploads/2021/06/cropped-cropped-image-66-1-32x32.jpg</url>
	<title>エラー対策  |  独学エクセル塾</title>
	<link>https://dokugakuexcel.com</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">194648711</site>	<item>
		<title>【マクロVBA】実行中のコードを強制停止・終了する方法！キーボードのみで操作可能</title>
		<link>https://dokugakuexcel.com/%e3%80%90%e3%83%9e%e3%82%af%e3%83%advba%e3%80%91%e5%ae%9f%e8%a1%8c%e4%b8%ad%e3%81%ae%e3%82%b3%e3%83%bc%e3%83%89%e3%82%92%e5%bc%b7%e5%88%b6%e5%81%9c%e6%ad%a2%e3%83%bb%e7%b5%82%e4%ba%86%e3%81%99/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=%25e3%2580%2590%25e3%2583%259e%25e3%2582%25af%25e3%2583%25advba%25e3%2580%2591%25e5%25ae%259f%25e8%25a1%258c%25e4%25b8%25ad%25e3%2581%25ae%25e3%2582%25b3%25e3%2583%25bc%25e3%2583%2589%25e3%2582%2592%25e5%25bc%25b7%25e5%2588%25b6%25e5%2581%259c%25e6%25ad%25a2%25e3%2583%25bb%25e7%25b5%2582%25e4%25ba%2586%25e3%2581%2599</link>
					<comments>https://dokugakuexcel.com/%e3%80%90%e3%83%9e%e3%82%af%e3%83%advba%e3%80%91%e5%ae%9f%e8%a1%8c%e4%b8%ad%e3%81%ae%e3%82%b3%e3%83%bc%e3%83%89%e3%82%92%e5%bc%b7%e5%88%b6%e5%81%9c%e6%ad%a2%e3%83%bb%e7%b5%82%e4%ba%86%e3%81%99/?noamp=mobile#respond</comments>
		
		<dc:creator><![CDATA[matsu]]></dc:creator>
		<pubDate>Fri, 30 Dec 2022 13:55:43 +0000</pubDate>
				<category><![CDATA[便利技]]></category>
		<category><![CDATA[エラー対策]]></category>
		<category><![CDATA[停止]]></category>
		<category><![CDATA[コードの実行]]></category>
		<category><![CDATA[中断]]></category>
		<category><![CDATA[強制終了]]></category>
		<category><![CDATA[Esc]]></category>
		<category><![CDATA[エスケープ]]></category>
		<guid isPermaLink="false">https://dokugakuexcel.com/?p=3970</guid>

					<description><![CDATA[マクロVBAを使用する上で誤って別のプロシージャを実行したり処理時間が長くてコードの実行を途中で止めたい場面があると思います。 その際に通常のメニューにある実行の停止はグレーアウトして選択できないため、プログラム実行が終 [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p>マクロVBAを使用する上で誤って別のプロシージャを実行したり<br>処理時間が長くて<strong><span class="marker-under">コードの実行を途中で止めたい場面</span></strong>があると思います。</p>



<p>その際に通常のメニューにある実行の停止はグレーアウトして<br>選択できないため、プログラム実行が終わるまで待つ方もいるのではないでしょうか。</p>



<p>そこで今回は<strong><span class="marker-under">キーボード操作のみでできる実行中のコードを<br>停止させる方法</span></strong>について紹介します。</p>



<p>作業自体もとても簡単なので是非参考にしてみてください。<br>それでは見ていきましょう。</p>




  <div id="toc" class="toc tnt-number toc-center tnt-number border-element"><input type="checkbox" class="toc-checkbox" id="toc-checkbox-2" checked><label class="toc-title" for="toc-checkbox-2">目次</label>
    <div class="toc-content">
    <ol class="toc-list open"><li><a href="#toc1" tabindex="0">&#x2b1b;︎こんなことができるようになる！</a></li><li><a href="#toc2" tabindex="0">&#x2b1b;︎実行コードを強制停止・終了する</a></li><li><a href="#toc3" tabindex="0">&#x2b1b;︎停止後の復帰方法</a><ol><li><a href="#toc4" tabindex="0">・完全に実行を中断したい場合</a></li><li><a href="#toc5" tabindex="0">・どこまでのコードを実行したか確認して終了する</a></li><li><a href="#toc6" tabindex="0">・コード実行を再開したい場合</a></li></ol></li><li><a href="#toc7" tabindex="0">■Escでも強制終了できない場合</a></li><li><a href="#toc8" tabindex="0">&#x2b1b;︎まとめ</a></li></ol>
    </div>
  </div>

<h2 class="wp-block-heading"><span id="toc1">&#x2b1b;︎こんなことができるようになる！</span></h2>



<p>・実行中のプロシージャを強制的に途中停止できる。<br>・処理中のコードを途中停止できる</p>



<h2 class="wp-block-heading"><span id="toc2">&#x2b1b;︎実行コードを強制停止・終了する</span></h2>



<p>では本題に入っていきましょう。<br>見本で下記のような簡単なコードを用意しました。</p>



<pre class="wp-block-verse">Sub 繰り返し処理()
Dim Time1 As Double, Time2 As Double, Result As Double
Dim Cycle As Long

Time1 = Timer
For Cycle = 1 To 1000 Step 1
 Cells(1, 1) = "=SUM(D:D)"
 Cells(1, 1) = "=SUM(D:D)" 
 Cells(1, 1) = "=SUM(D:D)"
Next Cycle

Time2 = Timer
Result = Time2 - Time1
MsgBox Result &amp; "秒で処理しました"
End Sub
</pre>



<p>内容としてはセル番地A1からA3にＤ列の合計の算出を<br>SUM関数を用いて<strong><span class="marker-under-red">10000回繰り返すコード</span></strong>になります。</p>



<figure class="wp-block-image size-full"><img fetchpriority="high" decoding="async" width="519" height="153" src="https://dokugakuexcel.com/wp-content/uploads/2022/12/image-88.png" alt="" class="wp-image-3971" srcset="https://dokugakuexcel.com/wp-content/uploads/2022/12/image-88.png 519w, https://dokugakuexcel.com/wp-content/uploads/2022/12/image-88-300x88.png 300w" sizes="(max-width: 519px) 100vw, 519px" /></figure>



<p>見本なので無意味な繰り返しをしていますが<br>こちらを<strong><span class="marker-under">実行すると「138秒」かかってしまいます</span></strong>。</p>



<figure class="wp-block-image size-full is-resized"><img decoding="async" src="https://dokugakuexcel.com/wp-content/uploads/2022/12/image-89.png" alt="" class="wp-image-3972" width="202" height="153"/></figure>



<p>一度<strong><span class="marker-under-blue">実行してしまうと止まるまでに約２分待たないといけない</span></strong>ので<br>誤動作時にはとてもめんどくさいです。<br>こちらを<strong><span class="marker-under">実行中に途中で止めてみましょう</span></strong>。</p>



<p>使用するキーは<strong><span class="marker-under">「エスケープキー」</span></strong>になります。<br>大半のキーボードでは左上に配置されており<strong><span class="marker-under">「Esc」</span></strong>と記されていると思います。</p>



<p>では実際に「Escキー」を活用してみましょう。<br>活用方法は簡単で、<strong><span class="marker-under-red">実行中に押すのみです</span></strong>。</p>



<p>では先程のコードを実行して「Escキー」を押してみたいと思います。<br>結果はこのようになりました。</p>



<figure class="wp-block-image size-full is-resized"><img decoding="async" src="https://dokugakuexcel.com/wp-content/uploads/2022/12/image-90.png" alt="" class="wp-image-3973" width="454" height="217" srcset="https://dokugakuexcel.com/wp-content/uploads/2022/12/image-90.png 611w, https://dokugakuexcel.com/wp-content/uploads/2022/12/image-90-300x143.png 300w" sizes="(max-width: 454px) 100vw, 454px" /></figure>



<p>「コードの実行が中断されました。」というメッセージが表示されました。<br>よく、エラー時に表示されるメッセージです。</p>



<p>では確認も含めてデバッグを押してみましょう。</p>



<figure class="wp-block-image size-full"><img decoding="async" width="528" height="211" src="https://dokugakuexcel.com/wp-content/uploads/2022/12/image-91.png" alt="" class="wp-image-3974" srcset="https://dokugakuexcel.com/wp-content/uploads/2022/12/image-91.png 528w, https://dokugakuexcel.com/wp-content/uploads/2022/12/image-91-300x120.png 300w" sizes="(max-width: 528px) 100vw, 528px" /></figure>



<p>変数Nは本来であれば<strong><span class="marker-under-red">10000回繰り返しますが<br>543回で止まっています</span></strong>。</p>



<p>つまり<strong><span class="marker-under">強制的に停止が出来たことになります。</span></strong></p>



<p>とても簡単ですね。</p>



<h2 class="wp-block-heading"><span id="toc3">&#x2b1b;︎停止後の復帰方法</span></h2>



<p>ここからは<strong><span class="marker-under">停止の復帰方法</span></strong>です。<br>デバッグ作業をされた事がある方は復習になりますが<br>一連の復帰方法を見ていきましょう。</p>



<h3 class="wp-block-heading"><span id="toc4">・完全に実行を中断したい場合</span></h3>



<p>現在の状態はあくまで強制停止状態にあります。<br>なので、そこから再度実行もできますし、デバッグもできます。</p>



<p>今回は<strong><span class="marker-under-red">完全に実行を中断</span></strong>するわけですから<br>先程のメッセージから<strong><span class="marker-under-red">「終了」</span></strong>を押します。</p>



<figure class="wp-block-image size-full"><img decoding="async" width="611" height="291" src="https://dokugakuexcel.com/wp-content/uploads/2022/12/image-92.png" alt="" class="wp-image-3975" srcset="https://dokugakuexcel.com/wp-content/uploads/2022/12/image-92.png 611w, https://dokugakuexcel.com/wp-content/uploads/2022/12/image-92-300x143.png 300w" sizes="(max-width: 611px) 100vw, 611px" /></figure>



<p>これで強制終了が終わりました。</p>



<p>注意点として、<strong><span class="marker-under">どこまでのコードが実行されたかは<br>確認が出来ないのでシートを確認して現状を把握する必要があります。</span></strong></p>



<p>どこまでのコードが実行されたか確認する方法は次の項目で説明します。</p>



<h3 class="wp-block-heading"><span id="toc5">・どこまでのコードを実行したか確認して終了する</span></h3>



<p>今度はどこまでのコードが実行されたか確認して終了する手順を紹介します。</p>



<p>「Escキー」を押した後にでるメニューか<strong><span class="marker-under-red">「デバッグ」</span></strong>を選択します。</p>



<figure class="wp-block-image size-full"><img decoding="async" width="611" height="291" src="https://dokugakuexcel.com/wp-content/uploads/2022/12/image-93.png" alt="" class="wp-image-3976" srcset="https://dokugakuexcel.com/wp-content/uploads/2022/12/image-93.png 611w, https://dokugakuexcel.com/wp-content/uploads/2022/12/image-93-300x143.png 300w" sizes="(max-width: 611px) 100vw, 611px" /></figure>



<p>すると<strong><span class="marker-under">黄色く塗りつぶされた行があるのですか<br>そこまでコードが実行されたことを示します。</span></strong></p>



<figure class="wp-block-image size-full"><img decoding="async" width="604" height="211" src="https://dokugakuexcel.com/wp-content/uploads/2022/12/image-94.png" alt="" class="wp-image-3977" srcset="https://dokugakuexcel.com/wp-content/uploads/2022/12/image-94.png 604w, https://dokugakuexcel.com/wp-content/uploads/2022/12/image-94-300x105.png 300w" sizes="(max-width: 604px) 100vw, 604px" /></figure>



<p>見本のように繰り返し動作の場合は変数を確認すれば何回まで繰り返し処理が行われたか確認することが出来ます。</p>



<p>その後、「<strong><span class="marker-under">リセット」ボタンにて停止中のプロシージャを<br>完全に中断</span></strong>することができます。</p>



<figure class="wp-block-image size-full is-resized"><img decoding="async" src="https://dokugakuexcel.com/wp-content/uploads/2022/12/image-95.png" alt="" class="wp-image-3978" width="631" height="154" srcset="https://dokugakuexcel.com/wp-content/uploads/2022/12/image-95.png 631w, https://dokugakuexcel.com/wp-content/uploads/2022/12/image-95-300x73.png 300w" sizes="(max-width: 631px) 100vw, 631px" /></figure>



<p></p>



<h3 class="wp-block-heading"><span id="toc6">・コード実行を再開したい場合</span></h3>



<p>最後に<strong><span class="marker-under">コードの実行を再開したい場合</span></strong>を見ていきましょう。</p>



<p>まずは、アラーム時の復帰から見ていきます。</p>



<p></p>



<p>操作は簡単でメニューにある<strong><span class="marker-under-red">「継続」</span></strong>を押せば、動作を再開してくれます。</p>



<figure class="wp-block-image size-full is-resized"><img decoding="async" src="https://dokugakuexcel.com/wp-content/uploads/2022/12/image-96.png" alt="" class="wp-image-3979" width="508" height="241" srcset="https://dokugakuexcel.com/wp-content/uploads/2022/12/image-96.png 615w, https://dokugakuexcel.com/wp-content/uploads/2022/12/image-96-300x142.png 300w" sizes="(max-width: 508px) 100vw, 508px" /></figure>



<p></p>



<p>次に<strong><span class="marker-under">「デバッグ」で停止させた後に再開の方法</span></strong>を見ていきましょう。</p>



<p>こちらは<strong><span class="marker-under-red">「F5」</span></strong>キーまたは<strong><span class="marker-under-red">「ユーザーフォームの実行」ボタン</span></strong>でコード実行の再開を行うことができます。</p>



<figure class="wp-block-image size-full is-resized"><img decoding="async" src="https://dokugakuexcel.com/wp-content/uploads/2022/12/image-97.png" alt="" class="wp-image-3980" width="433" height="145" srcset="https://dokugakuexcel.com/wp-content/uploads/2022/12/image-97.png 609w, https://dokugakuexcel.com/wp-content/uploads/2022/12/image-97-300x100.png 300w" sizes="(max-width: 433px) 100vw, 433px" /></figure>



<p>もちろん再開後も再度、「Esc」キーで停止させることも可能です。</p>



<h2 class="wp-block-heading"><span id="toc7">■Escでも強制終了できない場合</span></h2>



<p>Escキーでも止まらない場合は<strong><span class="marker-under">タスクマネージャーで停止させる</span></strong>ことも可能です。</p>



<p>しかし、先ほどまでの停止方法とは異なり、Excel自体の強制終了になるので</p>



<p><strong><span class="marker-under-red">上書き保存をしていないと、前回の保存以降の処理が消えてしまうので注意が必要</span></strong>です。</p>



<p></p>



<p>では方法を見ていきましょう。</p>



<p></p>



<p>まずキーボードの<strong><span class="marker-under-blue">「Ctrl」+「Alt」+「Delete」</span></strong>を押します。</p>



<p>するとメニューが表示されるので「<strong><span class="marker-under-blue">タスクマネージャー</span></strong>」を選択します。</p>



<p></p>



<p>「<strong><span class="marker-under-blue">プロセス」タブ</span></strong>に<strong><span class="marker-under-blue">「Microsoft　Excel」</span></strong>の項目があると思うので<strong><span class="marker-under-blue">選択</span></strong>します。</p>



<figure class="wp-block-image size-full is-resized"><img decoding="async" src="https://dokugakuexcel.com/wp-content/uploads/2022/12/image-98.png" alt="" class="wp-image-3984" width="552" height="286" srcset="https://dokugakuexcel.com/wp-content/uploads/2022/12/image-98.png 829w, https://dokugakuexcel.com/wp-content/uploads/2022/12/image-98-300x156.png 300w, https://dokugakuexcel.com/wp-content/uploads/2022/12/image-98-768x398.png 768w" sizes="(max-width: 552px) 100vw, 552px" /></figure>



<p>最後に<strong><span class="marker-under-blue">「タスクの終了」</span></strong>があるのでこちらを選択します。</p>



<p>するとExcelが強制終了します。</p>



<p></p>



<p>強制終了後のファイルの復元方法については下記記事をご参照ください☟</p>



<p><a href="https://dokugakuexcel.com/%e4%bf%9d%e5%ad%98%e3%81%a7%e3%81%8d%e3%81%9a%e3%81%ab%e7%b5%82%e4%ba%86%e3%83%bb%e4%b8%8a%e6%9b%b8%e3%81%8d%e3%81%97%e3%81%9f%e3%82%a8%e3%82%af%e3%82%bb%e3%83%ab%e3%83%95%e3%82%a1%e3%82%a4%e3%83%ab/">保存できずに終了・上書きしたエクセルファイルを復元する方法一覧！消えたファイルを元に戻す ► 独学エクセル塾 (dokugakuexcel.com)</a></p>



<p></p>



<p>いずれも<strong><span class="marker-under-red">コードの実行前には上書きを推奨</span></strong>します。</p>



<h2 class="wp-block-heading"><span id="toc8">&#x2b1b;︎まとめ</span></h2>



<p>いかがだったでしょうか。<br>処理に時間がかかるプロシージャを途中で止めたくなった<br>場合など様々な場面で使用できそうですね。</p>



<p>また「Escキー」のみで操作可能なので<br>とても簡単な方法になっています。</p>



<p>是非参考にしてみてください。<br>それでは次回の記事でお会いしましょう。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://dokugakuexcel.com/%e3%80%90%e3%83%9e%e3%82%af%e3%83%advba%e3%80%91%e5%ae%9f%e8%a1%8c%e4%b8%ad%e3%81%ae%e3%82%b3%e3%83%bc%e3%83%89%e3%82%92%e5%bc%b7%e5%88%b6%e5%81%9c%e6%ad%a2%e3%83%bb%e7%b5%82%e4%ba%86%e3%81%99/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">3970</post-id>	</item>
		<item>
		<title>【マクロVBA】「指定された値は境界を超えています。」の実行時エラーの原因と対策について</title>
		<link>https://dokugakuexcel.com/%e3%80%90%e3%83%9e%e3%82%af%e3%83%advba%e3%80%91%e3%80%8c%e6%8c%87%e5%ae%9a%e3%81%95%e3%82%8c%e3%81%9f%e5%80%a4%e3%81%af%e5%a2%83%e7%95%8c%e3%82%92%e8%b6%85%e3%81%88%e3%81%a6%e3%81%84%e3%81%be/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=%25e3%2580%2590%25e3%2583%259e%25e3%2582%25af%25e3%2583%25advba%25e3%2580%2591%25e3%2580%258c%25e6%258c%2587%25e5%25ae%259a%25e3%2581%2595%25e3%2582%258c%25e3%2581%259f%25e5%2580%25a4%25e3%2581%25af%25e5%25a2%2583%25e7%2595%258c%25e3%2582%2592%25e8%25b6%2585%25e3%2581%2588%25e3%2581%25a6%25e3%2581%2584%25e3%2581%25be</link>
					<comments>https://dokugakuexcel.com/%e3%80%90%e3%83%9e%e3%82%af%e3%83%advba%e3%80%91%e3%80%8c%e6%8c%87%e5%ae%9a%e3%81%95%e3%82%8c%e3%81%9f%e5%80%a4%e3%81%af%e5%a2%83%e7%95%8c%e3%82%92%e8%b6%85%e3%81%88%e3%81%a6%e3%81%84%e3%81%be/?noamp=mobile#respond</comments>
		
		<dc:creator><![CDATA[matsu]]></dc:creator>
		<pubDate>Wed, 28 Sep 2022 14:18:43 +0000</pubDate>
				<category><![CDATA[エラー対策]]></category>
		<category><![CDATA[マクロVBA応用・便利技]]></category>
		<category><![CDATA[エラー]]></category>
		<category><![CDATA[異常]]></category>
		<category><![CDATA[指定された値は境界を越えています]]></category>
		<guid isPermaLink="false">https://dokugakuexcel.com/?p=3544</guid>

					<description><![CDATA[エクセルのマクロVBAのプログラムを作ったことがある方は誰もが経験があるエラーですが、 皆さんも悩んでいるのではないでしょうか。プログラムを作った人は発生したエラーについて理解・把握できますが作ってない方はエラーが発生し [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p>エクセルのマクロVBAのプログラムを作ったことがある方は誰もが経験があるエラーですが、</p>



<p>皆さんも悩んでいるのではないでしょうか。<br>プログラムを作った人は発生したエラーについて理解・把握できますが作ってない方はエラーが発生したらすぐに原因を理解するのは難しいです。</p>



<p>その中でも今回は<strong><span class="marker-under">「実行時エラー1004」の中の「指定された値は境界を超えています」</span></strong><br>の<strong><span class="marker-under">発生する原因とエラーの対策</span></strong>について説明したいと思います。</p>



<p>同様のエラーが発生した場合には是非参考にしてみてください。<br>それでは見ていきましょう。</p>




  <div id="toc" class="toc tnt-number toc-center tnt-number border-element"><input type="checkbox" class="toc-checkbox" id="toc-checkbox-4" checked><label class="toc-title" for="toc-checkbox-4">目次</label>
    <div class="toc-content">
    <ol class="toc-list open"><li><a href="#toc1" tabindex="0">&#x2b1b;︎「指定された値は境界を超えています」のエラーの原因について</a></li><li><a href="#toc2" tabindex="0">&#x2b1b;︎原因①:色を変える際のSchemeColorの指定に不備がある</a><ol><li><a href="#toc3" tabindex="0">・原因の内容</a></li><li><a href="#toc4" tabindex="0">・対策方法</a></li></ol></li><li><a href="#toc5" tabindex="0">&#x2b1b;︎原因②:保護したシートへの写真・図貼り付け</a><ol><li><a href="#toc6" tabindex="0">・原因の内容</a></li><li><a href="#toc7" tabindex="0">・対策方法</a></li></ol></li><li><a href="#toc8" tabindex="0">&#x2b1b;︎公式の説明</a></li><li><a href="#toc9" tabindex="0">&#x2b1b;︎まとめ</a></li></ol>
    </div>
  </div>

<h2 class="wp-block-heading"><span id="toc1">&#x2b1b;︎「指定された値は境界を超えています」のエラーの原因について</span></h2>



<p>まず、「指定された値は境界を超えています」のエラーが発生する原因から見ていきましょう。</p>



<figure class="wp-block-image size-full is-resized"><img decoding="async" src="https://dokugakuexcel.com/wp-content/uploads/2022/09/image-97.png" alt="" class="wp-image-3545" width="390" height="193" srcset="https://dokugakuexcel.com/wp-content/uploads/2022/09/image-97.png 519w, https://dokugakuexcel.com/wp-content/uploads/2022/09/image-97-300x149.png 300w" sizes="(max-width: 390px) 100vw, 390px" /></figure>



<p>現在確認できる原因としては下記の通りです。</p>



<pre class="wp-block-verse"><strong><span class="fz-20px">①色を変える際のSchemeColorの指定に不備がある
②保護したシートへの写真・図の貼り付け</span></strong></pre>



<p>※確認できている原因を挙げています。<br>上記以外で発生の原因がある場合はコメントしていただけたら幸いです。</p>



<p> </p>



<p>では各原因についてみていきましょう。</p>



<h2 class="wp-block-heading"><span id="toc2">&#x2b1b;︎原因①:色を変える際のSchemeColorの指定に不備がある</span></h2>



<p></p>



<h3 class="wp-block-heading"><span id="toc3">・原因の内容</span></h3>



<p><strong><span class="marker-under">SchemeColorとはオブジェクトの配色の色を変更</span></strong>する際に使用します。<br><strong><span class="marker-under">SchemeColorは「<span class="bold-red">1から80</span>」で指定</span></strong>します。</p>



<p>では見本を見てみましょう。</p>



<pre class="wp-block-verse">Sub 図に色をつける()
ActiveSheet.Shapes("見本図").Fill.ForeColor.SchemeColor = <span class="bold-red">5</span>
End Sub</pre>



<p>見本のプログラムは「見本図」と言う名前をつけた図の<br>色をSchemeColorで変更するプログラムです。</p>



<figure class="wp-block-image size-full is-resized"><img decoding="async" src="https://dokugakuexcel.com/wp-content/uploads/2022/09/image-98.png" alt="" class="wp-image-3546" width="453" height="232" srcset="https://dokugakuexcel.com/wp-content/uploads/2022/09/image-98.png 562w, https://dokugakuexcel.com/wp-content/uploads/2022/09/image-98-300x154.png 300w" sizes="(max-width: 453px) 100vw, 453px" /></figure>



<p>今回は「SchemeColor＝5」で指示しています。<br>では実行してみます。結果はこのようになりました。</p>



<figure class="wp-block-image size-full"><img decoding="async" width="305" height="279" src="https://dokugakuexcel.com/wp-content/uploads/2022/09/image-99.png" alt="" class="wp-image-3547" srcset="https://dokugakuexcel.com/wp-content/uploads/2022/09/image-99.png 305w, https://dokugakuexcel.com/wp-content/uploads/2022/09/image-99-300x274.png 300w" sizes="(max-width: 305px) 100vw, 305px" /></figure>



<p><strong><span class="marker-under">図が黄色に変化</span></strong>してますね。</p>



<p>では<strong>「SchemeColor＝<span class="bold-red">81</span>」</strong>で再度実行したいと思います。<br>プログラムはこちらです。</p>



<pre class="wp-block-verse">Sub 図に色をつける()
ActiveSheet.Shapes("見本図").Fill.ForeColor.SchemeColor = <span class="bold-red">81</span>
End Sub</pre>



<p>では実行してみましょう。</p>



<figure class="wp-block-image size-full is-resized"><img decoding="async" src="https://dokugakuexcel.com/wp-content/uploads/2022/09/image-100.png" alt="" class="wp-image-3548" width="370" height="183" srcset="https://dokugakuexcel.com/wp-content/uploads/2022/09/image-100.png 519w, https://dokugakuexcel.com/wp-content/uploads/2022/09/image-100-300x149.png 300w" sizes="(max-width: 370px) 100vw, 370px" /></figure>



<p><strong><span class="marker-under">「指定された値は境界を超えています」というエラーが発生</span></strong>しました。</p>



<p>「SchemeColor」の前提条件として「1から80」となってます。<br>しかし今回は「81」と、<strong><span class="marker-under-red">条件以外の数値</span></strong>であることから<br>「指定された値は境界を超えています」というエラーが発生した仕組みになっています。</p>



<h3 class="wp-block-heading"><span id="toc4">・対策方法</span></h3>



<p>こちらの対策は単純です。<br><span class="marker-under-blue">「SchemeColor」を使用する場合は「1から80」までの数値</span>にしてください。</p>



<h2 class="wp-block-heading"><span id="toc5">&#x2b1b;︎原因②:保護したシートへの写真・図貼り付け</span></h2>



<h3 class="wp-block-heading"><span id="toc6">・原因の内容</span></h3>



<p>つぎに<strong><span class="marker-under">保護したシートへの写真・図の貼り付け時に発生するパターン</span></strong>を見ていきましょう。</p>



<p>見本のプログラムを用意しました。<br>プログラムは下記の通りです。</p>



<pre class="wp-block-verse">Sub 写真の貼り付け()

Set OBJSHAPE = ActiveSheet.Shapes.AddPicture( _
　　　　　　　　  Filename:="<span class="bold-red">C:\検証ファイル</span>\<span class="bold-blue">見本写真.jpg</span>", _
　　　　　　　　  LinkToFile:=False, _
　　　　　　　　  SaveWithDocument:=True, _
　　　　　　　　  Left:=ActiveSheet.Range("E7").Left, _
　　　　　　　　  Top:=ActiveSheet.Range("E7").Top, _
　　　　　　　　  Width:=ActiveSheet.Range("E7").Width, _
　　　　　　　　  Height:=ActiveSheet.Range("E7").Height) '貼り付け
End Sub</pre>



<p>プログラムの内容として「<span class="bold-red">Cドライブの検証ファイル</span>」というフォルダに保存してある「<span class="bold-blue">見本写真.jpg</span>」という写真を「E7」に貼り付けるプログラムになっています。</p>



<figure class="wp-block-image size-full is-resized"><img decoding="async" src="https://dokugakuexcel.com/wp-content/uploads/2022/09/image-101.png" alt="" class="wp-image-3549" width="308" height="200" srcset="https://dokugakuexcel.com/wp-content/uploads/2022/09/image-101.png 431w, https://dokugakuexcel.com/wp-content/uploads/2022/09/image-101-300x195.png 300w" sizes="(max-width: 308px) 100vw, 308px" /></figure>



<p>では通常通り実行してみます。<br>結果はこのようになりました。</p>



<figure class="wp-block-image size-full is-resized"><img decoding="async" src="https://dokugakuexcel.com/wp-content/uploads/2022/09/image-102.png" alt="" class="wp-image-3550" width="345" height="228" srcset="https://dokugakuexcel.com/wp-content/uploads/2022/09/image-102.png 463w, https://dokugakuexcel.com/wp-content/uploads/2022/09/image-102-300x198.png 300w" sizes="(max-width: 345px) 100vw, 345px" /></figure>



<p>「E7」に写真を貼り付けれていますね。</p>



<p>では本題です。</p>



<p><strong><span class="marker-under">貼り付け先のアクティブシートに保護をかけた状態でプログラムを実行</span></strong>してみます。<br>結果はこのようになりました。</p>



<figure class="wp-block-image size-full is-resized"><img decoding="async" src="https://dokugakuexcel.com/wp-content/uploads/2022/09/image-103.png" alt="" class="wp-image-3551" width="413" height="200" srcset="https://dokugakuexcel.com/wp-content/uploads/2022/09/image-103.png 539w, https://dokugakuexcel.com/wp-content/uploads/2022/09/image-103-300x145.png 300w" sizes="(max-width: 413px) 100vw, 413px" /></figure>



<p>「指定された値は境界を超えています」というエラーが発生しました。</p>



<h3 class="wp-block-heading"><span id="toc7">・対策方法</span></h3>



<p>こちらは貼り付け先のシートに保護が掛かっている可能性があるので<br><strong><span class="marker-under-blue">保護を解除して、実行する必要</span></strong>があります。</p>



<p>通常通り<strong><span class="marker-under-red">「校閲」</span></strong>タブから<strong><span class="marker-under-red">「シートの保護」</span></strong>状態であるか確認する方法もありますが、<br>シートの保護をプログラムで実行している場合は<strong><span class="marker-under-blue">ActiveSheet.Unprotect</span></strong>で<br>マクロから保護の解除をする方法もあります。</p>



<figure class="wp-block-image size-large"><img decoding="async" width="1024" height="174" src="https://dokugakuexcel.com/wp-content/uploads/2022/09/image-104-1024x174.png" alt="" class="wp-image-3552" srcset="https://dokugakuexcel.com/wp-content/uploads/2022/09/image-104-1024x174.png 1024w, https://dokugakuexcel.com/wp-content/uploads/2022/09/image-104-300x51.png 300w, https://dokugakuexcel.com/wp-content/uploads/2022/09/image-104-768x130.png 768w, https://dokugakuexcel.com/wp-content/uploads/2022/09/image-104.png 1248w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<p>シートの保護・解除をマクロで行う方法についてはこちらから☟</p>



<p><a href="https://dokugakuexcel.com/%e3%80%90%e3%83%9e%e3%82%af%e3%83%advba%e3%8a%b2%e3%80%91%e3%82%b7%e3%83%bc%e3%83%88%e3%81%ae%e4%bf%9d%e8%ad%b7%e3%83%bb%e8%a7%a3%e9%99%a4%e5%85%a8%e9%9b%86%ef%bc%81%e3%82%a2%e3%82%af%e3%83%86/">【マクロVBA㊲】シートの保護・解除全集！アクティブ・対象・全シートやパスワードを含めた保護・解除 ► 独学エクセル塾 (dokugakuexcel.com)</a></p>



<p>場面に合わせて使い分けてください。</p>



<h2 class="wp-block-heading"><span id="toc8">&#x2b1b;︎公式の説明</span></h2>



<p>わかりやすいように説明したため公式と使用する語句が異なりますが<br>マイクロソフト公式の説明については下記のリンクを参照してください。☟</p>



<p><a href="https://answers.microsoft.com/ja-jp/msoffice/forum/all/%E3%83%9E%E3%82%AF%E3%83%AD%E3%81%A7%E5%9B%B3/4bdc782c-8d15-4cf1-b0e4-4fb09ce70797">マクロで図形の塗りつぶしを行うと 「指定された値は境界を越えています」 のエラーになります &#8211; Microsoft コミュニティ</a></p>



<h2 class="wp-block-heading"><span id="toc9">&#x2b1b;︎まとめ</span></h2>



<p>いかがだったでしょうか。<br>「指定された値は境界を超えています」というエラー名だけでは<br>なにが原因か理解しにくいですが<br>本記事を参考にして解決いただけたら嬉しいです。</p>



<p>　</p>



<p>是非参考にしてみてください。<br>それでは次回の記事でお会いしましょう。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://dokugakuexcel.com/%e3%80%90%e3%83%9e%e3%82%af%e3%83%advba%e3%80%91%e3%80%8c%e6%8c%87%e5%ae%9a%e3%81%95%e3%82%8c%e3%81%9f%e5%80%a4%e3%81%af%e5%a2%83%e7%95%8c%e3%82%92%e8%b6%85%e3%81%88%e3%81%a6%e3%81%84%e3%81%be/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">3544</post-id>	</item>
		<item>
		<title>【マクロVBAエラー】ブックをCloseで閉じれない「インデックスが有効範囲にありません」原因対処法</title>
		<link>https://dokugakuexcel.com/%e3%80%90%e3%83%9e%e3%82%af%e3%83%advba%e3%82%a8%e3%83%a9%e3%83%bc%e3%80%91%e3%83%96%e3%83%83%e3%82%af%e3%82%92close%e3%81%a7%e9%96%89%e3%81%98%e3%82%8c%e3%81%aa%e3%81%84%e3%80%8c%e3%82%a4%e3%83%b3/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=%25e3%2580%2590%25e3%2583%259e%25e3%2582%25af%25e3%2583%25advba%25e3%2582%25a8%25e3%2583%25a9%25e3%2583%25bc%25e3%2580%2591%25e3%2583%2596%25e3%2583%2583%25e3%2582%25af%25e3%2582%2592close%25e3%2581%25a7%25e9%2596%2589%25e3%2581%2598%25e3%2582%258c%25e3%2581%25aa%25e3%2581%2584%25e3%2580%258c%25e3%2582%25a4%25e3%2583%25b3</link>
					<comments>https://dokugakuexcel.com/%e3%80%90%e3%83%9e%e3%82%af%e3%83%advba%e3%82%a8%e3%83%a9%e3%83%bc%e3%80%91%e3%83%96%e3%83%83%e3%82%af%e3%82%92close%e3%81%a7%e9%96%89%e3%81%98%e3%82%8c%e3%81%aa%e3%81%84%e3%80%8c%e3%82%a4%e3%83%b3/?noamp=mobile#respond</comments>
		
		<dc:creator><![CDATA[matsu]]></dc:creator>
		<pubDate>Sat, 03 Sep 2022 16:07:29 +0000</pubDate>
				<category><![CDATA[エラー対策]]></category>
		<category><![CDATA[マクロVBA応用・便利技]]></category>
		<category><![CDATA[マクロ]]></category>
		<category><![CDATA[close]]></category>
		<category><![CDATA[「実行時エラー&#039;9&#039;:インデックスが有効範囲にありません」]]></category>
		<category><![CDATA[エラー]]></category>
		<category><![CDATA[VBA]]></category>
		<guid isPermaLink="false">https://dokugakuexcel.com/?p=3380</guid>

					<description><![CDATA[マクロVBAの機能の１つに「Close」という機能が存在します。使用方法としてはブックを閉じる際に使用します。 別のブックから数値を参照したりなと、ブックを経由する場合はよく使用する機能です。しかし、使用方法を誤るとエラ [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p>マクロVBAの機能の１つに<span class="marker-under"><strong>「Close」</strong></span>という機能が存在します。<br>使用方法としては<strong><span class="marker-under">ブックを閉じる</span></strong>際に使用します。</p>



<p>別のブックから数値を参照したりなと、ブックを経由する場合はよく使用する機能です。<br>しかし、使用方法を誤るとエラーが発生し、閉じることができません。</p>



<p>そこで今回は<strong><span class="marker-under-red">ブックを閉じる際に発生するエラーに対しての原因と対処法</span></strong>を紹介します。<br>それでは見ていきましょう。</p>




  <div id="toc" class="toc tnt-number toc-center tnt-number border-element"><input type="checkbox" class="toc-checkbox" id="toc-checkbox-6" checked><label class="toc-title" for="toc-checkbox-6">目次</label>
    <div class="toc-content">
    <ol class="toc-list open"><li><a href="#toc1" tabindex="0">&#x2b1b;︎通常のブックの閉じ方</a></li><li><a href="#toc2" tabindex="0">&#x2b1b;︎closeのエラーの原因と対処法</a><ol><li><a href="#toc3" tabindex="0">・「実行時エラー&#8217;9&#8242;:インデックスが有効範囲にありません」について</a></li><li><a href="#toc4" tabindex="0">・プログラムが途中で停止、実行中のブックが閉じる</a></li></ol></li><li><a href="#toc5" tabindex="0">&#x2b1b;︎補足　ブックを閉じる際の確認画面を表示させない方法</a></li><li><a href="#toc6" tabindex="0">&#x2b1b;︎まとめ</a></li></ol>
    </div>
  </div>

<h2 class="wp-block-heading"><span id="toc1">&#x2b1b;︎通常のブックの閉じ方</span></h2>



<p>まず、エラー対応の前に<strong><span class="marker-under">通常のブックを閉じる方法</span></strong>を説明します。<br>構文は以下の通りです。</p>



<pre class="wp-block-verse"><strong>Workbooks("<span class="bold-red">ここに閉じたいブック名を記入</span>").Close</strong></pre>



<p>とてもシンプルですね。</p>



<p>では見本で<span class="bold-red">「見本.xlsx」</span>というブックを閉じるプログラムを見ていきましょう。<br>前提として<strong><span class="marker-under-blue">対象のブックが開いてあることが条件</span></strong>となります。</p>



<p>プログラムはこのようになります。</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p>Sub ブックを閉じる()</p><p>Workbooks(&#8220;<span class="bold-red">見本.xlsx</span>&#8220;).Close</p><p>End Sub</p></blockquote>



<p>こちらのプログラムで「<span class="bold-red">見本.xlsx</span>」のブックを閉じることができました。<br>結論としてブックを閉じる際に使用する<strong><span class="marker-under-red">「Close」はブック名で指示</span></strong>します。<br>これが原因でエラーが生じる場合もあります。<br>では、ブックを「close」で閉じる際のエラーの原因と対処法を見ていきましょう。</p>



<h2 class="wp-block-heading"><span id="toc2">&#x2b1b;︎closeのエラーの原因と対処法</span></h2>



<h3 class="wp-block-heading"><span id="toc3">・「実行時エラー&#8217;9&#8242;:インデックスが有効範囲にありません」について</span></h3>



<p>こちらはブック名で指定していない可能性があります。<br>よくある原因としては<strong><span class="marker-under-red">フルパスでファイルを開いて、そのフルパスを活用して「close」<br>することでエラーが生じてしまいます</span></strong>。</p>



<p>ではプログラムの例を見ていきましょう。<br>今回は「見本」ブックを開いて、処理を行い、閉じるプログラムです。</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p>Sub ブックを閉じる()</p><p>Dim FilePath As String</p><p>FilePath =<span class="bold-red"> &#8220;C:\excel\見本.xlsx&#8221;</span></p><p>Workbooks.Open FilePath</p><p></p><p>&#8216;ここに処理プログラム</p><p></p><p>Workbooks(<span class="bold-red">FilePath</span>).Close</p><p>End Sub</p></blockquote>



<p>一見問題無さそうですが実行したらこのようになります。</p>



<figure class="wp-block-image size-full is-resized"><img decoding="async" src="https://dokugakuexcel.com/wp-content/uploads/2022/09/image-3.png" alt="" class="wp-image-3384" width="373" height="174" srcset="https://dokugakuexcel.com/wp-content/uploads/2022/09/image-3.png 486w, https://dokugakuexcel.com/wp-content/uploads/2022/09/image-3-300x140.png 300w" sizes="(max-width: 373px) 100vw, 373px" /></figure>



<p><strong><span class="marker-under-red">「実行時エラー&#8217;9&#8242;:インデックスが有効範囲にありません」というエラーが発生</span></strong>しました。</p>



<p>原因として<strong><span class="marker-under-red">ブックを開く際の参照と閉じる際の参照の違いに問題</span></strong>があります。<br>ブックを開く際の<strong><span class="marker-under">「Open」はフルパス</span></strong>でなければなりませんが<br>ブックを閉じる際の<strong><span class="marker-under">「close」はファイル名</span></strong>でなければなりません。</p>



<p>しかし、パスが繰り返し動作やセルに入力してあるパスを参照する際には<br>プログラムにファイル名は入力できませんよね。</p>



<p>ファイル名が限定される方は次の項目に飛んでください。</p>



<p></p>



<p>使用するのは<strong><span class="marker-under-blue">「Dir」</span></strong>になります。<br>機能としては<strong><span class="marker-under-blue">フルパスからファイル名を取得する</span></strong>ことができます。</p>



<p>では構文を見ていきましょう。</p>



<pre class="wp-block-verse"><strong>ファイル名＝Dir(<span class="bold-blue">フルパス</span>)</strong></pre>



<p>ではこちらを活用したプログラムはこのようになります。</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p>Sub ブックを閉じる()</p><p>Dim FilePath As String, CloseFileName</p><p>FilePath = &#8220;C:\excel\見本.xlsx&#8221;</p><p>Workbooks.Open FilePath</p><p></p><p>&#8216;ここに処理プログラム</p><p></p><p>CloseFileName = Dir(<span class="bold-blue">FilePath</span>)<br>Workbooks(CloseFileName).Close</p><p>End Sub</p></blockquote>



<p>それでは実行してみましょう。<br>「CloseFileName = Dir(FilePath)」の部分の変数「CloseFileName」にファイル名が<br>格納できているか見てみましょう。</p>



<figure class="wp-block-image size-full"><img decoding="async" width="525" height="93" src="https://dokugakuexcel.com/wp-content/uploads/2022/09/image-2.png" alt="" class="wp-image-3383" srcset="https://dokugakuexcel.com/wp-content/uploads/2022/09/image-2.png 525w, https://dokugakuexcel.com/wp-content/uploads/2022/09/image-2-300x53.png 300w" sizes="(max-width: 525px) 100vw, 525px" /></figure>



<p><span class="marker-under">「C:\excel\見本.xlsx」のフルパスからファイル名のみを抽出できています。</span></p>



<p>これでファイル名で「close」するのでエラーが出なくなります。</p>



<p>また、「close」で設定したファイル名が合っていなくても同様のエラーとなります。</p>



<p>例えば閉じたいブック名に半角のスペースが入っただけでも<br>エラーになってしまいます。</p>



<p>正確なファイル名で「close」の指示ができるようにプログラムを作成してください。</p>



<h3 class="wp-block-heading"><span id="toc4">・プログラムが途中で停止、実行中のブックが閉じる</span></h3>



<p>こちらは実行中のプロシージャ（プログラム)のブックを閉じると<br>起こるエラーとなっています。</p>



<p>例えば、「ファイル A」というファイルからVBAを使用して<br>「ファイルB」というファイルを閉じようとします。</p>



<p>しかし誤って「ファイルA」を閉じるように設定すると<br><strong><span class="marker-under-red">プログラムを実行中のファイルAが閉じてしまうため<br>プログラムが途中で停止してしまう問題が発生します</span></strong>。</p>



<p>なので閉じるファイル名が正確か確認してみてください。</p>



<h2 class="wp-block-heading"><span id="toc5">&#x2b1b;︎補足　ブックを閉じる際の確認画面を表示させない方法</span></h2>



<p>ここからは補足になります。<br>編集したブックを「close」で閉じる指令を実行すると<strong><span class="marker-under">「’対象ファイル名’の変更内容を保存じますか？」</span></strong>というメッセージがでます。</p>



<figure class="wp-block-image size-full"><img decoding="async" width="673" height="211" src="https://dokugakuexcel.com/wp-content/uploads/2022/09/image-1.png" alt="" class="wp-image-3382" srcset="https://dokugakuexcel.com/wp-content/uploads/2022/09/image-1.png 673w, https://dokugakuexcel.com/wp-content/uploads/2022/09/image-1-300x94.png 300w" sizes="(max-width: 673px) 100vw, 673px" /></figure>



<p><br>「Workbooks(ファイル名).Save」で１度保存した場合は問題ありませんが、保存しない場合は処理しないといけません。</p>



<p>しかし何度もブックを閉じるプログラムだと毎回確認ボタンを押さないといけないので面倒くさいですよね。<br>そこでブックを閉じる前に<strong><span class="marker-under-blue">「Application.DisplayAlerts = False」</span></strong>を実行することで、確認動作をスルーすることができます。<br>ブックを閉じた後は<strong><span class="marker-under-red">「Application.DisplayAlerts = True」</span></strong>で再度、確認メッセージが出るように設定することをオススメします。</p>



<p>では先ほどのプログラムに織り込んでみましょう。</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p>Sub ブックを閉じる()</p><p>Dim FilePath As String, CloseFileName</p><p>FilePath = &#8220;C:\excel\見本.xlsx&#8221;</p><p>Workbooks.Open FilePath</p><p>&#8216;ここに処理プログラム</p><p>CloseFileName = Dir(FilePath)</p><p><span class="marker-under-blue"><strong>Application.DisplayAlerts = False</strong></span><br>Workbooks(CloseFileName).Close<br><strong><span class="marker-under-red">Application.DisplayAlerts = True</span></strong></p><p>End Sub</p></blockquote>



<p>これで「’対象ファイル名’の変更内容を保存じますか？」の確認メッセージをスルーすることが出来ます。</p>



<h2 class="wp-block-heading"><span id="toc6">&#x2b1b;︎まとめ</span></h2>



<p>いかがだったでしょうか。<br>ブックを経由してデータを取得したり入力したりする際に用いられる<br>「close」ですが、使用方法を間違えるとエラーが生じてしまいます。</p>



<p>ぜひ今回の記事を参考にして<br>エラーが出ないようなプログラムを作成してみてください。</p>



<p>それでは次回の記事でお会いしましょう。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://dokugakuexcel.com/%e3%80%90%e3%83%9e%e3%82%af%e3%83%advba%e3%82%a8%e3%83%a9%e3%83%bc%e3%80%91%e3%83%96%e3%83%83%e3%82%af%e3%82%92close%e3%81%a7%e9%96%89%e3%81%98%e3%82%8c%e3%81%aa%e3%81%84%e3%80%8c%e3%82%a4%e3%83%b3/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">3380</post-id>	</item>
		<item>
		<title>【マクロVBAエラー対応】ユーザーフォームのShowでFunctionまたは変数が必要ですの対応法</title>
		<link>https://dokugakuexcel.com/%e3%80%90%e3%83%9e%e3%82%af%e3%83%advba%e3%82%a8%e3%83%a9%e3%83%bc%e5%af%be%e5%bf%9c%e3%80%91%e3%83%a6%e3%83%bc%e3%82%b6%e3%83%bc%e3%83%95%e3%82%a9%e3%83%bc%e3%83%a0%e3%81%aeshow%e3%81%a7function/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=%25e3%2580%2590%25e3%2583%259e%25e3%2582%25af%25e3%2583%25advba%25e3%2582%25a8%25e3%2583%25a9%25e3%2583%25bc%25e5%25af%25be%25e5%25bf%259c%25e3%2580%2591%25e3%2583%25a6%25e3%2583%25bc%25e3%2582%25b6%25e3%2583%25bc%25e3%2583%2595%25e3%2582%25a9%25e3%2583%25bc%25e3%2583%25a0%25e3%2581%25aeshow%25e3%2581%25a7function</link>
					<comments>https://dokugakuexcel.com/%e3%80%90%e3%83%9e%e3%82%af%e3%83%advba%e3%82%a8%e3%83%a9%e3%83%bc%e5%af%be%e5%bf%9c%e3%80%91%e3%83%a6%e3%83%bc%e3%82%b6%e3%83%bc%e3%83%95%e3%82%a9%e3%83%bc%e3%83%a0%e3%81%aeshow%e3%81%a7function/?noamp=mobile#comments</comments>
		
		<dc:creator><![CDATA[matsu]]></dc:creator>
		<pubDate>Sun, 27 Feb 2022 13:10:17 +0000</pubDate>
				<category><![CDATA[エラー対策]]></category>
		<category><![CDATA[マクロVBA応用・便利技]]></category>
		<category><![CDATA[エラー]]></category>
		<category><![CDATA[VBA]]></category>
		<category><![CDATA[マクロ]]></category>
		<category><![CDATA[ユーザーフォーム]]></category>
		<category><![CDATA[Show]]></category>
		<guid isPermaLink="false">https://dokugakuexcel.com/?p=2295</guid>

					<description><![CDATA[本記事はVBAのエラー対応について説明します。今回はユーザーフォームを表示（Show）する際に発生する「Functionまたは変数が必要です」というエラーの原因と対策について紹介したいと思います。 このエラーが発生すると [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p>本記事はVBAのエラー対応について説明します。<br>今回はユーザーフォームを表示（Show）する際に発生する<br><strong><span class="marker-under">「Functionまたは変数が必要です」というエラーの原因と対策</span></strong>について紹介したいと思います。</p>



<p>このエラーが発生するとプログラム自体を走らせることができなくなるので対策が必要です。</p>



<p>原因も詳しく説明してますので是非参考にしてみてください。</p>



<p>では実際に見ていきましょう。</p>




  <div id="toc" class="toc tnt-number toc-center tnt-number border-element"><input type="checkbox" class="toc-checkbox" id="toc-checkbox-8" checked><label class="toc-title" for="toc-checkbox-8">目次</label>
    <div class="toc-content">
    <ol class="toc-list open"><li><a href="#toc1" tabindex="0">■通常のユーザーフォームの表示</a></li><li><a href="#toc2" tabindex="0">■「Functionまたは変数が必要です」のエラーの原因と対策法</a></li><li><a href="#toc3" tabindex="0">■まとめ</a></li></ol>
    </div>
  </div>

<h2 class="wp-block-heading"><span id="toc1">■通常のユーザーフォームの表示</span></h2>



<p>まずはエラーが発生しないユーザーフォームの表示を見てみましょう。<br>見本で下図のユーザーフォームを作成してみました。</p>



<figure class="wp-block-image size-full is-resized"><img decoding="async" src="https://dokugakuexcel.com/wp-content/uploads/2022/02/image-82.png" alt="" class="wp-image-2296" width="537" height="252" srcset="https://dokugakuexcel.com/wp-content/uploads/2022/02/image-82.png 746w, https://dokugakuexcel.com/wp-content/uploads/2022/02/image-82-300x141.png 300w" sizes="(max-width: 537px) 100vw, 537px" /></figure>



<p>特に変わりないユーザーフォームになってます。</p>



<figure class="wp-block-image size-full"><img decoding="async" width="329" height="206" src="https://dokugakuexcel.com/wp-content/uploads/2022/02/image-83.png" alt="" class="wp-image-2297" srcset="https://dokugakuexcel.com/wp-content/uploads/2022/02/image-83.png 329w, https://dokugakuexcel.com/wp-content/uploads/2022/02/image-83-300x188.png 300w" sizes="(max-width: 329px) 100vw, 329px" /></figure>



<p>ここで注目してほしいのは<strong><span class="marker-under-red">「オブジェクト名」</span></strong>です。<br>ではこちらのユーザーフォームを表示するだけのマクロVBAのプログラムを見てみましょう。</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p><strong>Sub ユーザーフォームを表示()</strong></p><p><strong><span class="marker-under-red">更新中</span>.Show</strong></p><p><strong>End Sub</strong></p></blockquote>



<p>とてもシンプルですね。<br><strong><span class="marker-under-red">更新中のユーザーフォームを表示する</span></strong>ので<br><strong><span class="bold-red"><span class="marker-under-red">更新中.Show</span></span></strong><br>というプログラムです。</p>



<p>では実行してみましょう。</p>



<figure class="wp-block-image size-full is-resized"><img decoding="async" src="https://dokugakuexcel.com/wp-content/uploads/2022/02/image-84.png" alt="" class="wp-image-2298" width="533" height="284" srcset="https://dokugakuexcel.com/wp-content/uploads/2022/02/image-84.png 929w, https://dokugakuexcel.com/wp-content/uploads/2022/02/image-84-300x160.png 300w, https://dokugakuexcel.com/wp-content/uploads/2022/02/image-84-768x409.png 768w" sizes="(max-width: 533px) 100vw, 533px" /></figure>



<p>問題なくユーザーフォームを表示することができました。</p>



<h2 class="wp-block-heading"><span id="toc2">■「Functionまたは変数が必要です」のエラーの原因と対策法</span></h2>



<p>では本題に移りたいと思います。<br>下記のマクロVBAのプログラムをご覧ください。</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p><strong>Sub 更新中()</strong></p><p><strong>更新中.Show</strong></p><p><strong>End Sub</strong></p></blockquote>



<p>一見問題なさそうなプログラムですよね。<br>では実行してみましょう。すると下記のようなエラーが表示されました。</p>



<figure class="wp-block-image size-full is-resized"><img decoding="async" src="https://dokugakuexcel.com/wp-content/uploads/2022/02/image-85.png" alt="" class="wp-image-2299" width="426" height="236" srcset="https://dokugakuexcel.com/wp-content/uploads/2022/02/image-85.png 524w, https://dokugakuexcel.com/wp-content/uploads/2022/02/image-85-300x166.png 300w, https://dokugakuexcel.com/wp-content/uploads/2022/02/image-85-160x90.png 160w" sizes="(max-width: 426px) 100vw, 426px" /></figure>



<p>ここでお気づきの方もおられると思いますが<br><strong><span class="marker-under">実行プログラム自体は先ほどの見本では実行できたことから何も問題ありません</span></strong>。</p>



<p>では正常に機能するプログラムとエラーが発生するプログラムを見比べてみましょう。</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p><strong><span class="bold-blue"><span class="fz-24px"><span class="fz-22px">正常のマクロVBAのプログラム</span></span></span></strong></p><p>Sub <strong><span class="marker-under-blue">ユーザーフォームを表示</span></strong>()</p><p><strong><span class="marker-under-red">更新中</span></strong>.Show</p><p>End Sub</p></blockquote>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p><strong><span class="bold-red"><span class="fz-22px">異常のマクロVBAのプログラム</span></span></strong></p><p>Sub<span class="marker-under-red"> <strong>更新中</strong></span>()</p><p><strong><span class="marker-under-red">更新中</span></strong>.Show</p><p>End Sub</p></blockquote>



<p>お分かりいただけましたでしょうか。</p>



<p>正常であるプログラムはプログラム名が「ユーザーフォームを表示」で「更新中」のユーザーフォームを表示させてますが、エラーが発生するプログラムは「更新中」というプログラム名で「更新中」のユーザーフォームを表示させてます。</p>



<p>つまり、<strong><span class="marker-under-red">エラーの原因は「オブジェクト名とプログラム名が一緒」である</span></strong>ことがわかります。</p>



<figure class="wp-block-image size-full"><img decoding="async" width="716" height="206" src="https://dokugakuexcel.com/wp-content/uploads/2022/02/image-86.png" alt="" class="wp-image-2300" srcset="https://dokugakuexcel.com/wp-content/uploads/2022/02/image-86.png 716w, https://dokugakuexcel.com/wp-content/uploads/2022/02/image-86-300x86.png 300w" sizes="(max-width: 716px) 100vw, 716px" /></figure>



<p>なので<strong><span class="marker-under-blue">対策としてはオブジェクト名とプログラム名は同じでないこと</span></strong>が正常のプログラムに欠かせません。</p>



<p>どうしても同じ名前で付けたい場合は下記プログラムのように1文字でも付け加えるだけで正常に動いてくれます。</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p>Sub 更新中1()</p><p>更新中.Show</p><p>End Sub</p></blockquote>



<h2 class="wp-block-heading"><span id="toc3">■まとめ</span></h2>



<p>いかがだったでしょうか。<br>私もユーザーフォームを使用し始めたときに同じエラーが発生して<br>対策方法がわからず試行錯誤して判明しました。</p>



<p>現在はプログラム名の後に作成した年月日を番号で入力しているので<br>そのようなエラーは起きない＆作成した日時がわかるのでおすすめです。</p>



<p>是非参考にしてみてください。</p>



<p>それでは次回の記事でお会いしましょう。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://dokugakuexcel.com/%e3%80%90%e3%83%9e%e3%82%af%e3%83%advba%e3%82%a8%e3%83%a9%e3%83%bc%e5%af%be%e5%bf%9c%e3%80%91%e3%83%a6%e3%83%bc%e3%82%b6%e3%83%bc%e3%83%95%e3%82%a9%e3%83%bc%e3%83%a0%e3%81%aeshow%e3%81%a7function/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">2295</post-id>	</item>
		<item>
		<title>【マクロVBAエラー対応】「.xlsm」の拡張子でブックの名前を付けて保存ができない対応方法</title>
		<link>https://dokugakuexcel.com/%e3%80%90%e3%83%9e%e3%82%af%e3%83%advba%e3%82%a8%e3%83%a9%e3%83%bc%e5%af%be%e5%bf%9c%e3%80%91%e3%80%8c-xlsm%e3%80%8d%e3%81%ae%e6%8b%a1%e5%bc%b5%e5%ad%90%e3%81%a7%e3%83%96%e3%83%83%e3%82%af%e3%81%ae/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=%25e3%2580%2590%25e3%2583%259e%25e3%2582%25af%25e3%2583%25advba%25e3%2582%25a8%25e3%2583%25a9%25e3%2583%25bc%25e5%25af%25be%25e5%25bf%259c%25e3%2580%2591%25e3%2580%258c-xlsm%25e3%2580%258d%25e3%2581%25ae%25e6%258b%25a1%25e5%25bc%25b5%25e5%25ad%2590%25e3%2581%25a7%25e3%2583%2596%25e3%2583%2583%25e3%2582%25af%25e3%2581%25ae</link>
					<comments>https://dokugakuexcel.com/%e3%80%90%e3%83%9e%e3%82%af%e3%83%advba%e3%82%a8%e3%83%a9%e3%83%bc%e5%af%be%e5%bf%9c%e3%80%91%e3%80%8c-xlsm%e3%80%8d%e3%81%ae%e6%8b%a1%e5%bc%b5%e5%ad%90%e3%81%a7%e3%83%96%e3%83%83%e3%82%af%e3%81%ae/?noamp=mobile#respond</comments>
		
		<dc:creator><![CDATA[matsu]]></dc:creator>
		<pubDate>Thu, 24 Feb 2022 12:24:50 +0000</pubDate>
				<category><![CDATA[エラー対策]]></category>
		<category><![CDATA[マクロVBA応用・便利技]]></category>
		<category><![CDATA[拡張子]]></category>
		<category><![CDATA[VBA]]></category>
		<category><![CDATA[エラー対応]]></category>
		<category><![CDATA[xlsm]]></category>
		<category><![CDATA[ブック保存]]></category>
		<guid isPermaLink="false">https://dokugakuexcel.com/?p=2267</guid>

					<description><![CDATA[今回はマクロVBAのエラー対応について説明します。VBAで名前を付けて保存する際に拡張子も入力して保存したとします。 しかし、通常のエクセルの拡張子である「xlsx」の要領で「xlsm」の拡張子で保存するとエラーが発生し [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p>今回は<strong><span class="marker-under">マクロ</span><span class="marker-under">VBAのエラー対応</span></strong>について説明します。<br>VBAで名前を付けて保存する際に拡張子も入力して保存したとします。</p>



<p>しかし、通常のエクセルの拡張子である<strong><span class="marker-under-red">「xlsx」の要領で「xlsm」の拡張子で保存すると<br>エラーが発生してしまいます</span></strong>。</p>



<p>そこで今回は<strong><span class="marker-under">「.xlsm」の拡張子でエラーが発生しない方法で保存する方法</span></strong>について紹介します。</p>



<p>それでは見ていきましょう。</p>




  <div id="toc" class="toc tnt-number toc-center tnt-number border-element"><input type="checkbox" class="toc-checkbox" id="toc-checkbox-10" checked><label class="toc-title" for="toc-checkbox-10">目次</label>
    <div class="toc-content">
    <ol class="toc-list open"><li><a href="#toc1" tabindex="0">■「.xlsm」の拡張子とは</a></li><li><a href="#toc2" tabindex="0">■通常のファイル保存方法</a></li><li><a href="#toc3" tabindex="0">■「.xlsm」で保存する方法</a></li><li><a href="#toc4" tabindex="0">&#x2b1b;︎公式の説明</a></li><li><a href="#toc5" tabindex="0">■まとめ</a></li></ol>
    </div>
  </div>

<h2 class="wp-block-heading"><span id="toc1">■「.xlsm」の拡張子とは</span></h2>



<p>まずは「.xlsm」の拡張子について紹介します。</p>



<p>拡張子とはファイル名の後の「.」の後に続く文字のことを言います。<br>VBAを使用する方はすでにご存知かと思いますがマクロの設定や<br>保存する際には通常の拡張子である「.xlsx」でなく「.xlsm」で保存しないといけません。</p>



<h2 class="wp-block-heading"><span id="toc2">■通常のファイル保存方法</span></h2>



<p>では通常のファイル保存を参考に見ていきましょう。</p>



<p>下記プログラムをご覧ください。</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p>Sub ブックの保存()</p><p>Workbooks.Add<br>ActiveWorkbook.SaveAs Filename:=&#8221;エクセルシート&#8221;</p><p>End Sub</p></blockquote>



<p>このプログラムではアクティブブックを「エクセルシート」という名前で保存します。<br>もちろん拡張子は通常ファイル形式の「.xlsx」となります。</p>



<p>ではもう1パターン見てみましょう。<br>下記プログラムをご覧ください。</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p>Sub ブックの保存()</p><p>Workbooks.Add<br>ActiveWorkbook.SaveAs Filename:=&#8221;エクセルシート.xlsx&#8221;</p><p>End Sub</p></blockquote>



<p>こちらは先ほどのプログラムに加え、拡張子を指定して「.xlsx」で<br>保存しています。<br>こちらも拡張子は通常ファイル形式の「.xlsx」となります。</p>



<h2 class="wp-block-heading"><span id="toc3">■「.xlsm」で保存する方法</span></h2>



<p>では本題の「.xlsm」の拡張子で保存してみましょう。<br>先ほどプログラムの「.xlsx」を「.xlsm」に変えてみました。</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p>Sub ブックの保存()</p><p>Workbooks.Add<br>ActiveWorkbook.SaveAs Filename:=&#8221;<strong><span class="marker-under-blue">エクセルシート.xlsm</span></strong>&#8220;</p><p>End Sub</p></blockquote>



<p>実行すると下図のようなエラーが発生してしまいます。</p>



<figure class="wp-block-image size-full is-resized"><img decoding="async" src="https://dokugakuexcel.com/wp-content/uploads/2022/02/image-70.png" alt="" class="wp-image-2268" width="615" height="298" srcset="https://dokugakuexcel.com/wp-content/uploads/2022/02/image-70.png 628w, https://dokugakuexcel.com/wp-content/uploads/2022/02/image-70-300x145.png 300w" sizes="(max-width: 615px) 100vw, 615px" /></figure>



<p>実行時エラー&#8217;1004&#8242;:が発生してしまいました。<br>皆さんもここで困っているのではないでしょうか。</p>



<p>エラーの内容をみても「<strong><span class="marker-under-red">この拡張子は、選択したファイル形式には使用できません。</span></strong>」<br>と書いてありますね。</p>



<p>では解決方法を紹介したいと思います。<br><strong><span class="marker-under">「FileFormat」で「.xlsm」でマクロ有効ブックであることを指定すれば解決します</span></strong>。</p>



<p><strong><span class="marker-under">xlsmの拡張子であれば「 FileFormat:=xlOpenXMLWorkbookMacroEnabled」</span></strong>で<br><strong><span class="marker-under">マクロ有効ブックの指定</span></strong>をします。</p>



<p>では実際にプログラムに組み込んだ結果を見てみましょう。</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p>Sub ブックの保存()</p><p>Workbooks.Add<br>ActiveWorkbook.SaveAs Filename:=&#8221;エクセルシート.xlsm&#8221;, FileFormat:=xlOpenXMLWorkbookMacroEnabled</p><p>End Sub</p></blockquote>



<p>こちらでxlsmの拡張子で保存することができました。</p>



<figure class="wp-block-image size-full is-resized"><img decoding="async" src="https://dokugakuexcel.com/wp-content/uploads/2022/02/image-71.png" alt="" class="wp-image-2269" width="559" height="269" srcset="https://dokugakuexcel.com/wp-content/uploads/2022/02/image-71.png 660w, https://dokugakuexcel.com/wp-content/uploads/2022/02/image-71-300x145.png 300w" sizes="(max-width: 559px) 100vw, 559px" /></figure>



<p>ちなみにファイルを保存する際には</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p><br><strong>①通常ファイルの「xlsx」<br>②マクロ有効ブックの「xlsm」<br>③csvファイルの「csv」<br>④メモ帳の「txt」</strong></p></blockquote>



<p><br>の種類がありますが<strong><span class="marker-under-red">エラーが出るのは②のマクロ有効ブックのみ</span></strong>です。</p>



<p>参考までに各フォーマットを載せておきます。</p>



<figure class="wp-block-table is-style-regular"><table><thead><tr><th>ファイル形式</th><th>フォーマット</th></tr></thead><tbody><tr><td>xlsx</td><td>xlOpenXMLWorkbook</td></tr><tr><td>xlsm</td><td>xlOpenXMLWorkbookMacroEnabled</td></tr><tr><td>csv</td><td>xlCSV</td></tr><tr><td>text</td><td>xlText</td></tr></tbody></table></figure>



<p>上記を踏まえて見本でもう一事例紹介したいと思います。<br>拡張子をtxtで保存してみましょう。</p>



<p>プログラムはこのようになります。</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p>Sub テキストでブックを保存()</p><p>Workbooks.Add<br>ActiveWorkbook.SaveAs Filename:=&#8221;エクセルシート&#8221;, FileFormat:=xlText</p><p>End Sub</p></blockquote>



<p>これで様々な拡張子で保存できるようになりましたね。</p>



<h2 class="wp-block-heading"><span id="toc4">&#x2b1b;︎公式の説明</span></h2>



<p>わかりやすいように説明したため公式と使用する語句が異なりますが<br>マイクロソフト公式の説明については下記のリンクを参照してください。☟</p>



<p><a href="https://learn.microsoft.com/ja-jp/office/vba/api/excel.workbook.saveas">Workbook.SaveAs メソッド (Excel) | Microsoft Learn</a></p>



<h2 class="wp-block-heading"><span id="toc5">■まとめ</span></h2>



<p>いかがだったでしょうか。<br>VBAを使用する上でxlsmの拡張子は必須ですが<br>いざ保存しようとするとエラーが出て困りますよね。</p>



<p>ぜひこの記事をもとにマクロ有効ブックで保存してみてください。</p>



<p>それでは次回の記事でお会いしましょう。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://dokugakuexcel.com/%e3%80%90%e3%83%9e%e3%82%af%e3%83%advba%e3%82%a8%e3%83%a9%e3%83%bc%e5%af%be%e5%bf%9c%e3%80%91%e3%80%8c-xlsm%e3%80%8d%e3%81%ae%e6%8b%a1%e5%bc%b5%e5%ad%90%e3%81%a7%e3%83%96%e3%83%83%e3%82%af%e3%81%ae/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">2267</post-id>	</item>
		<item>
		<title>【マクロVBAのエラー対策】エラーが発生したら指定行まで移動！終了までできるエラー処理方法について</title>
		<link>https://dokugakuexcel.com/%e3%80%90%e3%83%9e%e3%82%af%e3%83%advba%e3%81%ae%e3%82%a8%e3%83%a9%e3%83%bc%e5%af%be%e7%ad%96%e3%80%91%e3%82%a8%e3%83%a9%e3%83%bc%e3%81%ab%e8%b2%a0%e3%81%91%e3%81%aa%e3%81%84%e3%83%97%e3%83%ad/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=%25e3%2580%2590%25e3%2583%259e%25e3%2582%25af%25e3%2583%25advba%25e3%2581%25ae%25e3%2582%25a8%25e3%2583%25a9%25e3%2583%25bc%25e5%25af%25be%25e7%25ad%2596%25e3%2580%2591%25e3%2582%25a8%25e3%2583%25a9%25e3%2583%25bc%25e3%2581%25ab%25e8%25b2%25a0%25e3%2581%2591%25e3%2581%25aa%25e3%2581%2584%25e3%2583%2597%25e3%2583%25ad</link>
					<comments>https://dokugakuexcel.com/%e3%80%90%e3%83%9e%e3%82%af%e3%83%advba%e3%81%ae%e3%82%a8%e3%83%a9%e3%83%bc%e5%af%be%e7%ad%96%e3%80%91%e3%82%a8%e3%83%a9%e3%83%bc%e3%81%ab%e8%b2%a0%e3%81%91%e3%81%aa%e3%81%84%e3%83%97%e3%83%ad/?noamp=mobile#respond</comments>
		
		<dc:creator><![CDATA[matsu]]></dc:creator>
		<pubDate>Tue, 26 Oct 2021 11:33:30 +0000</pubDate>
				<category><![CDATA[エラー対策]]></category>
		<category><![CDATA[マクロVBA応用・便利技]]></category>
		<category><![CDATA[ERROR]]></category>
		<category><![CDATA[エラー]]></category>
		<category><![CDATA[異常]]></category>
		<guid isPermaLink="false">https://dokugakuexcel.com/?p=1815</guid>

					<description><![CDATA[エクセルのマクロVBAのプログラムを作ったことがある方は誰もが経験があるエラーですが、皆さんも悩んでいるのではないでしょうか。 プログラムを作った人は発生したエラーについて理解・把握できますが作ってない方はエラーが発生し [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p>エクセルのマクロVBAのプログラムを作ったことがある方は誰もが経験がある<br>エラーですが、皆さんも悩んでいるのではないでしょうか。</p>



<p>プログラムを作った人は発生したエラーについて理解・把握できますが<br>作ってない方はエラーが発生したらすぐに理解するのは難しいです。</p>



<p>さらにVBAを触ったことがない人がシステムを利用してエラーが出ると<br>何を押していいかわかりません。</p>



<p>そのためにもプログラムを作成する方はエラーに対応したプログラムを<br>作成する能力を求められます。</p>



<p>ぜひ、<strong><span class="marker-under">エラーに強いプログラムを作成</span></strong>できるようにしてみてください。</p>




  <div id="toc" class="toc tnt-number toc-center tnt-number border-element"><input type="checkbox" class="toc-checkbox" id="toc-checkbox-12" checked><label class="toc-title" for="toc-checkbox-12">目次</label>
    <div class="toc-content">
    <ol class="toc-list open"><li><a href="#toc1" tabindex="0">■マクロVBAで発生するエラーとは</a></li><li><a href="#toc2" tabindex="0">■エラーの対応方法</a><ol><li><a href="#toc3" tabindex="0">・①対象範囲内でエラーが起きたら指定位置へ飛ぶ</a></li><li><a href="#toc4" tabindex="0">・②IFを使用して異常値を検出して指定位置に飛ぶ</a></li><li><a href="#toc5" tabindex="0">・③入力規則で入力データを制限する</a></li></ol></li><li><a href="#toc6" tabindex="0">&#x2b1b;︎公式の説明</a></li><li><a href="#toc7" tabindex="0">■まとめ</a></li></ol>
    </div>
  </div>

<h2 class="wp-block-heading"><span id="toc1">■マクロVBAで発生するエラーとは</span></h2>



<p>まず発生するエラーについて見てみましょう。<br>下図をご覧ください。</p>



<figure class="wp-block-image size-full"><img decoding="async" width="360" height="182" src="https://dokugakuexcel.com/wp-content/uploads/2021/10/image-64.png" alt="" class="wp-image-1816" srcset="https://dokugakuexcel.com/wp-content/uploads/2021/10/image-64.png 360w, https://dokugakuexcel.com/wp-content/uploads/2021/10/image-64-300x152.png 300w" sizes="(max-width: 360px) 100vw, 360px" /></figure>



<p>このような異常はプログラムを作ったことがある方は見たことがあるのではないでしょうか。<br>ちなみに<strong><span class="marker-under">終了を押せばプログラムは強制終了</span></strong>します。</p>



<p><strong><span class="marker-under">デバッグを押せばエラーの原因のプログラムの位置で止まってくれます</span></strong>。<br>VBAを知らない方はデバッグを押すと、みたことのない画面が出るため困ってしまいます。</p>



<p><strong><span class="marker-under">ヘルプはマイクロソフト公式のヘルプに飛んでくれます</span></strong>。</p>



<p>今回の目的はこの画面が出ないようにプログラミングしていきます。</p>



<h2 class="wp-block-heading"><span id="toc2">■エラーの対応方法</span></h2>



<p>今回は下図のようなシステムを見本で作成しました。</p>



<figure class="wp-block-image size-full"><img decoding="async" width="777" height="262" src="https://dokugakuexcel.com/wp-content/uploads/2021/10/image-65.png" alt="" class="wp-image-1817" srcset="https://dokugakuexcel.com/wp-content/uploads/2021/10/image-65.png 777w, https://dokugakuexcel.com/wp-content/uploads/2021/10/image-65-300x101.png 300w, https://dokugakuexcel.com/wp-content/uploads/2021/10/image-65-768x259.png 768w" sizes="(max-width: 777px) 100vw, 777px" /></figure>



<p>システムとして、身長・体重を打ち込んで更新を押すと<br>8行以降に<span class="marker-under-red">データを蓄積していく</span>シートを用意しました。その際にBMIも算出します。</p>



<p>プログラムも見本で載せておきます</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p>Sub データ集計()</p><p>Dim EndRow As Long<br>Dim Height As Long &#8216;身長<br>Dim Weight As Long &#8216;体重</p><p>EndRow = Cells(7, 2).End(xlDown).Row + 1</p><p>Height = Cells(3, 2)<br>Weight = Cells(3, 3)</p><p>Cells(EndRow, 2) = Height<br>Cells(EndRow, 3) = Weight<br>Cells(EndRow, 4) = Weight / (Height / 100) ^ 2</p><p>Range(&#8220;B3:C3&#8221;).ClearContents</p><p>End Sub</p></blockquote>



<p>では、このプログラムを元に説明していきます。</p>



<p></p>



<h3 class="wp-block-heading"><span id="toc3">・①対象範囲内でエラーが起きたら指定位置へ飛ぶ</span></h3>



<p>まず、<strong><span class="marker-under-red">エラー発生時に指定位置に飛ぶ方法</span></strong>について紹介します。<br>この方法はエラーの根本的な解決方法にはなってませんが<br>エラー画面が発生しない1番手っ取り早い方法です。</p>



<p>プログラム構成はこのようになります。</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p><span class="bold-red">On Error Goto <span class="bold-blue">任意ラベル</span></span><br>　　・<br>　　・<br>　　・<br>通常プログラム<br>　　・<br>　　・<br>　　・<br>Exit Sub</p><p><span class="bold-blue">任意ラベル</span>：</p><p><span class="bold-green">エラー時の処理</span></p><p>End Sub</p></blockquote>



<p>つまり</p>



<p>「<span class="bold-red">On Error Goto<span class="bold-blue"> 任意ラベル</span></span>」から「<span class="bold-blue">任意ラベル</span>：」の範囲内で<br>異常があった場合、<span class="bold-green">エラー時の処理</span>まで飛んでくれます。</p>



<p>では先ほどのプログラムに織り込んでみましょう。</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p>Sub データ集計①織り込み()</p><p>Dim EndRow As Long<br>Dim Height As Long &#8216;身長<br>Dim Weight As Long &#8216;体重</p><p><span class="bold-red">On Error GoTo <span class="bold-blue">エラー1</span></span></p><p>EndRow = Cells(7, 2).End(xlDown).Row + 1</p><p>Height = Cells(3, 2)<br>Weight = Cells(3, 3)</p><p>Cells(EndRow, 2) = Height<br>Cells(EndRow, 3) = Weight<br>Cells(EndRow, 4) = Weight / (Height / 100) ^ 2</p><p>Range(&#8220;B3:C3&#8221;).ClearContents</p><p>Exit Sub</p><p><span class="bold-blue">エラー1</span>:</p><p><span class="bold-green">MsgBox (&#8220;誤った数値が入力されています&#8221;)</span></p><p>End Sub</p></blockquote>



<p>図解するとこのようになります。</p>



<figure class="wp-block-image size-full"><img decoding="async" width="655" height="379" src="https://dokugakuexcel.com/wp-content/uploads/2021/10/image-66.png" alt="" class="wp-image-1818" srcset="https://dokugakuexcel.com/wp-content/uploads/2021/10/image-66.png 655w, https://dokugakuexcel.com/wp-content/uploads/2021/10/image-66-300x174.png 300w, https://dokugakuexcel.com/wp-content/uploads/2021/10/image-66-330x190.png 330w, https://dokugakuexcel.com/wp-content/uploads/2021/10/image-66-120x68.png 120w" sizes="(max-width: 655px) 100vw, 655px" /></figure>



<p>では実際にエラーが出るように更新してみましょう。</p>



<figure class="wp-block-image size-full"><img decoding="async" width="366" height="253" src="https://dokugakuexcel.com/wp-content/uploads/2021/10/image-67.png" alt="" class="wp-image-1819" srcset="https://dokugakuexcel.com/wp-content/uploads/2021/10/image-67.png 366w, https://dokugakuexcel.com/wp-content/uploads/2021/10/image-67-300x207.png 300w" sizes="(max-width: 366px) 100vw, 366px" /></figure>



<p><strong><span class="marker-under-blue">身長に間違えて「A」</span></strong>を打ち込んだとします。<br>通常は身長を「Cells(EndRow, 2) = Height」で変数「Height」に格納するのですが<br><strong><span class="marker-under-blue">変数「Height」の型は「Long」なので文字は格納できません</span></strong>。</p>



<p>なので通常は下図のような異常が出ます。</p>



<figure class="wp-block-image size-full"><img decoding="async" width="401" height="198" src="https://dokugakuexcel.com/wp-content/uploads/2021/10/image-68.png" alt="" class="wp-image-1820" srcset="https://dokugakuexcel.com/wp-content/uploads/2021/10/image-68.png 401w, https://dokugakuexcel.com/wp-content/uploads/2021/10/image-68-300x148.png 300w" sizes="(max-width: 401px) 100vw, 401px" /></figure>



<p>では改善したプログラムを走らせてみます。<br>結果はこのようになります。</p>



<figure class="wp-block-image size-full"><img decoding="async" width="310" height="216" src="https://dokugakuexcel.com/wp-content/uploads/2021/10/image-69.png" alt="" class="wp-image-1821" srcset="https://dokugakuexcel.com/wp-content/uploads/2021/10/image-69.png 310w, https://dokugakuexcel.com/wp-content/uploads/2021/10/image-69-300x209.png 300w" sizes="(max-width: 310px) 100vw, 310px" /></figure>



<p>エラー処理にプログラムが飛んで、メッセージボックスの<br>「誤った数値が入力されています」が表示されました。<br>この後は<strong>「OK」を押せばプログラムは終わってくれます</strong>。</p>



<p>VBAのエラーを表示させたくない場合はプログラムの先頭に「<span class="bold-red">On Error GoTo　<span class="bold-blue">任意ラベル</span></span>」<br>最後に「<span class="bold-blue">任意ラベル</span>：」を置いて<br>そのあとに「<span class="bold-green">MsgBox (&#8220;エラーになりました。見直してください&#8221;）</span>」を入れれば<br>とりあえずVBAの異常は無くなり、メッセージでの出力にすることができました。</p>



<h3 class="wp-block-heading"><span id="toc4">・②IFを使用して異常値を検出して指定位置に飛ぶ</span></h3>



<p>今度はIFを使用して<strong><span class="marker-under">異常値を検出し、異常処理行まで飛んでみましょう</span></strong>。<br>この方法は先ほどの型が違うなどのエラーは適応外ですが<br><strong><span class="marker-under">エラーの根本を検出</span></strong>できます。</p>



<p>では先ほどの見本を元に進めていきます。</p>



<figure class="wp-block-image size-full"><img decoding="async" width="363" height="258" src="https://dokugakuexcel.com/wp-content/uploads/2021/10/image-70.png" alt="" class="wp-image-1822" srcset="https://dokugakuexcel.com/wp-content/uploads/2021/10/image-70.png 363w, https://dokugakuexcel.com/wp-content/uploads/2021/10/image-70-300x213.png 300w" sizes="(max-width: 363px) 100vw, 363px" /></figure>



<p>「B3」の身長を見てみましょう。<br>間違えて「0」を余分につけて「1720」になっています。</p>



<p>このまま更新ボタンを押すとそのまま計算・集計してしまい、正確な計算ができません。<br>ではIFを使用してエラーを防止してみましょう。</p>



<p>条件として<strong><span class="marker-under-red">身長の範囲を「120～200」</span></strong>までとします。</p>



<p>ではIFを使用して120～200以外は別の処理を行いたいと思います。<br>プログラムはこのようになります。</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p>If Height &lt; <strong><span class="marker-under-red">120</span></strong> Or Height &gt;<strong><span class="marker-under-red"> 200</span></strong> Then<br><span class="bold-green">MsgBox (&#8220;身長は120から200までで入力してください&#8221;)</span><br>Exit Sub<br>End If</p></blockquote>



<p>Heightは身長データを格納している変数とします。<br>IFを使い、Heightが120～200の範囲以外だと 「<span style="color: rgb(62, 179, 112); font-weight: bold;">身長は120から200までで入力してください</span> 」</p>



<p>とメッセージボックスを表示してExit Subでプログラムを終わらせます。<br>IFの詳しい使用方法は下記リンクを参照してみてください。☟</p>



<p><a href="https://dokugakuexcel.com/%e3%80%90if%e9%96%a2%e6%95%b0%e3%80%91%e3%82%a8%e3%82%af%e3%82%bb%e3%83%ab%e3%81%a7%e2%97%8b%e2%97%8b%e3%81%a0%e3%81%a3%e3%81%9f%e3%82%89%e2%96%b3%e2%96%b3%e3%82%92%e8%a1%a8%e7%a4%ba%e3%81%99%e3%82%8b/">【IF関数】エクセルで○○だったら△△を表示する～条件分岐を身に着ける～ ► 独学エクセル塾 (dokugakuexcel.com)</a></p>



<p>ではプログラムに組み込んでみましょう。</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p>Sub データ集計②織り込み()</p><p>Dim EndRow As Long<br>Dim Height As Long &#8216;身長<br>Dim Weight As Long &#8216;体重</p><p>EndRow = Cells(7, 2).End(xlDown).Row + 1</p><p>Height = Cells(3, 2)<br>Weight = Cells(3, 3)</p><p>If Height &lt; <strong><span class="marker-under-red">120 </span></strong>Or Height &gt; <strong><span class="marker-under-red">200</span></strong> Then<br><span class="bold-green">MsgBox (&#8220;身長は120から200までで入力してください&#8221;)</span><br>Exit Sub<br>End If</p><p>Cells(EndRow, 2) = Height<br>Cells(EndRow, 3) = Weight<br>Cells(EndRow, 4) = Weight / (Height / 100) ^ 2</p><p>Range(&#8220;B3:C3&#8221;).ClearContents</p><p>End Sub</p></blockquote>



<p>実行した結果はこのようになります。</p>



<figure class="wp-block-image size-full"><img decoding="async" width="338" height="212" src="https://dokugakuexcel.com/wp-content/uploads/2021/10/image-71.png" alt="" class="wp-image-1823" srcset="https://dokugakuexcel.com/wp-content/uploads/2021/10/image-71.png 338w, https://dokugakuexcel.com/wp-content/uploads/2021/10/image-71-300x188.png 300w" sizes="(max-width: 338px) 100vw, 338px" /></figure>



<p>IFで指定範囲外の数値を検出して</p>



<p>設定したメッセージでフィードバックが可能になりました。</p>



<h3 class="wp-block-heading"><span id="toc5">・③入力規則で入力データを制限する</span></h3>



<p>最後にVBAプログラムでの対処ではありませんが<br>エクセルの機能の<strong><span class="marker-under-red">入力規則を用いたエラーの回避</span></strong>を紹介します。</p>



<p>こちらはエラーの根本を解決する方法になっており、<br><strong><span class="marker-under-red">エラーになる入力をさせない方法</span></strong>です。</p>



<p>では先ほどと同様に身長を「120～200」で制限してみましょう。<br>入力制限対象を選択して<strong><span class="marker-under-blue">【データ】</span></strong>から<strong><span class="marker-under-blue">【データの入力規則】</span></strong>を選択してください。</p>



<figure class="wp-block-image"><img decoding="async" src="https://dokugakuexcel.com/wp-content/uploads/2021/08/image-10.png" alt=""/></figure>



<p>ここでは詳しく説明はしませんが<br>入力値の種類を「整数」にして最小値を「120」、最大限を「200」にします。<br>データを「次の値の間」にしました。</p>



<p>詳しい入力規則については下記リンクを参照してみてください☟</p>



<p><a href="https://dokugakuexcel.com/%e3%80%90%e3%83%aa%e3%82%b9%e3%83%88%e3%81%ae%e4%bd%9c%e6%88%90%e3%81%ab%e4%be%bf%e5%88%a9%e3%80%91%e3%83%87%e3%83%bc%e3%82%bf%e3%81%ae%e5%85%a5%e5%8a%9b%e8%a6%8f%e5%89%87%e3%81%ae%e8%a8%ad%e5%ae%9a/">【リストの作成に便利】データの入力規則の設定・制限から解除まで ► 独学エクセル塾 (dokugakuexcel.com)</a></p>



<figure class="wp-block-image size-full"><img decoding="async" width="488" height="366" src="https://dokugakuexcel.com/wp-content/uploads/2021/10/image-72.png" alt="" class="wp-image-1824" srcset="https://dokugakuexcel.com/wp-content/uploads/2021/10/image-72.png 488w, https://dokugakuexcel.com/wp-content/uploads/2021/10/image-72-300x225.png 300w" sizes="(max-width: 488px) 100vw, 488px" /></figure>



<p>これにより入力は「120～200」の整数のみ可能になります。<br>「A」などといった文字も制限されるのでとても便利です。</p>



<p></p>



<h2 class="wp-block-heading"><span id="toc6">&#x2b1b;︎公式の説明</span></h2>



<p>わかりやすいように説明したため公式と使用する語句が異なりますが<br>マイクロソフト公式の説明については下記のリンクを参照してください。☟</p>



<p><a href="https://learn.microsoft.com/ja-jp/office/vba/language/reference/user-interface-help/on-error-statement">On Error ステートメント (VBA) | Microsoft Learn</a></p>



<h2 class="wp-block-heading"><span id="toc7">■まとめ</span></h2>



<p>いかがだったでしょうか。</p>



<p>今回は対策を①から③の<br>①対象範囲内でエラーが起きたら指定位置へ飛ぶ<br>②IFを使用して異常値を検出して指定位置に飛ぶ<br>③入力規則で入力データを制限する<br>で紹介しました。</p>



<p>①～③の知識は、すべてエラーを防止するためには大切なのですが<br>操作する人が複数であればあるほど自分の想定を超えた処理を行い、<br>エラーになってしまいます。<br>そのためにも①の方法は入れておけば汎用性は上がると思います</p>



<p>ぜひ、①から③を織り込んでエラーに強いプログラムを作成してみてください。</p>



<p>それでは次回の記事でお会いしましょう。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://dokugakuexcel.com/%e3%80%90%e3%83%9e%e3%82%af%e3%83%advba%e3%81%ae%e3%82%a8%e3%83%a9%e3%83%bc%e5%af%be%e7%ad%96%e3%80%91%e3%82%a8%e3%83%a9%e3%83%bc%e3%81%ab%e8%b2%a0%e3%81%91%e3%81%aa%e3%81%84%e3%83%97%e3%83%ad/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1815</post-id>	</item>
	</channel>
</rss>
