<?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>最終行 | nujonoa_blog</title>
	<atom:link href="https://nujonoa.com/tag/%e6%9c%80%e7%b5%82%e8%a1%8c/feed/" rel="self" type="application/rss+xml" />
	<link>https://nujonoa.com</link>
	<description>人生に役立つデータ集</description>
	<lastBuildDate>Sun, 31 Jan 2021 02:04:46 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.8.3</generator>

<image>
	<url>https://nujonoa.com/wp-content/uploads/2019/04/cropped-DSC00976-e1554456145409-32x32.jpg</url>
	<title>最終行 | nujonoa_blog</title>
	<link>https://nujonoa.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>【VBA】最終行、最終列を取得する方法3選</title>
		<link>https://nujonoa.com/how-to-get-the-last-row/</link>
					<comments>https://nujonoa.com/how-to-get-the-last-row/#respond</comments>
		
		<dc:creator><![CDATA[nujonoa]]></dc:creator>
		<pubDate>Sun, 31 Jan 2021 01:43:50 +0000</pubDate>
				<category><![CDATA[マクロVBA]]></category>
		<category><![CDATA[vba]]></category>
		<category><![CDATA[最終行]]></category>
		<category><![CDATA[最終列]]></category>
		<guid isPermaLink="false">http://nujonoa.com/?p=8702</guid>

					<description><![CDATA[最終行、最終列を取得する方法を紹介します。 目次 【VBA】最終行を取得する方法3選「Ctrl+↑」「Ctrl+←」を再現「Ctrl+↓」「Ctrl+→」を再現最終行(空白はNG)を検出 【VBA】最終行を取得する方法3 [&#8230;]]]></description>
										<content:encoded><![CDATA[
<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">【VBA】最終行を取得する方法3選</a><ol><li><a href="#toc2" tabindex="0">「Ctrl+↑」「Ctrl+←」を再現</a></li><li><a href="#toc3" tabindex="0">「Ctrl+↓」「Ctrl+→」を再現</a></li><li><a href="#toc4" tabindex="0">最終行(空白はNG)を検出</a></li></ol></li></ol>
    </div>
  </div>

<h2 class="wp-block-heading"><span id="toc1">【VBA】最終行を取得する方法3選</span></h2>



<p>最終行、最終列をとってくるコードは、<br>「一番使うコードなのに」忘れてしまうんです…</p>



<p>ということで、今回は覚書で3種類さっと紹介したいと思います。</p>



<h3 class="wp-block-heading"><span id="toc2">「Ctrl+↑」「Ctrl+←」を再現</span></h3>



<p>「Ctrl+↑」「Ctrl+←」を再現して最終行を持ってくるのが、<br>下記プログラムです。<br>データが入っている行を選択してあげる必要があるので注意が必要です。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: vb; title: ; notranslate">
LASTY = Cells(Rows.Count, 2).End(xlUp).Row '1を任意に替える
LASTX = Cells(2, Columns.Count).End(xlToLeft).Column '1を任意に替える

Debug.Print LASTY, LASTX
</pre></div>


<h3 class="wp-block-heading"><span id="toc3">「Ctrl+↓」「Ctrl+→」を再現</span></h3>



<p>「Ctrl+↓」「Ctrl+→」を再現して最終行を持ってくるのが、<br>下記プログラムです。<br>こちらもデータが入っている行を選択してあげる必要があるので注意が必要です。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: vb; title: ; notranslate">
LASTY = Cells(1,3).End(xlDown).Row
LASTY = Cells(1,3).End(xlToRight).Column
</pre></div>


<h3 class="wp-block-heading"><span id="toc4">最終行(空白はNG)を検出</span></h3>



<p>使っている範囲をカウントするので、<br>空白行があると(最終行-空白行)が検出されてしまうのが、少々問題ですが、<br>一番きれいなのがこの関数です。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: vb; title: ; notranslate">
LASTY=ActiveSheet.UsedRange.Rows.Count
LASTX=ActiveSheet.UsedRange.Columns.Count
</pre></div>


<p>下記のような場合、上記のコードでは、<br>・LASTY=10<br>・LASTX=4<br>となってしまいます。<br>・使っている行として罫線もカウントされますので、<br>　11行目までカウントされますが、1行目は使ってないので、答えは11-1=10<br>・使っている列は、Eまでですカウントされますが、<br>　A列は使ってないので、5-1=4が返されます。<br>ですので、「A1セル」をタイトルか何かで埋めておく必要があります。</p>



<figure class="wp-block-image size-large"><a rel="noopener" target="_blank" href="https://nujonoa.com/wp-content/uploads/2021/01/image-55.png"><img fetchpriority="high" decoding="async" width="422" height="316" src="https://nujonoa.com/wp-content/uploads/2021/01/image-55.png" alt="" class="wp-image-8725" srcset="https://nujonoa.com/wp-content/uploads/2021/01/image-55.png 422w, https://nujonoa.com/wp-content/uploads/2021/01/image-55-300x225.png 300w" sizes="(max-width: 422px) 100vw, 422px" /></a></figure>
]]></content:encoded>
					
					<wfw:commentRss>https://nujonoa.com/how-to-get-the-last-row/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
