{"id":635,"date":"2021-05-20T23:39:54","date_gmt":"2021-05-20T14:39:54","guid":{"rendered":"https:\/\/sunafukin.jp\/?p=635"},"modified":"2021-05-21T19:57:47","modified_gmt":"2021-05-21T10:57:47","slug":"vba%e3%83%95%e3%82%a9%e3%83%ab%e3%83%80%e3%81%ae%e4%b8%ad%e3%81%ae%e3%83%95%e3%82%a1%e3%82%a4%e3%83%ab%e7%be%a4%e3%82%92%e5%88%97%e6%8c%99%e3%81%99%e3%82%8b","status":"publish","type":"post","link":"https:\/\/sunafukin.jp\/?p=635","title":{"rendered":"VBA:Dir\u95a2\u6570\u3092\u5229\u7528\u3057\u3066\u30d5\u30a9\u30eb\u30c0\u306e\u4e2d\u306e\u30d5\u30a1\u30a4\u30eb\u7fa4\u3092\u5217\u6319\u3059\u308b"},"content":{"rendered":"<h2>Dir\u95a2\u6570\u3092\u5229\u7528\u3057\u3001\u9078\u629e\u3057\u305f\u30d5\u30a9\u30eb\u30c0\u306e\u4e2d\u306e\u30d5\u30a1\u30a4\u30eb\u7fa4\u3092\u5217\u6319\u3059\u308b<\/h2>\n<p>Dir\u95a2\u6570\u3092\u5229\u7528\u3057\u3066\u3001\u30d5\u30a9\u30eb\u30c0\u306e\u4e2d\u306e\u30d5\u30a1\u30a4\u30eb\u7fa4\u3092\u5217\u6319\u3059\u308b\u65b9\u6cd5\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002<\/p>\n<p>\u95a2\u6570\u306b\u3057\u3066\u3044\u307e\u3059\u306e\u3067\u3001\u305d\u306e\u307e\u307e\u30b3\u30d4\u30fc\u3057\u3066\u5229\u7528\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre class=\"show-lang:2 lang:vb decode:true \">Function \u30d5\u30a1\u30a4\u30eb\u3092\u5217\u6319\u3059\u308b(strFolder As String, ByRef strGetFiles() As String)\r\n\r\n    Dim strFile As String\r\n    ReDim strGetFiles(0)   ' \u5f37\u5236\u7684\u306b\u521d\u671f\u5316\r\n    \r\n    If Right(strFolder, 1) &lt;&gt; \"\\\" Then\r\n        strFolder = strFolder &amp; \"\\\"\r\n    End If\r\n        \r\n    strFile = Dir(strFolder)\r\n    Do While strFile &lt;&gt; \"\"\r\n        If strGetFiles(0) &lt;&gt; \"\" Then\r\n            ReDim Preserve strGetFiles(UBound(strGetFiles) + 1)\r\n        End If\r\n        strGetFiles(UBound(strGetFiles)) = strFile\r\n        strFile = Dir()\r\n    Loop\r\n\r\nEnd Function<\/pre>\n<h3>\u95a2\u6570\u306e\u8aac\u660e<\/h3>\n<p>strFolder:\u521d\u671f\u8a2d\u5b9a\u3059\u308b\u30d5\u30a9\u30eb\u30c0\u3092\u6307\u5b9a\u3057\u307e\u3059\u3002<\/p>\n<p>strGetFile():\u5217\u6319\u3057\u305f\u30d5\u30a1\u30a4\u30eb\u3092\u8fd4\u3057\u307e\u3059\u3002<\/p>\n<h3>\u4f7f\u7528\u4f8b<\/h3>\n<p>C2\u30bb\u30eb\u306b\u30d5\u30a9\u30eb\u30c0\u304c\u5165\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>C2\u30bb\u30eb\u306e\u30d5\u30a9\u30eb\u30c0\u3092\u521d\u671f\u8a2d\u5b9a\u3068\u3057\u3066\u3001\u300c\u30d5\u30a9\u30eb\u30c0\u3092\u9078\u629e\u3059\u308b\u300d\u95a2\u6570\u306b\u6e21\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u9078\u629e\u3057\u305f\u30d5\u30a9\u30eb\u30c0\u306e\u4e2d\u306e\u30d5\u30a1\u30a4\u30eb\u7fa4\u3092\u5217\u6319\u3059\u308b\u305f\u3081\u300c\u30d5\u30a1\u30a4\u30eb\u3092\u5217\u6319\u3059\u308b\u300d\u95a2\u6570\u306b\u6e21\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u5217\u6319\u3057\u305f\u30d5\u30a1\u30a4\u30eb\u7fa4\u3092\u3001C3\u30bb\u30eb\u4ee5\u964d\u306b\u4ee3\u5165\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:vb decode:true\">Sub \u30d5\u30a9\u30eb\u30c0\u3092\u9078\u629e\u3059\u308b_Click()\r\n\r\n    Dim strFolder As String\r\n    Dim strGetFiles() As String\r\n    \r\n    strFolder = Range(\"c2\")\r\n    \r\n    strFolder = \u30d5\u30a9\u30eb\u30c0\u3092\u9078\u629e\u3059\u308b(strFolder)\r\n    Range(\"c2\") = strFolder\r\n    \r\n    Dim i As Long\r\n\r\n    ' \u30d5\u30a1\u30a4\u30eb\u3092\u5217\u6319\u3059\u308b\r\n    If strFolder &lt;&gt; \"\" Then\r\n        bret = \u30d5\u30a1\u30a4\u30eb\u3092\u5217\u6319\u3059\u308b(strFolder, strGetFiles)\r\n    \r\n        ' \u5217\u6319\u3057\u305f\u30d5\u30a1\u30a4\u30eb\u3092\u30bb\u30eb\u306b\u66f8\u304d\u8fbc\u3080\r\n        If strGetFiles(0) &lt;&gt; \"\" Then\r\n            For i = 0 To UBound(strGetFiles)\r\n                If strGetFiles(i) &lt;&gt; \"\" Then\r\n                    Range(\"c\" &amp; 3 + i) = strGetFiles(i)\r\n                End If\r\n            Next i\r\n        End If\r\n    End If\r\n    \r\nEnd Sub\r\n\r\n' FileDialog\u3092\u5229\u7528\u3057\u3066\u30d5\u30a9\u30eb\u30c0\u3092\u53c2\u7167\u3059\u308b\r\nFunction \u30d5\u30a9\u30eb\u30c0\u3092\u9078\u629e\u3059\u308b(strFolder As String)\r\n\r\n    If Right(strFolder, 1) &lt;&gt; \"\\\" Then\r\n        strFolder = strFolder &amp; \"\\\"\r\n    End If\r\n    With Application.FileDialog(msoFileDialogFolderPicker)\r\n        .InitialFileName = strFolder    ' \u30d5\u30a9\u30eb\u30c0\u306e\u521d\u671f\u5024\u3092\u8a2d\u5b9a\r\n        .Title = \"\u30d5\u30a9\u30eb\u30c0\u3092\u9078\u629e\"\r\n        If .Show = True Then\r\n            \u30d5\u30a9\u30eb\u30c0\u3092\u9078\u629e\u3059\u308b = .SelectedItems(1)\r\n        End If\r\n    End With\r\n\r\nEnd Function<\/pre>\n<h2>\u5fdc\u7528:Dir\u95a2\u6570\u3092\u5229\u7528\u3057\u3001\u30d5\u30a9\u30eb\u30c0\u306e\u4e2d\u304b\u3089<span class=\"marker-thickBlue\">\u5bfe\u8c61\u3068\u3059\u308b\u30d5\u30a1\u30a4\u30eb\u7fa4\u3092\u5217\u6319<\/span>\u3059\u308b<\/h2>\n<p>Dir\u95a2\u6570\u3092\u5229\u7528\u3057\u3066\u3001\u30d5\u30a9\u30eb\u30c0\u306e\u4e2d\u304b\u3089<span class=\"marker-halfBlue\">\u5bfe\u8c61\u3068\u3059\u308b\u30d5\u30a1\u30a4\u30eb\u7fa4\u3092\u5217\u6319\u3059\u308b<\/span>\u65b9\u6cd5\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002<\/p>\n<p>\u95a2\u6570\u306b\u3057\u3066\u3044\u307e\u3059\u306e\u3067\u3001\u305d\u306e\u307e\u307e\u30b3\u30d4\u30fc\u3057\u3066\u5229\u7528\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre class=\"show-lang:2 lang:vb decode:true \">Function \u30d5\u30a1\u30a4\u30eb\u3092\u5217\u6319\u3059\u308b(strFolder As String, ByRef strGetFiles() As String)\r\n\r\n    Dim strFile As String\r\n    ReDim strGetFiles(0)   ' \u5f37\u5236\u7684\u306b\u521d\u671f\u5316\r\n    \r\n    If Right(strFolder, 1) &lt;&gt; \"\\\" Then\r\n        strFolder = strFolder &amp; \"\\\"\r\n    End If\r\n        \r\n    strFile = Dir(strFolder &amp; \"\u30d5\u30a1\u30a4\u30eb*.xlsm\")\r\n    Do While strFile &lt;&gt; \"\"\r\n        If strGetFiles(0) &lt;&gt; \"\" Then\r\n            ReDim Preserve strGetFiles(UBound(strGetFiles) + 1)\r\n        End If\r\n        strGetFiles(UBound(strGetFiles)) = strFile\r\n        strFile = Dir()\r\n    Loop\r\n\r\nEnd Function<\/pre>\n<h3>\u66f4\u65b0\u3057\u305f\u90e8\u5206<\/h3>\n<pre class=\"show-lang:2 lang:vb decode:true\">strFile = Dir(strFolder &amp; \"\u30d5\u30a1\u30a4\u30eb*.xlsm\")<\/pre>\n<p>&#8220;\u30d5\u30a1\u30a4\u30eb*.xlsm&#8221;\u306f\u3001\u5148\u982d\u306b\u300c\u30d5\u30a1\u30a4\u30eb\u300d\u304c\u3064\u3044\u305f\u30d5\u30a1\u30a4\u30eb\u3092\u5bfe\u8c61\u306b\u5217\u6319\u3057\u307e\u3059\u3002<\/p>\n<p>\u4f8b\u3048\u3070\u3001\u300c\u30d5\u30a1\u30a4\u30eb1.xlsm\u300d\u3001\u300c\u30d5\u30a1\u30a4\u30eb2.xlsm\u300d\u3001\u300c\u30d5\u30a1\u30a4\u30eb\u3059\u3079\u3066.xlsm\u300d\u3067\u3059\u3002\u300c1\u30d5\u30a1\u30a4\u30eb.xlsm\u300d\u306f\u5bfe\u8c61\u306b\u306a\u308a\u307e\u305b\u3093\u3002\u300c1\u30d5\u30a1\u30a4\u30eb.xlsm\u300d\u3082\u5bfe\u8c61\u306b\u3057\u305f\u3044\u5834\u5408\u306f\u3001<\/p>\n<pre class=\"show-lang:2 lang:vb decode:true\">strFile = Dir(strFolder &amp; \"*\u30d5\u30a1\u30a4\u30eb*.xlsm\")<\/pre>\n<p>\u3068\u30d5\u30a1\u30a4\u30eb\u306e\u524d\u306b\u30a2\u30b9\u30bf\u30ea\u30b9\u30af\u3092\u3064\u3051\u308c\u3070OK\u3067\u3059\u3002<\/p>\n<p>\u307e\u305f\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u62e1\u5f35\u5b50\u306b\u30a2\u30b9\u30bf\u30ea\u30b9\u30af\u3092\u4f7f\u3046\u3068\u300cALL\u30d5\u30a1\u30a4\u30eb.xlsx\u300d\u3082\u5217\u6319\u5bfe\u8c61\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre class=\"show-lang:2 lang:vb decode:true \">strFile = Dir(strFolder &amp; \"*\u30d5\u30a1\u30a4\u30eb*.xls*\")<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Dir\u95a2\u6570\u3092\u5229\u7528\u3057\u3001\u9078\u629e\u3057\u305f\u30d5\u30a9\u30eb\u30c0\u306e\u4e2d\u306e\u30d5\u30a1\u30a4\u30eb\u7fa4\u3092\u5217\u6319\u3059\u308b Dir\u95a2\u6570\u3092\u5229\u7528\u3057\u3066\u3001\u30d5\u30a9\u30eb\u30c0\u306e\u4e2d\u306e\u30d5\u30a1\u30a4\u30eb\u7fa4\u3092\u5217\u6319\u3059\u308b\u65b9\u6cd5\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002 \u95a2\u6570\u306b\u3057\u3066\u3044\u307e\u3059\u306e\u3067\u3001\u305d\u306e\u307e\u307e\u30b3\u30d4\u30fc\u3057\u3066\u5229\u7528\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002 Function \u30d5\u30a1\u30a4\u30eb\u3092\u5217\u6319\u3059\u308b(strFolder As String, ByRef strGetFiles() As String) Dim strFile As String ReDi [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":599,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[230],"tags":[238,113,239,237,231],"class_list":["post-635","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-vba","tag-dir","tag-vba","tag-239","tag-237","tag-231"],"_links":{"self":[{"href":"https:\/\/sunafukin.jp\/index.php?rest_route=\/wp\/v2\/posts\/635"}],"collection":[{"href":"https:\/\/sunafukin.jp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sunafukin.jp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sunafukin.jp\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sunafukin.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=635"}],"version-history":[{"count":10,"href":"https:\/\/sunafukin.jp\/index.php?rest_route=\/wp\/v2\/posts\/635\/revisions"}],"predecessor-version":[{"id":645,"href":"https:\/\/sunafukin.jp\/index.php?rest_route=\/wp\/v2\/posts\/635\/revisions\/645"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sunafukin.jp\/index.php?rest_route=\/wp\/v2\/media\/599"}],"wp:attachment":[{"href":"https:\/\/sunafukin.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=635"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sunafukin.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=635"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sunafukin.jp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=635"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}