親フォルダ一覧がF列
子フォルダ一覧がG列のデータがあります。
最上位のフォルダは
ルートという名前で固定です。
ヘッダは1列目がヘッダになります。
2列目からデータになります。
J列にフルパスを自動でマクロで入れたいです。
シート名は:マクロ
頭がこんがらがってしまい、中々上手く行きません。
分かり易い、良い方法はないでしょうか?
Sub pathFolder()
Dim mcbook As Workbook Set mcbook = ThisWorkbook Const oya As String = "ルート" Dim stock() As String MaxRow = Cells(Rows.Count, 1).End(xlUp).Row MaxCol = Cells(1, Columns.Count).End(xlToLeft).Column ReDim Preserve stock(MaxRow) For i = 2 To MaxRow stock(i) = concatenationFolder(i) Next i For i = 2 To MaxRow Call concatenationFolder2(i, stock) Next i
MsgBox "終了"
End Sub
Function concatenationFolder(ByVal i) As String
Dim childFolder As String childFolder = "" Dim oyaFolder As String oyaFolder = "" Dim mcbook As Workbook Set mcbook = ThisWorkbook Dim fullPath As String fullPath = "" childFolder = mcbook.Worksheets("マクロ").Cells(i, "G") oyaFolder = mcbook.Worksheets("マクロ").Cells(i, "F") If childFolder <> "ルート" Then fullPath = "\" & oyaFolder & "\" & childFolder Cells(i, "J") = fullPath Else fullPath = "ルート" Cells(i, "J") = "ルート" End If concatenationFolder = mcbook.Worksheets("マクロ").Cells(i, "J")
End Function
Function concatenationFolder2(ByVal i, ByVal stock)
Dim childFolder As String childFolder = "" Dim oyaFolder As String oyaFolder = "" Dim mcbook As Workbook Set mcbook = ThisWorkbook Dim fullPath As String fullPath = "" MsgBox stock(i) Dim root As String root = Left(stock(i), InStr(2, stock(i), "\")) root = Replace(root, "\", "") MsgBox root Dim j As Integer j = 0 MaxRow = Cells(Rows.Count, 1).End(xlUp).Row MaxCol = Cells(1, Columns.Count).End(xlToLeft).Column If root <> "" Then For j = i To MaxRow childFolder = mcbook.Worksheets("マクロ").Cells(j, "G") If root = childFolder Then oyaFolder = mcbook.Worksheets("マクロ").Cells(j, "F") fullPath = "\" & oyaFolder & "\" & fullPath Cells(i, "J") = fullPath End If Next j End If
End Function
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。