質問編集履歴

2

調整

2023/01/18 06:42

投稿

mako_0221
mako_0221

スコア87

test CHANGED
File without changes
test CHANGED
@@ -189,17 +189,17 @@
189
189
  Debug.Print eachPath
190
190
  Debug.Print adr ' 検索するとちゃんと存在する
191
191
  Debug.Print GetAttr(adr) = 16 'ここが環境によってfalseになったりする
192
- Debug.Print eachPath = "共益維持費合算"
192
+ Debug.Print eachPath = "合算"
193
193
  Debug.Print "--------------------------"
194
194
 
195
- If eachPath <> "." And eachPath <> ".." And GetAttr(adr) = 16 And eachPath = "共益維持費合算" Then
195
+ If eachPath <> "." And eachPath <> ".." And GetAttr(adr) = 16 And eachPath = "合算" Then
196
- Cells(i, 6).Value = itemsdic
196
+ Cells(i, 6).Value = itemsdic
197
- Set hypLink = ActiveSheet.Cells(i, 7).Hyperlinks.Add( _
197
+ Set hypLink = ActiveSheet.Cells(i, 7).Hyperlinks.Add( _
198
- Anchor:=Cells(i, 7), _
198
+ Anchor:=Cells(i, 7), _
199
- Address:=".\" & YYYYMM & "\" & itemsdic & "\" & eachPath, _
199
+ Address:=".\" & YYYYMM & "\" & itemsdic & "\" & eachPath, _
200
- TextToDisplay:=eachPath)
200
+ TextToDisplay:=eachPath)
201
-
201
+
202
- deepPath = ThisWorkbook.path & "\" & YYYYMM & "\" & itemsdic & "\共益維持費合算"
202
+ deepPath = ThisWorkbook.path & "\" & YYYYMM & "\" & itemsdic & "\合算"
203
203
  n = fso.GetFolder(deepPath).SubFolders.Count
204
204
 
205
205
  i = i + 1
@@ -212,7 +212,7 @@
212
212
  i = i + n
213
213
 
214
214
 
215
- ElseIf eachPath <> "." And eachPath <> ".." And GetAttr(adr) = 16 And eachPath <> "共益維持費合算" Then
215
+ ElseIf eachPath <> "." And eachPath <> ".." And GetAttr(adr) = 16 And eachPath <> "合算" Then
216
216
  Cells(i, 6).Value = itemsdic
217
217
  Set hypLink = ActiveSheet.Cells(i, 7).Hyperlinks.Add( _
218
218
  Anchor:=Cells(i, 7), _
@@ -231,14 +231,14 @@
231
231
 
232
232
  k = 4
233
233
  Do While Cells(k, 7) <> ""
234
- If Cells(k, 7) = "共益維持費合算" Then
234
+ If Cells(k, 7) = "合算" Then
235
- camPath = Dir(ThisWorkbook.path & "\" & YYYYMM & "\" & Cells(k, 6) & "\" & "共益維持費合算\*", vbDirectory)
235
+ camPath = Dir(ThisWorkbook.path & "\" & YYYYMM & "\" & Cells(k, 6) & "\" & "合算\*", vbDirectory)
236
236
  Do While camPath <> ""
237
- camAdr = ThisWorkbook.path & "\" & YYYYMM & "\" & Cells(k, 6) & "\" & "共益維持費合算\" & camPath
237
+ camAdr = ThisWorkbook.path & "\" & YYYYMM & "\" & Cells(k, 6) & "\" & "合算\" & camPath
238
238
  If camPath <> "." And camPath <> ".." And GetAttr(camAdr) = 16 Then
239
239
  Set hypLink = ActiveSheet.Cells(k + 1, 8).Hyperlinks.Add( _
240
240
  Anchor:=Cells(k + 1, 8), _
241
- Address:=".\" & YYYYMM & "\" & Cells(k, 6) & "\" & "共益維持費合算\" & camPath, _
241
+ Address:=".\" & YYYYMM & "\" & Cells(k, 6) & "\" & "合算\" & camPath, _
242
242
  TextToDisplay:=camPath)
243
243
  k = k + 1
244
244
  End If

1

コードの追加

2023/01/18 06:41

投稿

mako_0221
mako_0221

スコア87

test CHANGED
File without changes
test CHANGED
@@ -124,5 +124,133 @@
124
124
  End Sub
125
125
 
126
126
  ```
127
+
128
+ 追加コード
129
+ ```vba
130
+ Sub getFolderStruct()
131
+ Dim i As Long
132
+ Dim j As Long
133
+ Dim YYYYMM As String
134
+ Dim fileName As String
135
+ Dim Dic As Object
136
+ Dim buf As String
137
+ Dim itemsdic As Variant
138
+
139
+ Dim eachPath As String
140
+ Dim deepPath As String
141
+ Dim hypLink As Hyperlink
142
+
143
+ Dim adr As String
144
+ Dim deepAdr As String
145
+
146
+ Dim fso As Object
147
+ Dim n As Long
148
+ Dim m As Integer
149
+
150
+ Set fso = CreateObject("Scripting.FileSystemObject")
151
+
152
+ YYYYMM = Cells(6, 4).Value
153
+
154
+ If YYYYMM = "" Then
155
+ MsgBox "YYYYMMに出力したいフォルダパスを入力してね" & vbCrLf & "「処理を終了します」"
156
+ Exit Sub
157
+ End If
158
+
159
+ If Cells(4, 6).Value <> "" Then
160
+ MsgBox "DebugAreaをクリーンにしてください" & vbCrLf & "「処理を終了します」"
161
+ Exit Sub
162
+ End If
163
+
164
+ fileName = Dir(ThisWorkbook.path & "\" & YYYYMM & "\*", vbDirectory)
165
+
166
+
167
+ Set Dic = CreateObject("Scripting.Dictionary")
168
+
169
+ Do While fileName <> ""
170
+ If fileName <> "." And fileName <> ".." Then
171
+ Dic.Add fileName, fileName
172
+ Debug.Print fileName
173
+ End If
174
+ fileName = Dir()
175
+ Loop
176
+
177
+ i = 4
178
+ For Each itemsdic In Dic
179
+
180
+
181
+ eachPath = Dir(ThisWorkbook.path & "\" & YYYYMM & "\" & itemsdic & "\*", vbDirectory)
182
+ Do While eachPath <> ""
183
+ adr = ThisWorkbook.path & "\" & YYYYMM & "\" & itemsdic & "\" & eachPath
184
+
185
+ Debug.Print ThisWorkbook.path & "\" & YYYYMM & "\" & itemsdic & "\*"
186
+ Debug.Print itemsdic
187
+ Debug.Print eachPath <> "."
188
+ Debug.Print eachPath <> ".."
189
+ Debug.Print eachPath
190
+ Debug.Print adr ' 検索するとちゃんと存在する
191
+ Debug.Print GetAttr(adr) = 16 'ここが環境によってfalseになったりする
192
+ Debug.Print eachPath = "共益維持費合算"
193
+ Debug.Print "--------------------------"
194
+
195
+ If eachPath <> "." And eachPath <> ".." And GetAttr(adr) = 16 And eachPath = "共益維持費合算" Then
196
+ Cells(i, 6).Value = itemsdic
197
+ Set hypLink = ActiveSheet.Cells(i, 7).Hyperlinks.Add( _
198
+ Anchor:=Cells(i, 7), _
199
+ Address:=".\" & YYYYMM & "\" & itemsdic & "\" & eachPath, _
200
+ TextToDisplay:=eachPath)
201
+
202
+ deepPath = ThisWorkbook.path & "\" & YYYYMM & "\" & itemsdic & "\共益維持費合算"
203
+ n = fso.GetFolder(deepPath).SubFolders.Count
204
+
205
+ i = i + 1
206
+ m = i + n
207
+ For j = i To m
208
+ Cells(j, 6).Value = itemsdic
209
+ Cells(j, 7).Value = ">"
210
+ Next
211
+
212
+ i = i + n
213
+
214
+
215
+ ElseIf eachPath <> "." And eachPath <> ".." And GetAttr(adr) = 16 And eachPath <> "共益維持費合算" Then
216
+ Cells(i, 6).Value = itemsdic
217
+ Set hypLink = ActiveSheet.Cells(i, 7).Hyperlinks.Add( _
218
+ Anchor:=Cells(i, 7), _
219
+ Address:=".\" & YYYYMM & "\" & itemsdic & "\" & eachPath, _
220
+ TextToDisplay:=eachPath)
221
+
222
+ i = i + 1
223
+ End If
224
+ eachPath = Dir()
225
+ Loop
226
+ Next
227
+
228
+ Dim k As Long
229
+ Dim camPath As String
230
+ Dim camAdr As String
231
+
232
+ k = 4
233
+ Do While Cells(k, 7) <> ""
234
+ If Cells(k, 7) = "共益維持費合算" Then
235
+ camPath = Dir(ThisWorkbook.path & "\" & YYYYMM & "\" & Cells(k, 6) & "\" & "共益維持費合算\*", vbDirectory)
236
+ Do While camPath <> ""
237
+ camAdr = ThisWorkbook.path & "\" & YYYYMM & "\" & Cells(k, 6) & "\" & "共益維持費合算\" & camPath
238
+ If camPath <> "." And camPath <> ".." And GetAttr(camAdr) = 16 Then
239
+ Set hypLink = ActiveSheet.Cells(k + 1, 8).Hyperlinks.Add( _
240
+ Anchor:=Cells(k + 1, 8), _
241
+ Address:=".\" & YYYYMM & "\" & Cells(k, 6) & "\" & "共益維持費合算\" & camPath, _
242
+ TextToDisplay:=camPath)
243
+ k = k + 1
244
+ End If
245
+ camPath = Dir()
246
+ Loop
247
+ Else
248
+ k = k + 1
249
+ End If
250
+ k = k + 1 'ここは必要であることが判明
251
+ Loop
252
+
253
+ End Sub
254
+ ```
127
255
  ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2023-01-18/0f315742-4103-415e-bb02-ef806e85cc91.png)
128
256