Sub FindTopFolderForUser() Dim ws As Worksheet Set ws = ThisWorkbook.Sheets("アクセス権情報") ' シート名を指定
Dim lastRow As Long lastRow = ws.Cells(ws.Rows.Count, 4).End(xlUp).Row ' D列以降にIDが含まれていると仮定
Dim folderColumn As Integer folderColumn = 1 ' フォルダのパスが含まれている列(A列を示す場合)
Dim r As Long Dim folderFound As Boolean
For r = 3 To lastRow ' 開始行を適切な行に変更してください If ws.Cells(r, 23).Value = "○" Then ' W列に権限のマークがあると仮定 ' 権限がある最上位のフォルダを取得する関数を呼び出して、結果を表示 Dim topFolder As String topFolder = GetTopFolder(ws.Cells(r, folderColumn).Value) If topFolder <> "" Then MsgBox "権限がある最上位のフォルダ: " & topFolder folderFound = True Exit For End If End If
If Not folderFound Then MsgBox "指定されたユーザーに権限があるフォルダが見つかりませんでした。" End If End Sub
Function GetTopFolder(folderPath As String) As String
Dim folders() As String folders = Split(folderPath, "\") GetTopFolder = folders(UBound(folders) - 1) End Function