Thí nghiệm với code này:Hi mọi người,
Trong macro để chọn 1 file bất kỳ thì có hàm Application.GetOpenFilename()
Cho mình hỏi là để chọn 1 folder/Directory bất kỳ thì dùng hàm j ạ. Mình search mãi mà ko dc![]()
Sub Test()
With Application.FileDialog(msoFileDialogFolderPicker)
.AllowMultiSelect = False
If .Show Then MsgBox .SelectedItems(1)
End With
End Sub
Bạn modify từ cái này nhé
Sub CommandButton2_Click()
Dim sFolder As String
On Error Resume Next
With CreateObject("Shell.Application")
sFolder = .BrowseForFolder(0, "", 1).Self.Path
End With
With cboFolder
If TypeName(sFolder) = "String" Then .Text = sFolder
.Enabled = False: .Enabled = True
End With
Sheet4.Range("Z1") = sFolder
End Sub
Thí nghiệm với code này:
Mã:Sub Test() With Application.FileDialog(msoFileDialogFolderPicker) .AllowMultiSelect = False If .Show Then MsgBox .SelectedItems(1) End With End Sub
Cái SelectedItems(1) chính là folder mà bạn vừa chọn
-----------------------------------------------
Bạn khai báo Dim sFolder As String nên sFolder nó phải là "String" rồi. Vì thế mà đoạn If TypeName(sFolder) = "String" là thừa ngay cả khi bạn bấm Cancel