[VBA] Folder & File Handling



Các đoạn mã VBA được tổng hợp từ analysistabs.com bao gồm:

Kiểm tra xem thư mục có tồn tại hay chưa

'In this Example I am checking if "C:\Temp" exits
Sub sbCheckingIfAFolderExists()

Dim FSO
Dim sFolder As String

sFolder = "C:\Temp" ' You can Specify Any Folder To Check It
Set FSO = CreateObject("Scripting.FileSystemObject")

If FSO.FolderExists(sFolder) Then
MsgBox "Specified Folder Is Available", vbInformation, "Exists!"
Else
MsgBox folder &"Specified Folder Not Found", vbInformation, "Not Found!"
End If

End Sub

Mở thư mục bằng VBA

'In this Example I am Opening a Folder ("C:\Temp")
Sub sbOpeningAFolder()

Dim FSO
Dim sFolder As String
sFolder = "C:\Temp" 'You can specify your Folder which you wants to Open

Set FSO = CreateObject("Scripting.FileSystemObject")

If Not FSO.FolderExists(sFolder) Then
MsgBox "Specified Folder Not Found", vbInformation, "Folder Not Found!"
ElseIf FSO.FolderExists(sFolder) Then
Call Shell("explorer.exe " &sFolder, vbNormalFocus)
End If

End Sub

Tạo thư mục bằng VBA

'In this Example I am creating a Folder "C:\SampleFolder"
Sub sbCreatingAFolder()

Dim FSO
Dim sFolder As String

sFolder= "C:\SampleFolder" ' You can Specify Any Path and Name To Create a Folder
Set FSO = CreateObject("Scripting.FileSystemObject")

If Not FSO.FolderExists(sFolder) Then
FSO.CreateFolder (sFolder) 'Checking if the same Folder already exists
MsgBox "New FolderCreated Successfully", vbExclamation, "Done!"
Else
MsgBox "Specified Folder Already Exists", vbExclamation, "Folder Already Exists!"
End If

End Sub

Sao chép thư mục sang 1 chỗ mới

'In this Example I am Coping "C:\Temp\" to "D:\Job\"
Sub sbCopyingAFolder()

Dim FSO
Dim sFolder As String, dFolder As String

sFolder = "C:\Temp\" 'Specify Your Source Folder
dFolder = "D:\Job\" ' Specify Your Destination Folder
Set FSO = CreateObject("Scripting.FileSystemObject")

If Not FSO.FolderExists(dFolder) Then
FSO.CopyFolder sFolder, dFolder
MsgBox "Folder Copied Successfully to The Destination", vbExclamation, "Done!"
Else
MsgBox "Folder Already Exists in the Destination", vbExclamation, "Folder Already Exists!"
End If

End Sub

Di chuyển thư mục sang chỗ mới

'In this Example I am Moving "C:\Temp\" to "D:\Job\"
Sub sbMovingAFolder()

Dim FSO
Dim sFolder As String, dFolder As String

sFolder = "C:\Temp\" 'Specify Your Source Folder
dFolder = "D:\Job\" ' Specify Your Destination Folder
Set FSO = CreateObject("Scripting.FileSystemObject")

If Not FSO.FolderExists(dFolder) Then
FSO.MoveFolder sFolder, dFolder
MsgBox "Folder Moved Successfully to The Destination", vbExclamation, "Done!"
Else
MsgBox "Folder Already Exists in the Destination", vbExclamation, "Folder Already Exists!"
End If

End Sub

Xóa thư mục

'In this Example I am Deleting "C:\SampleFolder"
Sub sbDeletingAFolder()

Dim FSO
Dim sFolder As String

sFolder = "C:\SampleFolder" 'Specify Your Folder Which You Wants to Delete
Set FSO = CreateObject("Scripting.FileSystemObject")

If FSO.FolderExists(sFolder) Then
FSO.DeleteFolder sFolder
MsgBox "Specified Folder Deleted Successfully", vbExclamation, "Done!"
Else
MsgBox "Specified Folder Not Found", vbExclamation, "Not Found!"
End If

End Sub

Đặt chế độ read-only cho file

Sub sbMakeFileReadOnly()

Dim strSaveFilename As String
Dim oFSO As Object
Dim oFile As Object

sFile = "C:\ExampleFile.xls" 'Your File name and Path to make it read only
'Create Objects
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFile = oFSO.GetFile(FilePath:=sFile)

'Set file to be read-only
oFile.Attributes = 1

'Releasing Objects
If Not oFSO Is Nothing Then Set oFSO = Nothing
If Not oFile Is Nothing Then Set oFile = Nothing

End Sub

Sao chép toàn bộ file Excel từ 1 folder sang 1 folder khác

'In this Example I am Coping all excel files from one Folder ("C:\Temp\") to another Folder ("D:\Job\")
Sub sbCopyingAllExcelFiles()

Dim FSO
Dim sFolder As String
Dim dFolder As String

sFolder = "C:\Temp\" ' change to match the source folder path
dFolder = "D:\Job\" ' change to match the destination folder path
Set FSO = CreateObject("Scripting.FileSystemObject")

If Not FSO.FolderExists(sFolder) Then
MsgBox "Source Folder Not Found", vbInformation, "Source Not Found!"
ElseIf Not FSO.FolderExists(dFolder) Then
MsgBox "Destination Folder Not Found", vbInformation, "Destination Not Found!"
Else
FSO.CopyFile (sFolder & "\*.xl*"), dFolder
MsgBox "Successfully Copied All Excel Files to Destination", vbInformation, "Done!"
End If

End Sub

Mở file bằng file dialog

Sub OpenWorkbookUsingFileDialog()

Dim fdl As FileDialog
Dim FileName As String
Dim FileChosen As Integer

Set fdl = Application.FileDialog(msoFileDialogFilePicker)

'Set the caption of the dialog box,
fdl.Title = "Please Select a Excel Macro File"
'Set the InitialFile Path
fdl.InitialFileName = "c:\"
'Set the Folder View
fdl.InitialView = msoFileDialogViewSmallIcons
'Set the filter
fdl.Filters.Clear
fdl.Filters.Add "Excel Macros Files", "*.xlsm"

FileChosen = fdl.Show

If FileChosen <> -1 Then
'Not choosen anything / Clicked on CANCEL
MsgBox "You have choosen nothing"
Else
'display name and complete path of file chosen
MsgBox fdl .SelectedItems(1)
End If

FileName = fdl.SelectedItems(1)
'Open the File
Workbooks.Open (FileName)

End Sub
[VBA] Folder & File Handling [VBA] Folder & File Handling Reviewed by Le Huy Hoang on December 25, 2017 Rating: 5

No comments:

Powered by Blogger.