<?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>MOD関数  |  独学エクセル塾</title>
	<atom:link href="https://dokugakuexcel.com/tag/mod%E9%96%A2%E6%95%B0/feed/" rel="self" type="application/rss+xml" />
	<link>https://dokugakuexcel.com</link>
	<description>初心者から中級者までシステム・関数・VBAを伝授します</description>
	<lastBuildDate>Sun, 02 Jul 2023 15:05:25 +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>MOD関数  |  独学エクセル塾</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】Mod関数を使って割り算の余りを求める方法！偶数・奇数の数を数えるにも活用！</title>
		<link>https://dokugakuexcel.com/%e3%80%90%e3%83%9e%e3%82%af%e3%83%advba%e3%80%91mod%e9%96%a2%e6%95%b0%e3%82%92%e4%bd%bf%e3%81%a3%e3%81%a6%e5%89%b2%e3%82%8a%e7%ae%97%e3%81%ae%e4%bd%99%e3%82%8a%e3%82%92%e6%b1%82%e3%82%81%e3%82%8b/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=%25e3%2580%2590%25e3%2583%259e%25e3%2582%25af%25e3%2583%25advba%25e3%2580%2591mod%25e9%2596%25a2%25e6%2595%25b0%25e3%2582%2592%25e4%25bd%25bf%25e3%2581%25a3%25e3%2581%25a6%25e5%2589%25b2%25e3%2582%258a%25e7%25ae%2597%25e3%2581%25ae%25e4%25bd%2599%25e3%2582%258a%25e3%2582%2592%25e6%25b1%2582%25e3%2582%2581%25e3%2582%258b</link>
					<comments>https://dokugakuexcel.com/%e3%80%90%e3%83%9e%e3%82%af%e3%83%advba%e3%80%91mod%e9%96%a2%e6%95%b0%e3%82%92%e4%bd%bf%e3%81%a3%e3%81%a6%e5%89%b2%e3%82%8a%e7%ae%97%e3%81%ae%e4%bd%99%e3%82%8a%e3%82%92%e6%b1%82%e3%82%81%e3%82%8b/?noamp=mobile#respond</comments>
		
		<dc:creator><![CDATA[matsu]]></dc:creator>
		<pubDate>Tue, 27 Jun 2023 15:01:03 +0000</pubDate>
				<category><![CDATA[計算・関数]]></category>
		<category><![CDATA[マクロVBA]]></category>
		<category><![CDATA[割り算]]></category>
		<category><![CDATA[余り]]></category>
		<category><![CDATA[MOD関数]]></category>
		<category><![CDATA[計算]]></category>
		<guid isPermaLink="false">https://dokugakuexcel.com/?p=4310</guid>

					<description><![CDATA[割り算をした後の余りを求める関数として「Mod関数」があります。 こちらは、対象のセル・変数などの数値をもとに割り算をした後の余りを求める関数となっています。 さらにMod関数を使用すれば、簡単に偶数または奇数の判別がで [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p>割り算をした後の余りを求める関数として<br /><span class="marker-under"><strong>「Mod関数」</strong></span>があります。</p>



<p>こちらは、対象のセル・変数などの数値をもとに割り算をした後の余りを求める関数となっています。</p>





<p><strong><span class="marker-under-blue">さらにMod関数を使用すれば、簡単に偶数または奇数の判別ができる</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-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></li><li><a href="#toc4" tabindex="0">&#x2b1b;︎Modで割ったあとのあまりを求める</a><ol><li><a href="#toc5" tabindex="0">・通常の使用方法</a></li></ol></li><li><a href="#toc6" tabindex="0">■Mod関数を使用した応用方法</a><ol><li><a href="#toc7" tabindex="0">・偶数・奇数の数を数える</a></li></ol></li><li><a href="#toc8" tabindex="0">&#x2b1b;︎よくあるエラーと対処法</a><ol><li><a href="#toc9" tabindex="0">・#DIV/0! エラー</a></li><li><a href="#toc10" tabindex="0">・#VALUE! エラー</a></li><li><a href="#toc11" tabindex="0">・#NUM! エラー</a></li></ol></li><li><a href="#toc12" tabindex="0">■練習問題</a></li><li><a href="#toc13" tabindex="0">&#x2b1b;︎公式の説明</a></li><li><a href="#toc14" tabindex="0">&#x2b1b;︎まとめ</a></li></ol>
    </div>
  </div>

<h2 class="wp-block-heading"><span id="toc1">&#x2b1b;︎こんな事ができるようになる！</span></h2>



<p>・VBAのMod関数を使って<span class="marker-under"><strong>割り算の余りを求める</strong></span>ことができる<br />・Mod関数を使って<span class="marker-under"><strong>偶数・奇数の数を数える</strong></span>方法を学ぶことができる</p>
<p><img fetchpriority="high" decoding="async" width="889" height="211" class="alignnone wp-image-4312 size-full" src="https://dokugakuexcel.com/wp-content/uploads/2023/06/Pasted-15.png" srcset="https://dokugakuexcel.com/wp-content/uploads/2023/06/Pasted-15.png 889w, https://dokugakuexcel.com/wp-content/uploads/2023/06/Pasted-15-300x71.png 300w, https://dokugakuexcel.com/wp-content/uploads/2023/06/Pasted-15-768x182.png 768w" sizes="(max-width: 889px) 100vw, 889px" /></p>







<h2 class="wp-block-heading"><span id="toc2">&#x2b1b;︎忙しい人向けのコピペ用ソースコード</span></h2>



<pre class="wp-block-verse">Sub 余りを求める()<br /><br />Dim 数値1 As Integer<br />Dim 数値2 As Integer<br />Dim 余り As Integer<br /><br /> 数値1 = <span class="bold-red">対象のセル・変数1</span><br /> 数値2 = <span class="bold-red">対象のセル・変数2</span><br /> 余り = 数値1 Mod 数値2<br /><br />MsgBox "割り算の余りは: " &amp; 余り<br /><br />End Sub</pre>



<p><span class="bold-red">対象のセル・変数</span>をカスタムして使用してください。</p>



<h2 class="wp-block-heading"><span id="toc3">&#x2b1b;︎構文の説明</span></h2>



<p>まず、使用する関数についてです<strong><span class="marker-under">Mod関数</span></strong>」になります。<br />関数仕様としては下記の通りです。</p>



<div class="information-box common-icon-box"><strong><span style="color: #0000ff;">余り</span> </strong>= <strong><span style="color: #ff0000;">数値1</span> </strong>Mod <span style="color: #ff0000;"><strong>数値2</strong></span></div>
<div>とてもシンプルな関数ですね。</div>
<div>では実際に使用した例を紹介したいと思います。</div>









<h2 class="wp-block-heading"><span id="toc4">&#x2b1b;︎Modで割ったあとのあまりを求める</span></h2>



<h3 class="wp-block-heading"><span id="toc5">・通常の使用方法</span></h3>



<p>Mod関数を<span class="marker-under"><strong>通常の割り算の余りの求め方として使用する方法</strong></span>を紹介します。<br />下図の見本をご覧ください。</p>



<figure class="wp-block-image size-full"><img decoding="async" width="386" height="206" class="alignnone wp-image-4313 size-full" src="https://dokugakuexcel.com/wp-content/uploads/2023/06/Pasted-16.png" srcset="https://dokugakuexcel.com/wp-content/uploads/2023/06/Pasted-16.png 386w, https://dokugakuexcel.com/wp-content/uploads/2023/06/Pasted-16-300x160.png 300w" sizes="(max-width: 386px) 100vw, 386px" /></figure>
<p>今回は数値1から数値2を割って、その余りを求めたいと思います。<br />では再度構文を見ていきましょう。</p>
<div class="blank-box bb-yellow"><strong><span style="color: #0000ff;">余り</span> </strong>= <strong><span style="color: #ff0000;">数値1</span> </strong>Mod <span style="color: #ff0000;"><strong>数値2</strong></span></div>
<div>とてもシンプルですね。<br />では構文に当てはめてみましょう。<br />ソースコードは下記の通りです。</div>
<div>
<p>&nbsp;</p>



<pre class="wp-block-verse">Sub 通常の使用方法()<br /><br />Dim <strong><span style="color: #ff0000;">数値1</span></strong> As Integer<br />Dim <span style="color: #ff9900;"><strong>数値2</strong></span> As Integer<br />Dim <span style="color: #0000ff;"><strong>余り</strong> </span>As Integer<br /><br /> <strong><span style="color: #ff0000;">数値1</span></strong> = Cells(2, 3)<br /> <span style="color: #ff9900;"><strong>数値2</strong></span> = Cells(3, 3)<br /> <span style="color: #0000ff;"><strong>余り</strong> </span>= <strong><span style="color: #ff0000;">数値1</span></strong> <strong>Mod</strong> <span style="color: #ff9900;"><strong>数値2</strong></span><br /><br /> Cells(6, 3) = <span style="color: #0000ff;"><strong>余り</strong> </span><br /><br />End Sub</pre>



<p>それでは実行してみましょう。<br />結果はこのようになります。</p>
<p><img decoding="async" width="433" height="211" class="alignnone wp-image-4314 size-full" src="https://dokugakuexcel.com/wp-content/uploads/2023/06/Pasted-17.png" srcset="https://dokugakuexcel.com/wp-content/uploads/2023/06/Pasted-17.png 433w, https://dokugakuexcel.com/wp-content/uploads/2023/06/Pasted-17-300x146.png 300w" sizes="(max-width: 433px) 100vw, 433px" /></p>
</div>
<div>「10/3」の結果は<span class="marker-under"><strong>「3あまり1」なので余りの「1」が出力</strong></span>されています。<br />うまく機能していますね。</div>



<h2 class="wp-block-heading"><span id="toc6">■Mod関数を使用した応用方法</span></h2>
<p>ではMod関数を使用した応用方法を紹介します。</p>



<h3 class="wp-block-heading"><span id="toc7">・偶数・奇数の数を数える</span></h3>



<p>Mod関数を使って、<span class="marker-under"><strong>指定した範囲内の偶数・奇数の数を数える方法</strong></span>を紹介します。<br />今回は「偶数」を数えてみます。<br />下図の見本をご覧ください。</p>



<figure class="wp-block-image size-full is-resized"><img decoding="async" width="358" height="342" class="alignnone wp-image-4315 size-full" src="https://dokugakuexcel.com/wp-content/uploads/2023/06/Pasted-18.png" srcset="https://dokugakuexcel.com/wp-content/uploads/2023/06/Pasted-18.png 358w, https://dokugakuexcel.com/wp-content/uploads/2023/06/Pasted-18-300x287.png 300w" sizes="(max-width: 358px) 100vw, 358px" /></figure>
<p><strong><span style="color: #ff0000;">C2からC7</span></strong>までにある数値の中で<span style="color: #0000ff;"><strong>偶数の数</strong></span>を数えたいと思います。<br />そこで今回は<span class="marker-under"><strong>「Mod関数」</strong></span>を応用して数えてみましょう。</p>
<p>偶数か、判別するためにはMod演算で２で割ったあまりを求めます。</p>
<p>つまり、<strong>「If <span style="color: #0095d9;">対象の数値</span> Mod 2 = 0  」</strong>で対象の値を2で割ったのち、その値が「0」か条件分岐します。<br /><span class="marker-under"><strong>結果が「0」の場合は偶数なのでカウントアップをする</strong></span>仕組みです。</p>
<p>条件分岐は下記記事を参照してください。</p>
<div class="information-box common-icon-box"><a href="https://dokugakuexcel.com/%e3%80%90vba%e7%9f%a5%e8%ad%98-9%e3%80%91if-then%e3%81%a7%e6%9d%a1%e4%bb%b6%e5%88%86%e5%b2%90%e3%83%bb%e5%88%a4%e5%88%a5%e3%82%92%e3%81%99%e3%82%8b%e2%97%8b%e2%97%8b%e3%81%aa%e3%82%89/">【マクロVBA】If …Thenで条件分岐・判別をする!Elseで異なる処理パターンも可能に ► 独学エクセル塾 (dokugakuexcel.com)</a></div>
<p>では見本のソースコードを見ていきましょう。</p>
<p>

</p>
<pre class="wp-block-verse">Sub 偶数の数を数える()<br /><br />Dim <span style="color: #0000ff;"><strong>偶数の数</strong></span> As Integer<br />Dim i As Integer<br /><br /><span style="color: #0000ff;"><strong>偶数の数</strong></span> = 0<br />For <span style="color: #ff0000;"><strong>i</strong> </span>= <strong><span style="color: #ff0000;">2</span> </strong>To <span style="color: #ff0000;"><strong>7</strong></span><br />　If Cells(<span style="color: #ff0000;"><strong>i</strong></span>, 3) <span class="marker-under"><strong>Mod</strong></span> 2 = 0 Then<br />　　<span style="color: #0000ff;"><strong>偶数の数</strong></span> = <span style="color: #0000ff;"><strong>偶数の数</strong></span> + 1<br />　End If<br />Next i<br /><br />Cells(13, 3) = <span style="color: #0000ff;"><strong>偶数の数</strong></span><br /><br />End Sub</pre>
<p>

</p>
<p>それでは実行してみましょう。<br />結果はこのようになります。</p>
<p><img decoding="async" class="alignnone wp-image-4316 " src="https://dokugakuexcel.com/wp-content/uploads/2023/06/Pasted-19.png" width="291" height="289" srcset="https://dokugakuexcel.com/wp-content/uploads/2023/06/Pasted-19.png 324w, https://dokugakuexcel.com/wp-content/uploads/2023/06/Pasted-19-300x298.png 300w, https://dokugakuexcel.com/wp-content/uploads/2023/06/Pasted-19-150x150.png 150w, https://dokugakuexcel.com/wp-content/uploads/2023/06/Pasted-19-100x100.png 100w" sizes="(max-width: 291px) 100vw, 291px" /></p>
<p>対象範囲に偶数は「3」つあるので上手く機能していますね！</p>



<h2 class="wp-block-heading"><span id="toc8">&#x2b1b;︎よくあるエラーと対処法</span></h2>



<h3 class="wp-block-heading"><span id="toc9">・#DIV/0! エラー</span></h3>



<p><span class="marker-under-red"><strong> エラーの原因</strong></span>: MOD関数の除数がゼロの場合、このエラーが発生します。<br /><br /><span class="marker-under-blue"><strong> 対処法:</strong></span> 除数がゼロにならないように確認してください。<br />            条件分岐などを使って、除数がゼロの場合にエラーを回避するような処理を追加することができます。</p>
<h3><span id="toc10">・#VALUE! エラー</span></h3>
<p>

</p>
<p><span class="marker-under-red"><strong> エラーの原因</strong></span>: MOD関数の引数に数値以外の値が含まれている場合、このエラーが発生します。<br />                     例えば、文字列やエラー値が引数として渡された場合です。<br /><br /><span class="marker-under-blue"><strong> 対処法:</strong></span>: 引数の値が正しいデータ型かどうかを確認してください。<br />              数値以外の値が渡されないようにするか、適切なエラーハンドリングを行いましょう。</p>
<h3><span id="toc11">・#NUM! エラー</span></h3>
<p>

</p>
<p><span class="marker-under-red"><strong> エラーの原因</strong></span>: MOD関数の引数に無効な値が含まれている場合、このエラーが発生します。<br />                     例えば、非数値を含む引数や、非整数を除数として使用した場合です。<br /><br /><span class="marker-under-blue"><strong> 対処法:</strong></span>: 引数の値が正しい範囲内にあるか、整数であるかを確認してください。<br />              引数に整数を使用するか、必要に応じて値を修正してください。</p>
<p>&nbsp;</p>
<p>これらのエラーはMOD関数の使用時に注意が必要です。適切な値を使用し、エラーハンドリングを行うことで、正確な結果を得ることができます。</p>
<h2><span id="toc12">■練習問題</span></h2>
<p>練習問題も別の記事でまとめているので是非参考にしてみてください！</p>
<div class="alert-box common-icon-box"><a href="https://dokugakuexcel.com/%e3%80%90%e3%83%9e%e3%82%af%e3%83%advba%e7%b7%b4%e7%bf%92%e5%95%8f%e9%a1%8c8%e3%80%91%e5%81%b6%e6%95%b0%e3%83%bb%e5%8f%88%e3%81%af%e5%a5%87%e6%95%b0%e3%81%ae%e5%80%8b%e6%95%b0%e3%82%92%e8%a8%88/">【マクロVBA練習問題8】偶数・又は奇数の個数を計算・判別し、その結果を表示する ► 独学エクセル塾 (dokugakuexcel.com)</a></div>



<h2 class="wp-block-heading"><span id="toc13">&#x2b1b;︎公式の説明</span></h2>



<p>わかりやすいように説明したため公式と使用する語句が異なりますが<br />マイクロソフト公式の説明については下記のリンクを参照してください。☟</p>





<p><a href="https://learn.microsoft.com/ja-jp/office/vba/language/reference/user-interface-help/mod-operator">Mod 演算子 | Microsoft Learn</a></p>



<h2 class="wp-block-heading"><span id="toc14">&#x2b1b;︎まとめ</span></h2>



<p>いかがだったでしょうか？<br />この記事では、VBAのMod関数を使って割り算の余りを求める方法と、偶数の数を数える方法について紹介しました。<br />Mod関数は、さまざまなシナリオで活用することができるので、ぜひ応用してみてください。<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%80%91mod%e9%96%a2%e6%95%b0%e3%82%92%e4%bd%bf%e3%81%a3%e3%81%a6%e5%89%b2%e3%82%8a%e7%ae%97%e3%81%ae%e4%bd%99%e3%82%8a%e3%82%92%e6%b1%82%e3%82%81%e3%82%8b/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">4310</post-id>	</item>
	</channel>
</rss>
