フリーページ

2024年02月12日
XML
カテゴリ: カテゴリ未分類
Sub FindTopLevelFoldersImproved()
    Dim wsLog As Worksheet, wsSheet2 As Worksheet
    Dim lastRow As Long, lastCol As Long, i As Long, j As Long
    Dim dictFolders As Object, listTopFolders As Object
    Dim userId As Variant, folderPath As Variant, checkPath As Variant
    Dim isTopFolder As Boolean
    Dim row As Long
    Set wsLog = ThisWorkbook.Sheets("log")
    Set wsSheet2 = ThisWorkbook.Sheets("Sheet2")
    Set dictFolders = CreateObject("Scripting.Dictionary")
    lastRow = wsLog.Cells(wsLog.Rows.Count, "A").End(xlUp).Row
    lastCol = wsLog.Cells(1, wsLog.Columns.Count).End(xlToLeft).Column
    ' 全てのフォルダパスを収集
    For i = 2 To lastRow
        For j = 4 To lastCol ' D列から始まるアクセス権者ID
            If wsLog.Cells(i, j).Value = "○" Then
                folderPath = wsLog.Cells(i, 1).Value
                userId = wsLog.Cells(1, j).Value
                If Not dictFolders.Exists(userId) Then
                    Set dictFolders(userId) = CreateObject("Scripting.Dictionary")
                End If
                dictFolders(userId)(folderPath) = True
            End If
        Next j
    Next i
    ' Sheet2をクリア
    wsSheet2.Cells.ClearContents
    wsSheet2.Cells(1, 1).Value = "権利者"
    wsSheet2.Cells(1, 2).Value = "最上層フォルダ"
    row = 2
    ' 最上層フォルダを特定
    For Each userId In dictFolders.Keys
        Set listTopFolders = CreateObject("Scripting.Dictionary")
            isTopFolder = True
            For Each checkPath In dictFolders(userId).Keys
                If folderPath <> checkPath And InStr(1, folderPath & "\", checkPath & "\") > 0 Then
                    isTopFolder = False
                    Exit For
                End If
            Next checkPath
            ' フォルダが最上層であればリストに追加
            If isTopFolder Then
                If Not listTopFolders.Exists(folderPath) Then
                    listTopFolders.Add folderPath, True
                End If
            End If
        Next folderPath
        ' 特定された最上層フォルダをSheet2に出力
        For Each folderPath In listTopFolders.Keys
            wsSheet2.Cells(row, 1).Value = userId
            wsSheet2.Cells(row, 2).Value = folderPath
            row = row + 1
        Next folderPath
    Next userId
    MsgBox "最上層フォルダの特定が完了しました。", vbInformation
End Sub





お気に入りの記事を「いいね!」で応援しよう

最終更新日  2024年02月12日 22時16分58秒コメント(0) | コメントを書く


【毎日開催】
15記事にいいね!で1ポイント
10秒滞在
いいね! -- / --
おめでとうございます!
ミッションを達成しました。
※「ポイントを獲得する」ボタンを押すと広告が表示されます。
x
X

PR

キーワードサーチ

▼キーワード検索

プロフィール

Helio Japan

Helio Japan

お気に入りブログ

毎日がエヴリディ! oyazy44さん
ドラマチックな映画… groomixさん
スッポンの達人 弥右ヱ門さん
アメリカ奮闘記 System of a Downさん
吉川裕一のオースト… 和歌山の風達人 ヒロさんさん

コメント新着

海のくまさん@ チン型取られちゃったw http://onaona.mogmog55.net/f2p8mnr/ 俺…
レレレのお父さん @ ケ、ケ、ケ、K太! K太郎さん、残念でしたね。 また観戦に…
中村k太郎@ こんにちは 自分の名前で検索してきました。 会場で…
楽器男@ パフェの語源 パフェの語源は,パーフェクトっていうの…
レレレのお父さん @ Re:びっくり!(02/23) しろもんさん 26日は川越で五日市さん…

© Rakuten Group, Inc.
Design a Mobile Site
スマートフォン版を閲覧 | PC版を閲覧
Share by: