<?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/%e3%82%b5%e3%83%96%e3%83%95%e3%82%a9%e3%83%ab%e3%83%80/feed/" rel="self" type="application/rss+xml" />
	<link>https://nujonoa.com</link>
	<description>人生に役立つデータ集</description>
	<lastBuildDate>Thu, 04 Feb 2021 08:42:04 +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】サブフォルダまで検索し、特定のシートを実行する方法</title>
		<link>https://nujonoa.com/search-to-subfolder-and-execute/</link>
					<comments>https://nujonoa.com/search-to-subfolder-and-execute/#respond</comments>
		
		<dc:creator><![CDATA[nujonoa]]></dc:creator>
		<pubDate>Mon, 01 Feb 2021 11:00:00 +0000</pubDate>
				<category><![CDATA[マクロVBA]]></category>
		<category><![CDATA[サブフォルダ]]></category>
		<category><![CDATA[実行]]></category>
		<category><![CDATA[Dir]]></category>
		<guid isPermaLink="false">http://nujonoa.com/?p=8712</guid>

					<description><![CDATA[目次 【VBA】サブフォルダまで検索し、特定のシートを実行する方法サブフォルダまで検索し、実行するプログラム紹介プログラムの説明 【VBA】サブフォルダまで検索し、特定のシートを実行する方法 VBAを使用して、データベー [&#8230;]]]></description>
										<content:encoded><![CDATA[

  <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】サブフォルダまで検索し、特定のシートを実行する方法</a></li><li><a href="#toc2" tabindex="0">サブフォルダまで検索し、実行するプログラム紹介</a></li><li><a href="#toc3" tabindex="0">プログラムの説明</a></li></ol>
    </div>
  </div>

<h2 class="wp-block-heading"><span id="toc1">【VBA】サブフォルダまで検索し、特定のシートを実行する方法</span></h2>



<p>VBAを使用して、データベースを構築しようとすると、<br>・人によってはサブフォルダを使ってきれいにデータを保存している人<br>・乱雑にデータが並んでいる人<br>など、様々なデータの持ち方をしていることがわかります。</p>



<p>そこで今回は、サブフォルダーまですべて読み込んで、<br>ファイルを実するプログラムを書いていきたいと思います。</p>



<h2 class="wp-block-heading"><span id="toc2">サブフォルダまで検索し、実行するプログラム紹介</span></h2>



<p>サブフォルダまで検索し、指定した名前と一致した場合にそのファイルを<br>実行するプログラムは下記の通りです。<br>↓のプログラムの場合は、「C:\Downloads」の中の「SAMPLE1.xls」を実行しています。</p>



<p>※ダブルクリックでコピーできます。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: vb; title: ; notranslate">
Sub JIKKOU()

        Call searchFolder(&quot;C:\Downloads&quot;)'←検索したいフォルダを指定する。
    
End Sub


Sub searchFolder(Path As String)
    Dim buf As String, childf As Object

'フォルダ内全部
    buf = Dir(Path &amp; &quot;\*.*&quot;)
   
    Do While buf &lt;&gt; &quot;&quot;
    
    '名前が一致したら実行するように指示。
    If buf = &quot;SAMPLE1.xls&quot; Then　'←ファイル名を指定してください。
    
        With CreateObject(&quot;Wscript.Shell&quot;)
            .Run Path &amp; &quot;\&quot; &amp; buf
        End With
        
        Exit Sub

    End If
    '次のファイル
        buf = Dir()
    Loop
    
    '子フォルダも同様に、subFolderを実行する。
    With CreateObject(&quot;Scripting.FileSystemObject&quot;)
        For Each childf In .GetFolder(Path).SubFolders
            Call searchFolder(childf.Path)
        Next
    End With
    
End Sub


</pre></div>


<h2 class="wp-block-heading"><span id="toc3">プログラムの説明</span></h2>



<p>まずは、1階層目を検索するプログラムです。<br>Dir関数とDo While関数を使って、<br>・Path内のフォルダを頭からすべて検索していきます。<br>・その中で(IF文で)、名前が一致したときに、Wscript.Shellでファイルを実行しています。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: vb; title: ; notranslate">
'フォルダ内全部
    buf = Dir(Path &amp; &quot;\*.*&quot;)
    Do While buf &lt;&gt; &quot;&quot;
    
    '名前が一致したら実行する場合は↓
    If buf = &quot;SAMPLE1.xls&quot; Then　'←ファイル名を指定してください。
    
        With CreateObject(&quot;Wscript.Shell&quot;)
            .Run Path &amp; &quot;\&quot; &amp; buf
        End With
        
        Exit Sub

    End If
    '次のファイル
        buf = Dir()
    Loop
</pre></div>


<p>ここまでは、よくある定型文のテクニックです。</p>



<p>ここから、2階層目以降を検索するために、<br>この部分があります。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: vb; title: ; notranslate">
    '子フォルダも同様に、subFolderを実行する。
    With CreateObject(&quot;Scripting.FileSystemObject&quot;)
        For Each childf In .GetFolder(Path).SubFolders
            Call searchFolder(childf.Path)
        Next
    End With
</pre></div>


<p>子フォルダに対しても、1階層目と同じ処理をしたいので、<br>関数内で同じ関数を繰り返しています。</p>



<p>こうすることで、サブフォルダがある限りこの関数内をループし続け、<br>すべてのファイルを検索することができます。</p>



<p>理解してしまえばそこまで難しいと思いませんので、<br>是非ご活用ください。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://nujonoa.com/search-to-subfolder-and-execute/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
