duchuan299
Thành viên mới 

			
		- Tham gia
- 30/3/11
- Bài viết
- 12
- Được thích
- 0
Chào anh/chị trên diễn đàn Giải Pháp Excel. Em mới tập tành VBA, em có đoạn code sau nhờ anh/chị giúp chỉnh sửa đoạn code để tối ưu ạ hoặc nếu có cách nào viết hay hơn anh/chị hướng dẫn giúp em. Đoạn code này dùng để "liệt kê tên các file từ 1 folder được chọn". Em cám ơn.
	
	
	
		
				
			
		Mã:
		
	
	Sub Report()
    Dim objFSO As Object
    Dim objFolder As Object
    Dim objFile As Object
    Dim i As Integer
    Dim sFldr As String
    Set objFSO = CreateObject("Scripting.FileSystemObject") 'Create an instance of the FileSystemObject
    Range("B4:C100") = "" 'set up innitial condition
    sFldr = Select_folder() 'Call the ChooseFolder function
    If sFldr <> "" Then 'Check sFldr choose or not
        Set objFolder = objFSO.GetFolder(sFldr)
        i = 1
        'loops through each file in the directory and prints their ordinal numbers and file name
        For Each objFile In objFolder.Files
            Cells(i + 3, 2) = i  'print ordinal numbers
            Cells(i + 3, 3) = objFile.Name  'print filenames
            i = i + 1
        Next objFile
    End If
End Sub
Function Select_folder()
    Dim fldr As FileDialog
    Dim sItem As String
    
    Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
    With fldr
        .Title = "Select a Folder"
        .AllowMultiSelect = False
        .InitialFileName = strPath
        If .Show <> -1 Then GoTo NextCode
        sItem = .SelectedItems(1)
    End With
NextCode:
    Select_folder = sItem
    Set fldr = Nothing
End Function 
	 
	  
 
 
		
 
 
		 
 
		

 
 
		 
 
		 
 
		
 
 
		 
 
		 
 
		 
 
		
 
 
		