Anh chị giúp em cách đọc tên cây thư mục và cập nhật tên khi thay đổi vào bảng excel với ạ

Liên hệ QC
Tôi tuân thủ nội quy khi đăng bài

hoctapphotoshop

Thành viên mới
Tham gia
4/4/18
Bài viết
29
Được thích
3
Giới tính
Nam
1688787820453.png
Em có cây thư mục như trên hình, trong các thư mục lại có thư mục con; anh chị giúp em
- Đọc được tên thư mục vào bảng excel;
- Cập nhật được khi tên thư mục thay đổi;
- Có đường link để mở được thư mục tương ứng.
1688788102369.png
em xin cám ơn ạ!
 

File đính kèm

  • doc ten thu muc.xlsm
    10.7 KB · Đọc: 4
Dùng tính năng Get Data→From File→From Folder của Power Query em nghĩ có thể giúp được đấy.
1688796062824.png
 
Upvote 0
Upvote 0
Phần này lấy tên file trong thư mục nên khi thư mục chứa nhiều file nhỏ cập nhật rất lâu,
Nghe câu này hình như anh cũng biết dùng Power Query rồi phải không, vậy thêm mấy bước sau nhé.
Get Data→From File→From Folder→Chọn thư mục mẹ→TransformData→Chuột phải vào cột Folder Path chọn Remove other Columns→Chọn chuột phải lần nữa chọn Remove Duplicate→Close and load.
1689056042479.png1689056075952.png
 
Upvote 0
View attachment 292562
Em có cây thư mục như trên hình, trong các thư mục lại có thư mục con; anh chị giúp em
- Đọc được tên thư mục vào bảng excel;
- Cập nhật được khi tên thư mục thay đổi;
- Có đường link để mở được thư mục tương ứng.
View attachment 292564
em xin cám ơn ạ!
Nếu nó có thư mục cấp 3 cấp 4, cấp ... thì sao.
Mã:
Option Explicit
Private oSh As Worksheet
Private xRow As Long, aCount As Byte
Sub GPE()
    Dim sFolder As String, aRow As Integer
    Dim Root_Folder_Path As String, fso As Object, xFolder As Object
    Dim iSh As Worksheet
    Set oSh = ThisWorkbook.Sheets(1)
    aRow = oSh.Range("C1000").End(xlUp).Row
    If aRow > 1 Then oSh.Range("A2:D" & aRow).Clear
    sFolder = ThisWorkbook.Path
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set xFolder = fso.getFolder(sFolder)
    xRow = 2: aCount = 1
    List_Subfolders xFolder, aCount
    aRow = oSh.Range("C1000").End(xlUp).Row
    If aRow > 1 Then oSh.Range("A2:D" & aRow).Borders.LineStyle = xlContinuous
    oSh.Columns.AutoFit
    Set xFolder = Nothing
    Set fso = Nothing
    MsgBox "Hoan thanh"
End Sub
Sub List_Subfolders(ByRef objFolder As Object, CheckSub As Byte)
    Dim SubFolder As Object
    If CheckSub < 3 Then
        While oSh.Cells(xRow, 1) <> ""
            xRow = xRow + 1
        Wend
        If CheckSub = 2 Then xRow = xRow - 1
        For Each SubFolder In objFolder.SubFolders
            oSh.Cells(xRow, CheckSub + 1).Value = "'" & SubFolder.Name
            If CheckSub = 2 Then
                oSh.Cells(xRow, CheckSub + 2).Value = SubFolder.Path & "\"
                oSh.Hyperlinks.Add Anchor:=oSh.Cells(xRow, CheckSub + 2), Address:=SubFolder.Path & "\", TextToDisplay:=SubFolder.Path & "\"
            End If
            xRow = xRow + 1
            List_Subfolders SubFolder, CheckSub + 1
        Next SubFolder
    End If
End Sub
 
Upvote 0
Nghe câu này hình như anh cũng biết dùng Power Query rồi phải không, vậy thêm mấy bước sau nhé.
Get Data→From File→From Folder→Chọn thư mục mẹ→TransformData→Chuột phải vào cột Folder Path chọn Remove other Columns→Chọn chuột phải lần nữa chọn Remove Duplicate→Close and load.
Nghe câu này hình như anh cũng biết dùng Power Query rồi phải không, vậy thêm mấy bước sau nhé.
Get Data→From File→From Folder→Chọn thư mục mẹ→TransformData→Chuột phải vào cột Folder Path chọn Remove other Columns→Chọn chuột phải lần nữa chọn Remove Duplicate→Close and load.
View attachment 292632View attachment 292633
cám ơn bạn đã hướng dẫn, nhưng folder của mình có rất nhiều file nhỏ. nên quá trình transform Data lâu và bị đơ nên không áp dụng được


Bài đã được tự động gộp:

Nếu nó có thư mục cấp 3 cấp 4, cấp ... thì sao.
Mã:
Option Explicit
Private oSh As Worksheet
Private xRow As Long, aCount As Byte
Sub GPE()
    Dim sFolder As String, aRow As Integer
    Dim Root_Folder_Path As String, fso As Object, xFolder As Object
    Dim iSh As Worksheet
    Set oSh = ThisWorkbook.Sheets(1)
    aRow = oSh.Range("C1000").End(xlUp).Row
    If aRow > 1 Then oSh.Range("A2:D" & aRow).Clear
    sFolder = ThisWorkbook.Path
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set xFolder = fso.getFolder(sFolder)
    xRow = 2: aCount = 1
    List_Subfolders xFolder, aCount
    aRow = oSh.Range("C1000").End(xlUp).Row
    If aRow > 1 Then oSh.Range("A2:D" & aRow).Borders.LineStyle = xlContinuous
    oSh.Columns.AutoFit
    Set xFolder = Nothing
    Set fso = Nothing
    MsgBox "Hoan thanh"
End Sub
Sub List_Subfolders(ByRef objFolder As Object, CheckSub As Byte)
    Dim SubFolder As Object
    If CheckSub < 3 Then
        While oSh.Cells(xRow, 1) <> ""
            xRow = xRow + 1
        Wend
        If CheckSub = 2 Then xRow = xRow - 1
        For Each SubFolder In objFolder.SubFolders
            oSh.Cells(xRow, CheckSub + 1).Value = "'" & SubFolder.Name
            If CheckSub = 2 Then
                oSh.Cells(xRow, CheckSub + 2).Value = SubFolder.Path & "\"
                oSh.Hyperlinks.Add Anchor:=oSh.Cells(xRow, CheckSub + 2), Address:=SubFolder.Path & "\", TextToDisplay:=SubFolder.Path & "\"
            End If
            xRow = xRow + 1
            List_Subfolders SubFolder, CheckSub + 1
        Next SubFolder
    End If
End Sub
dạ folder của em thường có 2 cấp; đoạn mã trên đã giải quyết được vấn đề ạ. Nếu nhiều cấp folder em tìm cách tìm max cấp folder trước rồi phân cấp sau ạ (max cũng không quá 6 cấp folder ạ)
em xin cám ơn!
 
Lần chỉnh sửa cuối:
Upvote 0
Web KT
Back
Top Bottom