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 AutoTimer()
Application.OnTime TimeValue("10:00:00"), "StartTimer"
Application.OnTime TimeValue("12:30:00"), "StopTimer"
Application.OnTime TimeValue("14:30:00"), "StartTimer"
Application.OnTime TimeValue("16:30:00"), "StopTimer"
End Sub
Code: Select all
Sub StartTimer()
Dim t As String
With Sheets("VolCalculation")
t = Format(.Range("G8").Value, "00")
t = t & ":" & Format(.Range("H8").Value, "00")
t = t & ":" & Format(.Range("I8").Value, "00")
End With
dTime = Now + TimeValue(t)
Application.OnTime dTime, "ValueStore", Schedule:=True
End Sub
Sub StopTimer()
On Error Resume Next
Application.OnTime dTime, "ValueStore", Schedule:=False
End Sub
Code: Select all
Sub AutoTimer()
Application.OnTime TimeValue("17:22:00"), "AutoOn", Schedule:=True
On Error Resume Next
Application.OnTime TimeValue("17:22:01"), "AutoOn", Schedule:=False
Application.OnTime TimeValue("17:23:00"), "AutoOff"
Application.OnTime TimeValue("17:24:00"), "AutoOn", Schedule:=True
On Error Resume Next
Application.OnTime TimeValue("17:24:01"), "AutoOn", Schedule:=False
Application.OnTime TimeValue("17:25:00"), "AutoOff"
End Sub
Code: Select all
Sub AutoOn()
Call StartTimer
End Sub
Code: Select all
Sub AutoOff()
Call StopTimer
End Sub
Code: Select all
Sub AutoTimer()
Application.OnTime TimeValue("10:00:00"), "AutoOn", Schedule:=True
On Error Resume Next
Application.OnTime TimeValue("10:00:01"), "AutoOn", Schedule:=False
Application.OnTime TimeValue("12:30:00"), "AutoOff"
Application.OnTime TimeValue("14:30:00"), "AutoOn", Schedule:=True
On Error Resume Next
Application.OnTime TimeValue("14:30:01"), "AutoOn", Schedule:=False
Application.OnTime TimeValue("16:30:00"), "AutoOff"
End Sub
Code: Select all
Option Explicit
Public dTime As Date
Public aTime As Date
Public bTime As Date
Sub ValueStore()
Dim NC As Long
With Sheets("VolCalculation")
NC = .Cells(2, .Columns.Count).End(xlToLeft).Column + 1
.Cells(2, NC).Resize(2).Value = .Range("C2:C3").Value
If NC > 30 Then .Range("D2:D3").Delete xlShiftToLeft
End With
Application.CutCopyMode = False
Call StartTimer
End Sub
Sub StartTimer()
Dim t As String
With Sheets("VolCalculation")
t = Format(.Range("G8").Value, "00")
t = t & ":" & Format(.Range("H8").Value, "00")
t = t & ":" & Format(.Range("I8").Value, "00")
End With
dTime = Now + TimeValue(t)
Application.OnTime dTime, "ValueStore", Schedule:=True
End Sub
Sub StopTimer()
On Error Resume Next
Application.OnTime dTime, "ValueStore", Schedule:=False
End Sub
Sub AutoOn()
Call StartTimer
End Sub
Sub AutoOff()
Call StopTimer
End Sub
Sub ConditionAuto()
aTime = Now()
If aTime >= TimeValue("10:00:00") And aTime <= TimeValue("12:30:00") Then
Call AutoTimer1
If aTime > TimeValue("12:30:00") And aTime <= TimeValue("14:30:00") Then
Call AutoTimer2
If aTime > TimeValue("14:30:00") And aTime <= TimeValue("16:30:00") Then
Call AutoTimer3
Else
Call AutoTimer0
End If
End If
End If
End Sub
Sub AutoTimer0()
Application.OnTime TimeValue("10:00:00"), "AutoOn", Schedule:=True
On Error Resume Next
Application.OnTime TimeValue("10:00:01"), "AutoOn", Schedule:=False
Application.OnTime TimeValue("12:30:00"), "AutoOff"
Application.OnTime TimeValue("14:30:00"), "AutoOn", Schedule:=True
On Error Resume Next
Application.OnTime TimeValue("14:30:01"), "AutoOn", Schedule:=False
Application.OnTime TimeValue("16:30:00"), "AutoOff"
End Sub
Sub AutoTimer1()
aTime = Now()
bTime = Now + TimeValue("00:00:01")
Application.OnTime aTime, "AutoOn", Schedule:=True
On Error Resume Next
Application.OnTime bTime, "AutoOn", Schedule:=False
Application.OnTime TimeValue("12:30:00"), "AutoOff"
Application.OnTime TimeValue("14:30:00"), "AutoOn", Schedule:=True
On Error Resume Next
Application.OnTime TimeValue("14:30:01"), "AutoOn", Schedule:=False
Application.OnTime TimeValue("16:30:00"), "AutoOff"
End Sub
Sub AutoTimer2()
Application.OnTime TimeValue("14:30:00"), "AutoOn", Schedule:=True
On Error Resume Next
Application.OnTime TimeValue("14:30:01"), "AutoOn", Schedule:=False
Application.OnTime TimeValue("16:30:00"), "AutoOff"
End Sub
Sub AutoTimer3()
aTime = Now()
bTime = Now + TimeValue("00:00:01")
Application.OnTime aTime, "AutoOn", Schedule:=True
On Error Resume Next
Application.OnTime bTime, "AutoOn", Schedule:=False
Application.OnTime TimeValue("16:30:00"), "AutoOff"
End Sub
ConditionAuto
ถูกเรียกด้วย Procedure ใดหรือทำงานเมื่อใด ทำงานตลอดเวลาหรือทำงานครั้งเดียวครับAtoTimer0
ครับCode: Select all
Sub ConditionAuto()
aTime = Now()
If aTime >= TimeValue("10:00:00") And aTime <= TimeValue("12:30:00") Then
Call AutoTimer1
ElseIf aTime > TimeValue("12:30:00") And aTime <= TimeValue("14:30:00") Then
Call AutoTimer2
ElseIf aTime > TimeValue("14:30:00") And aTime <= TimeValue("16:30:00") Then
Call AutoTimer3
Else
Call AutoTimer0
End If
End Sub
?aTime
ก็จะทราบว่าตัวแปร aTime
มีค่าเป็นเท่าไร เช่นนี้ครับCode: Select all
aTime = FORMAT(Now(), "HH:mm:ss")
aTime = Now()
ตัวแปร aTime
จะไม่ใช่เฉพาะเวลาครับNow()
คือวันที่และเวลา หากจะเอาเฉพาะเวลาอย่างเดียวจะใช้ Time
ครับCode: Select all
Option Explicit
Public dTime As Date
Public aTime As Date
Public bTime As Date
Public OpenT1 As Date
Public OpenT2 As Date
Public BreakT As Date
Public CloseT As Date
Sub ValueStore()
Dim NC As Long
With Sheets("VolCalculation")
NC = .Cells(2, .Columns.Count).End(xlToLeft).Column + 1
.Cells(2, NC).Resize(2).Value = .Range("C2:C3").Value
If NC > 30 Then .Range("D2:D3").Delete xlShiftToLeft
End With
Application.CutCopyMode = False
Call StartTimer
End Sub
Sub StartTimer()
Dim t As String
With Sheets("VolCalculation")
t = Format(.Range("G8").Value, "00")
t = t & ":" & Format(.Range("H8").Value, "00")
t = t & ":" & Format(.Range("I8").Value, "00")
End With
dTime = Now + TimeValue(t)
Application.OnTime dTime, "ValueStore", Schedule:=True
End Sub
Sub StopTimer()
On Error Resume Next
Application.OnTime dTime, "ValueStore", Schedule:=False
End Sub
Sub ResetTimer()
With Sheets("VolCalculation")
.Range("D2:XFD3").ClearContents
End With
End Sub
Sub AutoOn()
Call StartTimer
End Sub
Sub AutoOff()
Call StopTimer
End Sub
Sub ConditionAuto()
aTime = Format(Now(), "HH:mm:ss")
OpenT1 = Format(TimeValue("08:20:00"), "HH:mm:ss")
BreakT = Format(TimeValue("08:22:00"), "HH:mm:ss")
OpenT2 = Format(TimeValue("08:35:00"), "HH:mm:ss")
CloseT = Format(TimeValue("08:36:00"), "HH:mm:ss")
If aTime >= OpenT1 And aTime <= BreakT Then
Call AutoTimer1
ElseIf aTime > BreakT And aTime < OpenT2 Then
Call AutoTimer2
ElseIf aTime >= OpenT2 And aTime <= CloseT Then
Call AutoTimer3
Else
Call AutoTimer0
End If
End Sub
Sub AutoTimer0()
OpenT1 = Format(TimeValue("08:20:00"), "HH:mm:ss")
BreakT = Format(TimeValue("08:22:00"), "HH:mm:ss")
OpenT2 = Format(TimeValue("08:35:00"), "HH:mm:ss")
CloseT = Format(TimeValue("08:36:00"), "HH:mm:ss")
Application.OnTime OpenT1, "AutoOn", Schedule:=True
On Error Resume Next
Application.OnTime OpenT1 + TimeValue("00:00:01"), "AutoOn", Schedule:=False
Application.OnTime BreakT, "AutoOff"
Application.OnTime OpenT2, "AutoOn", Schedule:=True
On Error Resume Next
Application.OnTime OpenT2 + TimeValue("00:00:01"), "AutoOn", Schedule:=False
Application.OnTime CloseT, "AutoOff"
End Sub
Sub AutoTimer1()
aTime = Now()
bTime = Now + TimeValue("00:00:01")
BreakT = Format(TimeValue("08:22:00"), "HH:mm:ss")
OpenT2 = Format(TimeValue("08:35:00"), "HH:mm:ss")
CloseT = Format(TimeValue("08:36:00"), "HH:mm:ss")
Application.OnTime aTime, "AutoOn", Schedule:=True
On Error Resume Next
Application.OnTime bTime, "AutoOn", Schedule:=False
Application.OnTime BreakT, "AutoOff"
Application.OnTime OpenT2, "AutoOn", Schedule:=True
On Error Resume Next
Application.OnTime OpenT2 + TimeValue("00:00:01"), "AutoOn", Schedule:=False
Application.OnTime CloseT, "AutoOff"
End Sub
Sub AutoTimer2()
OpenT2 = Format(TimeValue("08:35:00"), "HH:mm:ss")
CloseT = Format(TimeValue("08:36:00"), "HH:mm:ss")
Application.OnTime OpenT2, "AutoOn", Schedule:=True
On Error Resume Next
Application.OnTime OpenT2 + TimeValue("00:00:01"), "AutoOn", Schedule:=False
Application.OnTime CloseT, "AutoOff"
End Sub
Sub AutoTimer3()
aTime = Now()
bTime = Now + TimeValue("00:00:01")
CloseT = Format(TimeValue("08:36:00"), "HH:mm:ss")
Application.OnTime aTime, "AutoOn", Schedule:=True
On Error Resume Next
Application.OnTime bTime, "AutoOn", Schedule:=False
Application.OnTime CloseT, "AutoOff"
End Sub