quochung2005
Thành viên chính thức


- Tham gia
- 16/6/06
- Bài viết
- 89
- Được thích
- 19
chào anh chị trong diễn đàn!
Mình mới sưu tập trên internet 1 marco backup dữ liệu mình muốn share cho mọi người, mình cũng không rành về VBA, các bạn có thể chỉnh sửa lại và lam theo ý mình !
marco trên thì back up cùng với file hiện hành (file nằm trong thư mục nào thì backup trong thư mục đó, có đuoi bachup.)
---------------
còn marco này thì chỉ đường dẫn VD D:\ thì lưu vào drive D:
Mình mới sưu tập trên internet 1 marco backup dữ liệu mình muốn share cho mọi người, mình cũng không rành về VBA, các bạn có thể chỉnh sửa lại và lam theo ý mình !
PHP:
Sub SaveWorkbookBackup()
Dim awb As Workbook, BackupFileName As String, i As Integer, OK As Boolean
If TypeName(ActiveWorkbook) = "Nothing" Then Exit Sub
Set awb = ActiveWorkbook
If awb.Path = "" Then
Application.Dialogs(xlDialogSaveAs).Show
Else
BackupFileName = awb.FullName
i = 0
While InStr(i + 1, BackupFileName, ".") > 0
i = InStr(i + 1, BackupFileName, ".")
Wend
If i > 0 Then BackupFileName = Left(BackupFileName, i - 1)
BackupFileName = BackupFileName & ".backup"
OK = False
On Error GoTo NotAbleToSave
With awb
Application.StatusBar = "Saving this workbook..."
.Save
Application.StatusBar = "Saving this workbook backup..."
.SaveCopyAs BackupFileName
OK = True
End With
End If
NotAbleToSave:
Set awb = Nothing
Application.StatusBar = False
If Not OK Then
MsgBox "Backup Copy Not Saved!", vbExclamation, ThisWorkbook.Name
End If
End Sub
---------------
còn marco này thì chỉ đường dẫn VD D:\ thì lưu vào drive D:
PHP:
Sub SaveWorkbookBackupTodrive()
Dim awb As Workbook, BackupFileName As String, i As Integer, OK As Boolean
If TypeName(ActiveWorkbook) = "Nothing" Then Exit Sub
Set awb = ActiveWorkbook
If awb.Path = "" Then
Application.Dialogs(xlDialogSaveAs).Show
Else
BackupFileName = awb.Name
OK = False
On Error GoTo NotAbleToSave
If Dir("D:" & BackupFileName) <> "" Then
Kill "D:" & BackupFileName
End If
With awb
Application.StatusBar = "Saving this workbook..."
.Save
Application.StatusBar = "Saving this workbook backup..."
.SaveCopyAs "D:"& BackupFileName
OK = True
End With
End If
NotAbleToSave:
Set awb = Nothing
Application.StatusBar = False
If Not OK Then
MsgBox "Backup Copy Not Saved!", vbExclamation, ThisWorkbook.Name
End If
End Sub