Bạn có thể tạo thư mục trong VBA bằng việc sử dụng đối tượng Scripting.FileSystemObject hoặc lệnh MkDir. Hãy xem các ví dụ sau đây:
Ví dụ 1: Sử dụng đối tượng Scripting.FileSystemObject để tạo thư mục trong VBA.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | Sub createFolderExample1() Dim fso As Object Set fso = CreateObject( "Scripting.FileSystemObject" ) Dim currentPath As String Dim folderTest As String currentPath = Application.ActiveWorkbook.Path folderTest = currentPath & "\" & " test" ' create folder "test" if not exists If ( Not fso.FolderExists(folderTest)) Then fso.CreateFolder (folderTest) End If End Sub |
Ví dụ 2: Sử dụng lệnh MkDir để tạo thư mục trong VBA.
1 2 3 4 5 6 7 8 9 10 11 12 | Sub createFolderExample2() Dim currentPath As String Dim folderTest As String currentPath = Application.ActiveWorkbook.Path folderTest = currentPath & "\" & " test2" ' create folder "test2" if not exists If Dir(folderTest, vbDirectory) = "" Then MkDir folderTest End If End Sub |