EXCEL TOOLS
Excel Add-ins ที่พัฒนาโดยคุณสันติพงศ์ ณสุย (MVP Excel 2010-2020) ด้วยภาษา C# เพื่อแก้ไขปัญหาไฟล์ใหญ่ คำนวณนาน ทำงานช้า จัดการข้อมูลต่าง ๆ ที่ทำงานประจำวันได้อย่างสะดวกรวดเร็ว สนใจคลิกไปดูได้ที่นี่ครับ => Excel Tools
Excel Add-ins ที่พัฒนาโดยคุณสันติพงศ์ ณสุย (MVP Excel 2010-2020) ด้วยภาษา C# เพื่อแก้ไขปัญหาไฟล์ใหญ่ คำนวณนาน ทำงานช้า จัดการข้อมูลต่าง ๆ ที่ทำงานประจำวันได้อย่างสะดวกรวดเร็ว สนใจคลิกไปดูได้ที่นี่ครับ => Excel Tools
[code]
และปิดด้วย [/code]
ตัวอย่างเช่น [code]dim r as range[/code]
เพื่อให้แตกต่างจากข้อความทั่วไป สะดวกในการอ่านและทดสอบ (คลิกเพื่อดูตัวอย่าง)Code: Select all
Sub run_macro()
Dim on_time As Date
on_time = Now + TimeValue("00:01:00") ' currently its 1 min chnage it
Application.OnTime on_time, "run_macro"
Call add_Data
End Sub
Sub add_Data()
Dim wk As Workbook
'Set wk = Workbooks.Open("C:\Users\admin\Downloads\Link Data.xlsx")
Set wk = Workbooks.Open("D:\Test Link\Link Data.xlsx")
'wk.Sheets(1).Range("xfd2").End(xlToLeft).Offset(-1, 1).Value = ThisWorkbook.Sheets(1).Range("a1").Value
wk.Sheets(1).Range("xfd2").End(xlToLeft).Offset(-1, 1).Value = Format(Now, "yyyy-mm-dd hh-mm-ss")
ThisWorkbook.Sheets(1).Range("A1:A25").Copy Destination:=wk.Sheets(1).Range("xfd2").End(xlToLeft).Offset(0, 1)
ThisWorkbook.Sheets(1).Range("B1:B25").Copy Destination:=wk.Sheets(1).Range("xfd2").End(xlToLeft).Offset(0, 1)
wk.Save
wk.Close
End Sub
ThisWorkbook
ก็ต้องใส่ Code ที่เคยโพสต์ไว้เดิมด้วยครับCode: Select all
Sub run_macro()
Dim on_time As Date
on_time = Now + TimeValue("00:01:00") ' currently its 1 min change it
Application.OnTime on_time, "run_macro"
Call add_Data
End Sub
Sub add_Data()
Dim wk As Workbook, wb As Workbook
Dim CurrBook As Workbook, CheckFile As Boolean
For Each wb In Workbooks
If wb.Name = "Link Data.xlsx" Then CheckFile = True
Next wb
Set CurrBook = ThisWorkbook
If CheckFile = True Then
Set wk = Workbooks("Link Data.xlsx")
Else
Set wk = Workbooks.Open("D:\Test Link\Link Data.xlsx")
End If
wk.Sheets(1).Range("xfd2").End(xlToLeft).Offset(-1, 1).Value _
= Format(Now, "yyyy-mm-dd hh-mm-ss")
CurrBook.Sheets(1).Range("A1:B25").Copy _
Destination:=wk.Sheets(1).Range("xfd2").End(xlToLeft).Offset(0, 1)
wk.Save
'wk.Close
End Sub
Code: Select all
Private Sub Workbook_BeforeClose(Cancel As Boolean)
'Application.OnTime nTime, "SaveFile", , False
Application.OnTime on_time, "run_macro", , False
End Sub
Private Sub Workbook_Open()
'Call SaveFile
Call run_macro
End Sub
on_time
ให้เป็น Public ด้วยครับ ให้สังเกตว่าโพสต์เดิมมีการประกาศและใช้ตัวแปร nTime
อย่างไร โพสต์นี้ก็ให้ทำลักษณะนั้นครับCode: Select all
CurrBook.Sheets(1).Range("A1:B25").Copy
wk.Sheets(1).Range("xfd2").End(xlToLeft).Offset(0, 1).PasteSpecial xlPasteValues
Code: Select all
Public nTime As Double
Sub SaveFile()
Dim nTime As Date
nTime = Now + TimeValue("00:01:00") ' currently its 1 min change it
Application.OnTime nTime, "SaveFile"
Call Copy_Data
End Sub
Sub Copy_Data()
Dim wk As Workbook, wb As Workbook
Dim CurrBook As Workbook, CheckFile As Boolean
For Each wb In Workbooks
If wb.Name = "Link Data.xlsx" Then CheckFile = True
Next wb
Set CurrBook = ThisWorkbook
If CheckFile = True Then
Set wk = Workbooks("Link Data.xlsx")
Else
Set wk = Workbooks.Open("D:\SET Backup\Link Data.xlsx")
End If
wk.Sheets(1).Range("xfd2").End(xlToLeft).Offset(-1, 1).Value = Format(Now, "yyyy-mm-dd hh-mm-ss")
'CurrBook.Sheets(3).Range("d2:d51").Copy wk.Sheets(1).Range("xfd2").End(xlToLeft).Offset(0, 1)
'CurrBook.Sheets(3).Range("j2:j51").Copy wk.Sheets(1).Range("xfd2").End(xlToLeft).Offset(0, 1)
CurrBook.Sheets(3).Range("d2:d51").Copy wk.Sheets(1).Range("xfd2").End(xlToLeft).Offset(0, 1).PasteSpecial xlPasteValues
'wk.Sheets(2).Range("xfd2").End(xlToLeft).Offset(-1, 1).Value = Format(Now, "yyyy-mm-dd hh-mm-ss")
'CurrBook.Sheets(3).Range("d56:d105").Copy wk.Sheets(2).Range("xfd2").End(xlToLeft).Offset(0, 1)
'CurrBook.Sheets(3).Range("j56:j105").Copy wk.Sheets(2).Range("xfd2").End(xlToLeft).Offset(0, 1)
'wk.Sheets(3).Range("xfd2").End(xlToLeft).Offset(-1, 1).Value = Format(Now, "yyyy-mm-dd hh-mm-ss")
'CurrBook.Sheets(4).Range("d2:d51").Copy wk.Sheets(3).Range("xfd2").End(xlToLeft).Offset(0, 1)
'CurrBook.Sheets(4).Range("j2:j51").Copy wk.Sheets(3).Range("xfd2").End(xlToLeft).Offset(0, 1)
'wk.Sheets(4).Range("xfd2").End(xlToLeft).Offset(-1, 1).Value = Format(Now, "yyyy-mm-dd hh-mm-ss")
'CurrBook.Sheets(4).Range("d56:d105").Copy wk.Sheets(4).Range("xfd2").End(xlToLeft).Offset(0, 1)
'CurrBook.Sheets(4).Range("j56:j105").Copy wk.Sheets(4).Range("xfd2").End(xlToLeft).Offset(0, 1)
wk.Save
'wk.Close
End Sub
Code: Select all
Public nTime As Double
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Application.OnTime nTime, "SaveFile", , False
End Sub
Private Sub Workbook_Open()
Call SaveFile
End Sub
Code: Select all
Sub SaveFile()
Dim nTime As Date
nTime = Now + TimeValue("00:01:00") ' currently its 1 min change it
Application.OnTime nTime, "SaveFile"
Call Copy_Data
End Sub
Sub Copy_Data()
Dim wk As Workbook, wb As Workbook
Dim CurrBook As Workbook, CheckFile As Boolean
For Each wb In Workbooks
If wb.Name = "Link Data.xlsx" Then CheckFile = True
Next wb
Set CurrBook = ThisWorkbook
If CheckFile = True Then
Set wk = Workbooks("Link Data.xlsx")
Else
Set wk = Workbooks.Open("D:\SET Backup\Link Data.xlsx")
End If
wk.Sheets(1).Range("xfd2").End(xlToLeft).Offset(-1, 1).Value = Format(Now, "yyyy-mm-dd hh-mm-ss")
CurrBook.Sheets(3).Range("d2:d51").Copy
wk.Sheets(1).Range("xfd2").End(xlToLeft).Offset(0, 1).PasteSpecial xlPasteValues
CurrBook.Sheets(3).Range("j2:j51").Copy
wk.Sheets(1).Range("xfd2").End(xlToLeft).Offset(0, 1).PasteSpecial xlPasteValues
wk.Sheets(2).Range("xfd2").End(xlToLeft).Offset(-1, 1).Value = Format(Now, "yyyy-mm-dd hh-mm-ss")
CurrBook.Sheets(3).Range("d56:d105").Copy
wk.Sheets(2).Range("xfd2").End(xlToLeft).Offset(0, 1).PasteSpecial xlPasteValues
CurrBook.Sheets(3).Range("j56:j105").Copy
wk.Sheets(2).Range("xfd2").End(xlToLeft).Offset(0, 1).PasteSpecial xlPasteValues
wk.Sheets(3).Range("xfd2").End(xlToLeft).Offset(-1, 1).Value = Format(Now, "yyyy-mm-dd hh-mm-ss")
CurrBook.Sheets(4).Range("d2:d51").Copy
wk.Sheets(3).Range("xfd2").End(xlToLeft).Offset(0, 1).PasteSpecial xlPasteValues
CurrBook.Sheets(4).Range("j2:j51").Copy
wk.Sheets(3).Range("xfd2").End(xlToLeft).Offset(0, 1).PasteSpecial xlPasteValues
wk.Sheets(4).Range("xfd2").End(xlToLeft).Offset(-1, 1).Value = Format(Now, "yyyy-mm-dd hh-mm-ss")
CurrBook.Sheets(4).Range("d56:d105").Copy
wk.Sheets(4).Range("xfd2").End(xlToLeft).Offset(0, 1).PasteSpecial xlPasteValues
CurrBook.Sheets(4).Range("j56:j105").Copy
wk.Sheets(4).Range("xfd2").End(xlToLeft).Offset(0, 1).PasteSpecial xlPasteValues
wk.Save
'wk.Close
End Sub
Code: Select all
Sub SaveFile()
Dim nTime As Date
nTime = Now + TimeValue("00:01:00") ' currently its 1 min change it
Application.OnTime nTime, "SaveFile"
Call Copy_Data
End Sub
Code: Select all
Sub Copy_Data
.
.
.
If Now > TimeValue("16:00:00") Then
wk.Close
Exit Sub
End If
End Sub
Code: Select all
Sub Copy_Data
'Other code
If Now > Date + TimeValue("16:00:00") Then
Application.OnTime nTime, "SaveFile", , False
wk.save
wk.Close
Exit Sub
End If
End Sub
Code: Select all
Public nTime As Date
Sub SaveFile()
nTime = Now + TimeValue("00:15:00") ' currently its 15 min change it
Application.OnTime nTime, "SaveFile"
Call Copy_Data
End Sub
Sub Copy_Data()
Dim wk As Workbook, wb As Workbook
Dim CurrBook As Workbook, CheckFile As Boolean
For Each wb In Workbooks
If wb.Name = "Link Data.xlsx" Then CheckFile = True
Next wb
Set CurrBook = ThisWorkbook
If CheckFile = True Then
Set wk = Workbooks("Link Data.xlsx")
Else
Set wk = Workbooks.Open("D:\SET Backup\Link Data.xlsx")
End If
wk.Sheets(1).Range("xfd2").End(xlToLeft).Offset(-1, 1).Value = Format(Now, "yyyy-mm-dd hh-mm-ss")
CurrBook.Sheets(3).Range("d2:d51").Copy
wk.Sheets(1).Range("xfd2").End(xlToLeft).Offset(0, 1).PasteSpecial xlPasteValues
CurrBook.Sheets(3).Range("j2:j51").Copy
wk.Sheets(1).Range("xfd2").End(xlToLeft).Offset(0, 1).PasteSpecial xlPasteValues
wk.Sheets(2).Range("xfd2").End(xlToLeft).Offset(-1, 1).Value = Format(Now, "yyyy-mm-dd hh-mm-ss")
CurrBook.Sheets(3).Range("d56:d105").Copy
wk.Sheets(2).Range("xfd2").End(xlToLeft).Offset(0, 1).PasteSpecial xlPasteValues
CurrBook.Sheets(3).Range("j56:j105").Copy
wk.Sheets(2).Range("xfd2").End(xlToLeft).Offset(0, 1).PasteSpecial xlPasteValues
'wk.Sheets(3).Range("xfd2").End(xlToLeft).Offset(-1, 1).Value = Format(Now, "yyyy-mm-dd hh-mm-ss")
'CurrBook.Sheets(4).Range("d2:d51").Copy
'wk.Sheets(3).Range("xfd2").End(xlToLeft).Offset(0, 1).PasteSpecial xlPasteValues
'CurrBook.Sheets(4).Range("j2:j51").Copy
'wk.Sheets(3).Range("xfd2").End(xlToLeft).Offset(0, 1).PasteSpecial xlPasteValues
'wk.Sheets(4).Range("xfd2").End(xlToLeft).Offset(-1, 1).Value = Format(Now, "yyyy-mm-dd hh-mm-ss")
'CurrBook.Sheets(4).Range("d56:d105").Copy
'wk.Sheets(4).Range("xfd2").End(xlToLeft).Offset(0, 1).PasteSpecial xlPasteValues
'CurrBook.Sheets(4).Range("j56:j105").Copy
'wk.Sheets(4).Range("xfd2").End(xlToLeft).Offset(0, 1).PasteSpecial xlPasteValues
wk.Save
'wk.Close
If Now > Date + TimeValue("16:00:00") Then
Application.OnTime nTime, "SaveFile", , False
wk.Save
wk.Close
Exit Sub
End If
End Sub