0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

tree /fコマンドをExcelに張り付けてハイパーリンク付与

0
Last updated at Posted at 2026-01-26
'========================
' 階層判定(修正版)
'========================
Private Function GetFolderLevel(ByVal line As String, ByVal unit As Long) As Long
    Dim p As Long
    p = GetFolderMarkerPos(line)
    If p = 0 Then Exit Function

    'prefixLen = marker の直前までの長さ
    Dim prefixLen As Long
    prefixLen = p - 1

    '切り上げでブロック数を出す(割り切れない行でも 1段浅くならない)
    GetFolderLevel = ((prefixLen + unit - 1) \ unit) + 1
End Function

Private Function GetFileLevel(ByVal line As String, ByVal unit As Long) As Long
    Dim p As Long
    p = InStrRev(line, "│")
    If p = 0 Then p = InStrRev(line, "|")
    If p = 0 Then Exit Function

    Dim prefixLen As Long
    prefixLen = p - 1

    GetFileLevel = ((prefixLen + unit - 1) \ unit) + 1
End Function

Private Function GetFolderMarkerPos(ByVal line As String) As Long
    Dim p As Long
    p = InStr(line, "├─")
    If p = 0 Then p = InStr(line, "└─")
    If p = 0 Then p = InStr(line, "+---")
    If p = 0 Then p = InStr(line, "\---")
    GetFolderMarkerPos = p
End Function

Private Function DetermineIndentUnit(ByVal ws As Worksheet, ByVal col As Long, ByVal startRow As Long, ByVal lastRow As Long) As Long
    Dim r As Long, s As String
    Dim u As Long: u = 0

    For r = startRow To lastRow
        s = CStr(ws.Cells(r, col).Value2)
        If Len(Trim$(s)) = 0 Then GoTo NextR

        'フォルダ行:marker 位置から prefixLen を集める
        Dim p As Long
        p = GetFolderMarkerPos(s)
        If p > 1 Then
            u = GcdNonZero(u, p - 1)
        End If

        'ファイル行:最後の │ の位置から prefixLen を集める
        Dim q As Long
        q = InStrRev(s, "│")
        If q = 0 Then q = InStrRev(s, "|")
        If q > 1 Then
            u = GcdNonZero(u, q - 1)
        End If

NextR:
    Next r

    '保険:変な値になったら 4 を採用
    If u < 2 Or u > 8 Then u = 4
    DetermineIndentUnit = u
End Function

Private Function GcdNonZero(ByVal a As Long, ByVal b As Long) As Long
    If a = 0 Then
        GcdNonZero = Abs(b)
    ElseIf b = 0 Then
        GcdNonZero = Abs(a)
    Else
        GcdNonZero = Gcd(Abs(a), Abs(b))
    End If
End Function

Private Function Gcd(ByVal a As Long, ByVal b As Long) As Long
    Do While b <> 0
        Dim t As Long
        t = a Mod b
        a = b
        b = t
    Loop
    Gcd = a
End Function

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?