Lấy danh sách tên từ file trong folder sang excel

Liên hệ QC

gaudenhungbao

Thành viên mới
Tham gia
16/7/13
Bài viết
1
Được thích
0
Chào các bạn,
Mình đang tìm cách lấy danh sách tên các file word trong một folder sang excel để dễ quản lý và theo dõi. Nhưng mình không rành về excel nên không biết cách làm như thế nào? Nhà mình có ai biết cách chỉ giúp mình với. Mình cảm ơn rất nhiều.}}}}}
 
Chào các bạn,
Mình đang tìm cách lấy danh sách tên các file word trong một folder sang excel để dễ quản lý và theo dõi. Nhưng mình không rành về excel nên không biết cách làm như thế nào? Nhà mình có ai biết cách chỉ giúp mình với. Mình cảm ơn rất nhiều.}}}}}

Thử sử dụng File tại bài #2, Topic sau:

http://www.giaiphapexcel.com/forum/...-thời-gian-tạo-hiệu-chỉnh&p=418404#post418404
 
Chào các bạn,
Mình đang tìm cách lấy danh sách tên các file word trong một folder sang excel để dễ quản lý và theo dõi. Nhưng mình không rành về excel nên không biết cách làm như thế nào? Nhà mình có ai biết cách chỉ giúp mình với. Mình cảm ơn rất nhiều.}}}}}
Copy file này bỏ vào chung thư mục mà bạn muốn lấy tên các file Word. Sau đó nhấp vào cái nút trên bảng tính. Phải Enable Macros.
 

File đính kèm

  • Lay ten file trong folder.xlsm
    19.3 KB · Đọc: 315
Copy file này bỏ vào chung thư mục mà bạn muốn lấy tên các file Word. Sau đó nhấp vào cái nút trên bảng tính. Phải Enable Macros.

Chọt cái coi. Chỗ này:
Mã:
If ObjFile.Name <> ThisWorkbook.Name Then
hình như hơi... thừa. File .doc thì làm sao có thể giống ThisWorkbook.Name nhỉ?
Ẹc... Ẹc...
 
Copy file này bỏ vào chung thư mục mà bạn muốn lấy tên các file Word. Sau đó nhấp vào cái nút trên bảng tính. Phải Enable Macros.
Vậy nếu muốn lấy thêm file xls* và pdf thì sao bạn? Và lấy luôn những thứ có trong folder con của nó thì sao?
Thanks so much
 
Lần chỉnh sửa cuối:
Vậy nếu muốn lấy thêm file xls* và pdf thì sao bạn? Và lấy luôn những thứ có trong folder con của nó thì sao?
Thanks so much
Tìm trong các bài viết của mình. Hình như cách đây 3 ngày mình có viết 1 code lấy file xls*, doc và pdf
 
Đây là code của bạn mình copy lại nha. Nhưng với code này nó lấy tất tần tật các file luôn, file thumb nó cũng lấy luôn. Có cách nào bỏ không lấy file đó không? Hoặc lấy theo tiêu chí mình đặt ra vd như file xls*, doc*, pdf, ppt*,... Thanks so much
Mã:
[COLOR=#0000BB][FONT=monospace]Sub Main[/FONT][/COLOR][COLOR=#007700][FONT=monospace]()
[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Dim fso [/FONT][/COLOR][COLOR=#007700][FONT=monospace]As [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Object[/FONT][/COLOR][COLOR=#007700][FONT=monospace], [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]res[/FONT][/COLOR][COLOR=#007700][FONT=monospace]() As [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]String
    Set fso [/FONT][/COLOR][COLOR=#007700][FONT=monospace]= [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]CreateObject[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#DD0000][FONT=monospace]"Scripting.FileSystemObject"[/FONT][/COLOR][COLOR=#007700][FONT=monospace])
    [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]With Application[/FONT][/COLOR][COLOR=#007700][FONT=monospace].[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]FileDialog[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]msoFileDialogFolderPicker[/FONT][/COLOR][COLOR=#007700][FONT=monospace])
        If .[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Show Then
            ReDim res[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]1 To 1[/FONT][/COLOR][COLOR=#007700][FONT=monospace])
            [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]GetAllFiles [/FONT][/COLOR][COLOR=#007700][FONT=monospace].[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]SelectedItems[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]1[/FONT][/COLOR][COLOR=#007700][FONT=monospace]), [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]fso[/FONT][/COLOR][COLOR=#007700][FONT=monospace], [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]res
            [/FONT][/COLOR][COLOR=#007700][FONT=monospace]If [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]UBound[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]res[/FONT][/COLOR][COLOR=#007700][FONT=monospace]) > [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]1 Then [/FONT][/COLOR][COLOR=#007700][FONT=monospace][[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]A5[/FONT][/COLOR][COLOR=#007700][FONT=monospace]].[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Resize[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]UBound[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]res[/FONT][/COLOR][COLOR=#007700][FONT=monospace]) - [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]1[/FONT][/COLOR][COLOR=#007700][FONT=monospace]) = [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Application[/FONT][/COLOR][COLOR=#007700][FONT=monospace].[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Transpose[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]res[/FONT][/COLOR][COLOR=#007700][FONT=monospace])
        [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]End [/FONT][/COLOR][COLOR=#007700][FONT=monospace]If
    [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]End With
End Sub
[/FONT][/COLOR][COLOR=#007700][FONT=monospace]Function [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]GetAllFiles[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]ByVal StrFolder [/FONT][/COLOR][COLOR=#007700][FONT=monospace]As [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]String[/FONT][/COLOR][COLOR=#007700][FONT=monospace], [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]fso [/FONT][/COLOR][COLOR=#007700][FONT=monospace]As [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Object[/FONT][/COLOR][COLOR=#007700][FONT=monospace], [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]res[/FONT][/COLOR][COLOR=#007700][FONT=monospace]() As [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]String[/FONT][/COLOR][COLOR=#007700][FONT=monospace])
[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Dim objFolder [/FONT][/COLOR][COLOR=#007700][FONT=monospace]As [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Object[/FONT][/COLOR][COLOR=#007700][FONT=monospace], [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]objSubFolder [/FONT][/COLOR][COLOR=#007700][FONT=monospace]As [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Object[/FONT][/COLOR][COLOR=#007700][FONT=monospace], [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]File
    Set objFolder [/FONT][/COLOR][COLOR=#007700][FONT=monospace]= [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]fso[/FONT][/COLOR][COLOR=#007700][FONT=monospace].[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]GetFolder[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]StrFolder[/FONT][/COLOR][COLOR=#007700][FONT=monospace])
    For [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Each File In objFolder[/FONT][/COLOR][COLOR=#007700][FONT=monospace].[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Files
        res[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]UBound[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]res[/FONT][/COLOR][COLOR=#007700][FONT=monospace])) = [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]fso[/FONT][/COLOR][COLOR=#007700][FONT=monospace].[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]GetBaseName[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]File[/FONT][/COLOR][COLOR=#007700][FONT=monospace])
        [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]ReDim Preserve res[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]1 To UBound[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]res[/FONT][/COLOR][COLOR=#007700][FONT=monospace])   [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]1[/FONT][/COLOR][COLOR=#007700][FONT=monospace])
    [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Next
    [/FONT][/COLOR][COLOR=#007700][FONT=monospace]For [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Each objSubFolder In objFolder[/FONT][/COLOR][COLOR=#007700][FONT=monospace].[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]SubFolders
        GetAllFiles objSubFolder[/FONT][/COLOR][COLOR=#007700][FONT=monospace].[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Path[/FONT][/COLOR][COLOR=#007700][FONT=monospace], [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]fso[/FONT][/COLOR][COLOR=#007700][FONT=monospace], [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]res
    Next objSubFolder
End [/FONT][/COLOR][COLOR=#007700][FONT=monospace]Function  [/FONT][/COLOR]
 
Lần chỉnh sửa cuối:
Đây là code của bạn mình copy lại nha. Nhưng với code này nó lấy tất tần tật các file luôn, file thumb nó cũng lấy luôn. Có cách nào bỏ không lấy file đó không? Hoặc lấy theo tiêu chí mình đặt ra vd như file xls*, doc*, pdf, ppt*,... Thanks so much
Mã:
[COLOR=#0000BB][FONT=monospace]Sub Main[/FONT][/COLOR][COLOR=#007700][FONT=monospace]()
[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Dim fso [/FONT][/COLOR][COLOR=#007700][FONT=monospace]As [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Object[/FONT][/COLOR][COLOR=#007700][FONT=monospace], [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]res[/FONT][/COLOR][COLOR=#007700][FONT=monospace]() As [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]String
    Set fso [/FONT][/COLOR][COLOR=#007700][FONT=monospace]= [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]CreateObject[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#DD0000][FONT=monospace]"Scripting.FileSystemObject"[/FONT][/COLOR][COLOR=#007700][FONT=monospace])
    [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]With Application[/FONT][/COLOR][COLOR=#007700][FONT=monospace].[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]FileDialog[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]msoFileDialogFolderPicker[/FONT][/COLOR][COLOR=#007700][FONT=monospace])
        If .[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Show Then
            ReDim res[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]1 To 1[/FONT][/COLOR][COLOR=#007700][FONT=monospace])
            [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]GetAllFiles [/FONT][/COLOR][COLOR=#007700][FONT=monospace].[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]SelectedItems[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]1[/FONT][/COLOR][COLOR=#007700][FONT=monospace]), [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]fso[/FONT][/COLOR][COLOR=#007700][FONT=monospace], [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]res
            [/FONT][/COLOR][COLOR=#007700][FONT=monospace]If [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]UBound[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]res[/FONT][/COLOR][COLOR=#007700][FONT=monospace]) > [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]1 Then [/FONT][/COLOR][COLOR=#007700][FONT=monospace][[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]A5[/FONT][/COLOR][COLOR=#007700][FONT=monospace]].[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Resize[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]UBound[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]res[/FONT][/COLOR][COLOR=#007700][FONT=monospace]) - [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]1[/FONT][/COLOR][COLOR=#007700][FONT=monospace]) = [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Application[/FONT][/COLOR][COLOR=#007700][FONT=monospace].[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Transpose[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]res[/FONT][/COLOR][COLOR=#007700][FONT=monospace])
        [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]End [/FONT][/COLOR][COLOR=#007700][FONT=monospace]If
    [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]End With
End Sub
[/FONT][/COLOR][COLOR=#007700][FONT=monospace]Function [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]GetAllFiles[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]ByVal StrFolder [/FONT][/COLOR][COLOR=#007700][FONT=monospace]As [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]String[/FONT][/COLOR][COLOR=#007700][FONT=monospace], [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]fso [/FONT][/COLOR][COLOR=#007700][FONT=monospace]As [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Object[/FONT][/COLOR][COLOR=#007700][FONT=monospace], [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]res[/FONT][/COLOR][COLOR=#007700][FONT=monospace]() As [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]String[/FONT][/COLOR][COLOR=#007700][FONT=monospace])
[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Dim objFolder [/FONT][/COLOR][COLOR=#007700][FONT=monospace]As [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Object[/FONT][/COLOR][COLOR=#007700][FONT=monospace], [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]objSubFolder [/FONT][/COLOR][COLOR=#007700][FONT=monospace]As [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Object[/FONT][/COLOR][COLOR=#007700][FONT=monospace], [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]File
    Set objFolder [/FONT][/COLOR][COLOR=#007700][FONT=monospace]= [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]fso[/FONT][/COLOR][COLOR=#007700][FONT=monospace].[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]GetFolder[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]StrFolder[/FONT][/COLOR][COLOR=#007700][FONT=monospace])
    For [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Each File In objFolder[/FONT][/COLOR][COLOR=#007700][FONT=monospace].[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Files
        res[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]UBound[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]res[/FONT][/COLOR][COLOR=#007700][FONT=monospace])) = [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]fso[/FONT][/COLOR][COLOR=#007700][FONT=monospace].[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]GetBaseName[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]File[/FONT][/COLOR][COLOR=#007700][FONT=monospace])
        [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]ReDim Preserve res[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]1 To UBound[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]res[/FONT][/COLOR][COLOR=#007700][FONT=monospace]) + [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]1[/FONT][/COLOR][COLOR=#007700][FONT=monospace])
    [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Next
    [/FONT][/COLOR][COLOR=#007700][FONT=monospace]For [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Each objSubFolder In objFolder[/FONT][/COLOR][COLOR=#007700][FONT=monospace].[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]SubFolders
        GetAllFiles objSubFolder[/FONT][/COLOR][COLOR=#007700][FONT=monospace].[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Path[/FONT][/COLOR][COLOR=#007700][FONT=monospace], [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]fso[/FONT][/COLOR][COLOR=#007700][FONT=monospace], [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]res
    Next objSubFolder
End [/FONT][/COLOR][COLOR=#007700][FONT=monospace]Function  [/FONT][/COLOR]
Bài số 10 trong link này
http://www.giaiphapexcel.com/forum/...ỗi-Input-past-end-of-file&p=600838#post600838
 
Lần chỉnh sửa cuối:
Sao mình copy 2 code của bạn vào trong file excel phần module chạy sub main thì nó kêu chọn đường dẫn xong chẳng thấy kết quả gì cả vậy? Bạn hướng dẫn mình kỹ chút được không? Nếu được cho mình file mẫu để xem nhé. Cảm ơn nhiều
PHP:
Sub Main()
On Error Resume Next
Dim i As Long, path As String, Sarr, Res(), insub As Boolean, tam As String
insub = 1
With Application.FileDialog(msoFileDialogFolderPicker)
   If .Show Then
      path = .SelectedItems(1)
      If Right(path, 1) <> "\" Then path = path & "\"
      tam = path
      path = """" & path & """"
      path = path & "*.xls " & path & "*.doc " & path & "*.pdf "
      Sarr = GetAllFile(path, insub)
      ReDim Res(1 To UBound(Sarr), 1 To 1)
      For i = LBound(Sarr) To UBound(Sarr) - 1
         If Not insub Then
            Res(i + 1, 1) = tam & Sarr(i)
         Else
            Res(i + 1, 1) = Sarr(i)
         End If
      Next
      [A1].Resize(i) = Res
   End If
End With
End Sub

PHP:
Function GetAllFile(ByVal StrFolder As String, insub As Boolean)
On Error Resume Next
Dim sComm As String, tmpFile As String, Res
   With CreateObject("Scripting.FileSystemObject")
      tmpFile = "D:\" & .GetTempName
      sComm = "DIR " & StrFolder & " /B/O " _
      & IIf(insub, "/S", vbNullString) & " > " & tmpFile
      CreateObject("Wscript.Shell").Run "cmd /u/c " & sComm, 0, 1
      GetAllFile = Split(.OpenTextFile(tmpFile, 1, , -1).ReadAll, vbCrLf)
      Kill tmpFile
   End With
End Function
 
Đây là code của bạn mình copy lại nha. Nhưng với code này nó lấy tất tần tật các file luôn, file thumb nó cũng lấy luôn. Có cách nào bỏ không lấy file đó không? Hoặc lấy theo tiêu chí mình đặt ra vd như file xls*, doc*, pdf, ppt*,... Thanks so much

Tặng bạn chương trình này. Excel 2003, 2007 hay 2010 đều dùng được
Khởi động file xong, bạn sẽ thấy có 1 toolbar, trong đó có nút "Search for files and folders", bấm vào và tự nghiên cứu nhé (nếu cần thì xem video hướng dẫn để biết chi tiết cách dùng)
File này có thể save thành addin để dùng chung
 

File đính kèm

  • Search_For_Files_Folders.xls
    62 KB · Đọc: 242
  • Search_For_Files_Folders.xlsm
    28.7 KB · Đọc: 145
  • Video.rar
    2 MB · Đọc: 216
Tặng bạn chương trình này. Excel 2003, 2007 hay 2010 đều dùng được
Khởi động file xong, bạn sẽ thấy có 1 toolbar, trong đó có nút "Search for files and folders", bấm vào và tự nghiên cứu nhé (nếu cần thì xem video hướng dẫn để biết chi tiết cách dùng)
File này có thể save thành addin để dùng chung
File của anh Ndu quá hay, code chạy rất nhanh, nhưng trong đây mình muốn thêm cái code chỉ lấy tên file thôi được không anh?
Cảm ơn anh nhiều
 
Lần chỉnh sửa cuối:
File của anh Ndu quá hay, code chạy rất nhanh, nhưng trong đây mình muốn thêm cái code chỉ lấy tên file thôi được không anh?
Cảm ơn anh nhiều

Trong file đang có hàm:
Mã:
Function FilesFoldersList(ByVal RootFolder As String, ByVal ListType As Boolean, _
                          ByVal Search As String, ByVal InSub As Boolean)
  'ListType = True: Get Files list
  'ListType = False: Get Folders list
  Dim sComm As String, tmp As String, str As String, tmpFile, Arr
  On Error Resume Next
  If Right(RootFolder, 1) <> "\" Then RootFolder = RootFolder & "\"
  str = """" & RootFolder & IIf(ListType, Search, "") & """"
  
  With CreateObject("Scripting.FileSystemObject")
    tmpFile = .GetTempName
    sComm = "DIR " & str & " /ON /B /A" & IIf(ListType, "-", "") & "D-S" & IIf(InSub, "/S", " ") & " >" & tmpFile
    CreateObject("Wscript.Shell").Run "cmd /u /c " & sComm, 0, True
    With .OpenTextFile(tmpFile, 1, , -2)
      tmp = Trim(.ReadAll)
      If Right(tmp, 2) = vbCrLf Then tmp = Left(tmp, Len(tmp) - 2)
      If Len(tmp) Then
        If InSub = False Then tmp = RootFolder & Replace(tmp, vbCrLf, vbCrLf & RootFolder)
        FilesFoldersList = Split(tmp, vbCrLf)
      End If
      .Close
    End With
  End With
  Kill tmpFile
End Function
Bạn cứ lấy ra mà dùng, tên FILE hay FOLDER đều lấy được tuốt
Ví dụ muốn lấy tên file có đuôi .DOC trong thư mục D:\Test, không bao gồm thư mục con, bạn viết theo cú pháp
arr = FilesFoldersList("D:\Test",True, "*.doc",False)
Với arr là nơi chứa kết quả trả về, nó là một mảng mà các phần tử là tất cả các tên file bạn cần
----------------------
Giải pháp toàn diện, dùng cho nhiều trường hợp khác nhau, khỏi mất công mỗi yêu cầu lại viết riêng một code
 
Trong file đang có hàm:
Mã:
Function FilesFoldersList(ByVal RootFolder As String, ByVal ListType As Boolean, _
                          ByVal Search As String, ByVal InSub As Boolean)
  'ListType = True: Get Files list
  'ListType = False: Get Folders list
  Dim sComm As String, tmp As String, str As String, tmpFile, Arr
  On Error Resume Next
  If Right(RootFolder, 1)  "\" Then RootFolder = RootFolder
 
Lần chỉnh sửa cuối:
File của anh Ndu quá hay, code chạy rất nhanh, nhưng trong đây mình muốn thêm cái code chỉ lấy tên file thôi được không anh?
Cảm ơn anh nhiều
Nếu không cần tốc độ cực nhanh thì xài code này sẽ đơn giản khi muốn tuỳ biến lấy loại file nào
PHP:
Sub Main()
Dim fso As Object, Dic As Object
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set Dic = CreateObject("scripting.dictionary")
    With Application.FileDialog(msoFileDialogFolderPicker)
        If .Show Then
            GetAllFiles .SelectedItems(1), fso, Dic
            [A1].Resize(Dic.Count) = Application.Transpose(Dic.Keys)
        End If
    End With
End Sub
Chỉ cần chế biến câu này thì lấy loại gì cũng được, ví dụ:
If ext = "doc" Then
hoặc
If ext = "doc" Or ext Like "xls*" Then
hoặc
If ext = "pdf" Or ext Like "xls*" Or ext Like "doc*" Then
PHP:
Function GetAllFiles(ByVal StrFolder As String, fso As Object, Dic As Object)
Dim objFolder As Object, objSubFolder As Object, File As Object, ext As String
    Set objFolder = fso.GetFolder(StrFolder)
    For Each File In objFolder.Files
        ext = fso.GetExtensionName(File)
        If ext = "doc" Or ext Like "xls*" Then
           Dic.Item(File.Name) = ""
        End If
    Next
    For Each objSubFolder In objFolder.SubFolders
        GetAllFiles objSubFolder.path, fso, Dic
    Next objSubFolder
End Function
 
Cảm ơn anh Ndu đã hướng dẫn.
Chắc do mình diễn đạt chưa rõ, ý mình là file của anh cho ra kết quả là kèm đường dẫn và tên file luôn (Ví dụ: D:\test\abc.doc) nhưng mình chỉ cần lấy cái tên abc.doc thôi, không kèm đường dẫn.
Vậy thì mình sẽ phải chỉnh chỗ nào ạ? Anh hướng dẫn giúp mình nhé
Cảm ơn anh nhiều

Thế thì cũng đâu phải chuyện khó
- Đầu tiên bạn có thư mục "D:\Test" (chẳng hạn) ---> Chuỗi này đếm được 7 ký tự, đúng không?
- Sau khi bạn lấy được tên file, chẳng hạn là "D:\Test\aaa.doc", vậy bạn chỉ cần dùng hàm MID, lấy từ ký tự thứ 8 trở đi là có ngay tên file thôi
 
Thế thì cũng đâu phải chuyện khó
- Đầu tiên bạn có thư mục "D:\Test" (chẳng hạn) ---> Chuỗi này đếm được 7 ký tự, đúng không?
- Sau khi bạn lấy được tên file, chẳng hạn là "D:\Test\aaa.doc", vậy bạn chỉ cần dùng hàm MID, lấy từ ký tự thứ 8 trở đi là có ngay tên file thôi
Vâng, anh nói chính xác, nhưng khó 1 điều là em có các thư mục con nên đến kiểu này không ổn, vì vậy cần linh động mới được ạ.
 
Lần chỉnh sửa cuối:
Vâng, anh nói chính xác, nhưng khó 1 điều là em có các thư mục con nên đến kiểu này không ổn, vì vậy cần linh động mới được ạ.
Nếu bạn lấy luôn tập tin của các thư mục con thì sau khi lấy tên các file có đường dẫn, dùng công thức sau để lấy tên file:
=RIGHT(B2,LEN(B2)-MAX(IF(MID(B2,ROW($1:$500),1)="\",ROW($1:$500)))) với B2 là ô chứa tên file.
Bạn nhớ nhấn Ctrl + Shift + Enter vì đây là công thức mảng.
 
Vâng, anh nói chính xác, nhưng khó 1 điều là em có các thư mục con nên đến kiểu này không ổn, vì vậy cần linh động mới được ạ.

Có hàng đống cách để làm:
- Đưa nguyên đường dẫn cho FileSystemObject nó xử lý---> Ra ngay kết quả
- Xử lý chuỗi thông thường, lấy từ ký tự phía sau dấu "\" cuối cùng của chuỗi (dùng hàm InStrRev)
 
Có hàng đống cách để làm:
- Đưa nguyên đường dẫn cho FileSystemObject nó xử lý---> Ra ngay kết quả
- Xử lý chuỗi thông thường, lấy từ ký tự phía sau dấu "\" cuối cùng của chuỗi (dùng hàm InStrRev)
Hihi, Cảm ơn anh Ndu, em gà mờ 2 cách anh đưa ra chẳng biết làm sao với chúng, nên tạm thời làm thêm 1 bước trung gian, chèn thêm 1 cột như công thức của bạn @Vu Dinh Duy hướng dẫn để lấy tên ra thôi.
 
Lần chỉnh sửa cuối:
Web KT
Back
Top Bottom