snasui.com ยินดีต้อนรับ ยินดีต้อนรับสู่กระดานถามตอบ Excel and VBA และอื่น ๆ ที่เป็นมิตรกับทุกท่าน มีไฟล์แนบมหาศาล ช่วยให้ท่านค้นหาและติดตามศึกษาได้โดยง่าย สมาชิกท่านใดที่ยังไม่ได้ระบุ Version ของ Excel ที่ใช้งานจริง สามารถทำตาม Link นี้เพื่อจะได้รับคำตอบที่ตรงกับ Version ของท่านครับ ระบุ Version ของ Excel
(code)
Public Const backupfolder As String = "C:\excelbackup\"
Sub Auto_Save()
If Dir(backupfolder, vbDirectory) = "" Then
' This will check if folder already exist
' If not then it will create new directory at first time
MkDir backupfolder
Call Save
' Save copy of Excel file to backupfolder
Else
Call Save
' If folder already exist then only save copy of Excel File
End If
End Sub
Function Save() As String
' Auto_save Macro
' This will create a copy of file at Seleted destination path
' Keyboard Shortcut: Ctrl+r
' Saves the current file to a backup folder
Dim savedate
savedate = Date ' Current system date
Dim savetime
savetime = Time ' Current system time
Dim formattime As String
formattime = Format(savetime, "hh.mm.ss")
Dim formatdate As String
formatdate = Format(savedate, "DD-MM-YYYY")
Public Const backupfolder As String = "C:\excelbackup\"
Sub Auto_Save()
If Dir(backupfolder, vbDirectory) = "" Then
' This will check if folder already exist
' If not then it will create new directory at first time
MkDir backupfolder
Call Save
' Save copy of Excel file to backupfolder
Else
Call Save
' If folder already exist then only save copy of Excel File
End If
End Sub
Function Save() As String
' Auto_save Macro
' This will create a copy of file at Seleted destination path
' Keyboard Shortcut: Ctrl+r
' Saves the current file to a backup folder
Dim savedate
savedate = Date ' Current system date
Dim savetime
savetime = Time ' Current system time
Dim formattime As String
formattime = Format(savetime, "hh.mm.ss")
Dim formatdate As String
formatdate = Format(savedate, "DD-MM-YYYY")
Application.DisplayAlerts = False
'
'Application.Run ("Auto_Save")
'
ActiveWorkbook.SaveCopyAs Filename:=backupfolder & formatdate & " " & formattime & " " & ActiveWorkbook.Name
ActiveWorkbook.Save
Application.DisplayAlerts = True
Beep
MsgBox "Backup Run. Please Check at: " & backupfolder
End Function
ต้องการให้ auto save ทุกๆ 30 นาที โดยไม่ต้องกด Ctrl+Q และทำงานระหว่างเวลา 10.00am-05.00pm
You do not have the required permissions to view the files attached to this post.
Public Const backupfolder As String = "C:\excelbackup\"
Sub Auto_Save()
If Dir(backupfolder, vbDirectory) = "" Then
' This will check if folder already exist
' If not then it will create new directory at first time
MkDir backupfolder
Call Save
' Save copy of Excel file to backupfolder
Else
Call Save
' If folder already exist then only save copy of Excel File
End If
End Sub
Function Save() As String
' Auto_save Macro
' This will create a copy of file at Seleted destination path
' Keyboard Shortcut: Ctrl+r
' Saves the current file to a backup folder
Dim savedate
savedate = Date ' Current system date
Dim savetime
savetime = Time ' Current system time
Dim formattime As String
formattime = Format(savetime, "hh.mm.ss")
Dim formatdate As String
formatdate = Format(savedate, "DD-MM-YYYY")
Application.DisplayAlerts = False
'
'Application.Run ("Auto_Save")
'
ActiveWorkbook.SaveCopyAs Filename:=backupfolder & formatdate & " " & formattime & " " & ActiveWorkbook.Name
ActiveWorkbook.Save
Application.DisplayAlerts = True
Beep
End Function
You do not have the required permissions to view the files attached to this post.
ขอบคุณที่เป็นแรงบันดาลให้เรียนรู้ โค้ดข้างล่างให้งานได้แล้วครับ แต่อยากขอคำแนะนำเพิ่มเติม เรื่องการเขียน ต้องตั้งตัวแปร หรือแก้ไขอย่างไรให้กระชับ เหมาะสมกว่านี้ครับ
Final : Auto Save AS every 30 minutes with NO warning upon saving over the last copy.
__________
ThisWorkBook
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Application.OnTime nTime, "SaveFile", , False
End Sub
Private Sub Workbook_Open()
Call SaveFile
End Sub
Public Const backupfolder As String = "D:\SETbackup\"
Public nTime As Double
Public Sub SaveFile()
With ThisWorkbook
nTime = Now + TimeSerial(0, 1, 0) 'every minute
If Dir(backupfolder, vbDirectory) = "" Then
MkDir backupfolder
.SaveCopyAs Filename:=backupfolder & (Format(Now, " yyyy-mm-dd, hh.mm.ss ")) & ActiveWorkbook.Name
Application.OnTime nTime, "SaveFile"
Else
.SaveCopyAs Filename:=backupfolder & (Format(Now, " yyyy-mm-dd, hh.mm.ss ")) & ActiveWorkbook.Name
Application.OnTime nTime, "SaveFile"
End If
End With
End Sub
Public Const backupfolder As String = "D:\SETbackup\"
Public nTime As Double
Public Sub SaveFile()
With ThisWorkbook
nTime = Now + TimeSerial(0, 1, 0) 'every minute
If Dir(backupfolder, vbDirectory) = "" Then
MkDir backupfolder
End If
.SaveCopyAs Filename:=backupfolder & (Format(Now, _
" yyyy-mm-dd, hh.mm.ss ")) & ActiveWorkbook.Name
Application.OnTime nTime, "SaveFile"
End With
End Sub