ต้องการให้เวลาในuserformเปลี่ยนตามเครื่องค่ะ
Posted: Fri Aug 28, 2020 12:10 pm
Code: Select all
Dim MyTime
Me.TextBox9 = DateTime.Now
alerttime = Now + TimeValue("00:00:01")
คลังคำตอบแห่งความรู้จากคนไทย เพื่อโลกที่ต้องการเข้าใจในสิ่งเล็ก ๆ อย่างลึกซึ้ง
https://snasui.com/
Code: Select all
Dim MyTime
Me.TextBox9 = DateTime.Now
alerttime = Now + TimeValue("00:00:01")
ต้องการให้เวลาในuserformแสดงเวลาตามเครื่องค่ะตอนนี้แสดงเวลาแล้วแต่ไม่ตามเครื่องค่ะthinatda wrote: Fri Aug 28, 2020 12:10 pmCode: Select all
Dim MyTime Me.TextBox9 = DateTime.Now alerttime = Now + TimeValue("00:00:01")
Code: Select all
Sub UserForm_Initialize()
Call StartTimer
End Sub
Private Sub UserForm_Terminate()
Call EndTimer
End SubCode: Select all
#If VBA7 Then
Public Declare PtrSafe Function SetTimer Lib "user32" ( _
ByVal HWnd As Long, ByVal nIDEvent As LongLong, _
ByVal uElapse As Long, ByVal lpTimerFunc As LongLong) As Long
Public Declare PtrSafe Function KillTimer Lib "user32" ( _
ByVal HWnd As Long, ByVal nIDEvent As Long) As Long
#Else
Public Declare Function SetTimer Lib "user32" ( _
ByVal HWnd As Long, ByVal nIDEvent As Long, _
ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
Public Declare Function KillTimer Lib "user32" ( _
ByVal HWnd As Long, ByVal nIDEvent As Long) As Long
#End If
Public TimerID As Long, TimerSeconds As Single, tim As Boolean
Dim Counter As Long
'~~> Start Timer
Sub StartTimer()
'~~ Set the timer for 1 second
TimerSeconds = 1
TimerID = SetTimer(0&, 0&, TimerSeconds * 1000&, AddressOf TimerProc)
End Sub
'~~> End Timer
Sub EndTimer()
On Error Resume Next
KillTimer 0&, TimerID
End Sub
Sub TimerProc(ByVal HWnd As Long, ByVal uMsg As Long, _
ByVal nIDEvent As Long, ByVal dwTimer As Long)
'~~> Update value in Sheet 1
Sheet1.Range("A1").Value = Time ' <~~ โชว์ในเซลล์
UserForm1.Label1.Caption = Time ' <~~ โชว์ใน label
UserForm1.TextBox1.Text = Time ' <~~ โชว์ใน textbox
End Sub
ติดช่วงนี้ค่ะ ขึ้นแดงหมดเลยค่ะlogic wrote: Fri Aug 28, 2020 3:15 pm ลองอันนี้
ใส่ใน userformใส่ใน moduleCode: Select all
Sub UserForm_Initialize() Call StartTimer End Sub Private Sub UserForm_Terminate() Call EndTimer End Subต้นแหล่ง ~~> https://stackoverflow.com/questions/118 ... k-in-excelCode: Select all
#If VBA7 Then Public Declare PtrSafe Function SetTimer Lib "user32" ( _ ByVal HWnd As Long, ByVal nIDEvent As LongLong, _ ByVal uElapse As Long, ByVal lpTimerFunc As LongLong) As Long Public Declare PtrSafe Function KillTimer Lib "user32" ( _ ByVal HWnd As Long, ByVal nIDEvent As Long) As Long #Else Public Declare Function SetTimer Lib "user32" ( _ ByVal HWnd As Long, ByVal nIDEvent As Long, _ ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long Public Declare Function KillTimer Lib "user32" ( _ ByVal HWnd As Long, ByVal nIDEvent As Long) As Long #End If Public TimerID As Long, TimerSeconds As Single, tim As Boolean Dim Counter As Long '~~> Start Timer Sub StartTimer() '~~ Set the timer for 1 second TimerSeconds = 1 TimerID = SetTimer(0&, 0&, TimerSeconds * 1000&, AddressOf TimerProc) End Sub '~~> End Timer Sub EndTimer() On Error Resume Next KillTimer 0&, TimerID End Sub Sub TimerProc(ByVal HWnd As Long, ByVal uMsg As Long, _ ByVal nIDEvent As Long, ByVal dwTimer As Long) '~~> Update value in Sheet 1 Sheet1.Range("A1").Value = Time ' <~~ โชว์ในเซลล์ UserForm1.Label1.Caption = Time ' <~~ โชว์ใน label UserForm1.TextBox1.Text = Time ' <~~ โชว์ใน textbox End Sub
Code: Select all
#If VBA7 Then
Public Declare PtrSafe Function SetTimer Lib "user32" ( _
ByVal HWnd As Long, ByVal nIDEvent As LongLong, _
ByVal uElapse As Long, ByVal lpTimerFunc As LongLong) As Long
Public Declare PtrSafe Function KillTimer Lib "user32" ( _
ByVal HWnd As Long, ByVal nIDEvent As Long) As Long
#Else
Public Declare Function SetTimer Lib "user32" ( _
ByVal HWnd As Long, ByVal nIDEvent As Long, _
ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
Public Declare Function KillTimer Lib "user32" ( _
ByVal HWnd As Long, ByVal nIDEvent As Long) As Long
#End If
โค้ดใส่ไว้ในชื่อ รายรับ1 กับ ชื่อ Module1 ค่ะ
Code: Select all
Private Sub UserForm_Initialize()
Call Start_Timer
End Sub
Private Sub UserForm_Terminate()
Call Stop_Timer
End SubCode: Select all
Dim TimerActive As Boolean
Sub StartTimer()
Start_Timer
End Sub
Public Sub Start_Timer()
TimerActive = True
Application.OnTime Now() + TimeValue("00:00:01"), "Timer"
End Sub
Public Sub Stop_Timer()
TimerActive = False
End Sub
Private Sub Timer()
If TimerActive Then
' ActiveSheet.Cells(1, 1).Value = Time
รายรับ1.Label18.Caption = Time
Application.OnTime Now() + TimeValue("00:00:01"), "Timer"
End If
End Subติดเหลืองค่ะlogic wrote: Mon Aug 31, 2020 10:27 am ลองอันนี้แทนครับ จากแหล่งเดิม
ใน UserFormใน ModuleCode: Select all
Private Sub UserForm_Initialize() Call Start_Timer End Sub Private Sub UserForm_Terminate() Call Stop_Timer End SubCode: Select all
Dim TimerActive As Boolean Sub StartTimer() Start_Timer End Sub Public Sub Start_Timer() TimerActive = True Application.OnTime Now() + TimeValue("00:00:01"), "Timer" End Sub Public Sub Stop_Timer() TimerActive = False End Sub Private Sub Timer() If TimerActive Then ' ActiveSheet.Cells(1, 1).Value = Time รายรับ1.Label18.Caption = Time Application.OnTime Now() + TimeValue("00:00:01"), "Timer" End If End Sub
Code: Select all
Private Sub UserForm_Terminate()
Call EndTimer
End Sub
แก้ได้แล้วค่ะ ขอบคุณค่ะlogic wrote: Mon Aug 31, 2020 12:16 pm โค้ดที่ยกมาผมเขียนไปแบบหนึ่งเอาไปใช้เป็นอีกแบบหนึ่ง ติดก็ไม่แปลกครับ![]()