Hello stanigator,
Here is simple class. Hopefully this will clearly illustrate the basic components, and structure of a class module.
Add a Class Module to project and rename it StopWatchCls. The class contains 4 properties: EventID, Hours, Minutes, Seconds. EventID is a read/write property, while the others are read only. There are 3 methods: StartWatch, StopWatch, and Elapsed. Only Elapsed returns a value. It returns the elapsed time as string formatted in "HH:MM:SS".
Class Module Code
'Stop Watch Class -
' Properties: Hours, Minutes, and Seconds
' Methods: Start, Stop, and Elapsed
'-----------------------------------------'
' Declare Private Variables for Class use '
'-----------------------------------------'
Dim Hrs As Long
Dim Mins As Long
Dim Secs As Long
Dim StartTime As Double
Dim StopTime As Double
Dim ET As Double
Dim pvtEventID As String
'-------------------------------'
' Public Properties - Read Only '
'-------------------------------'
Public Property Get Hours() As Long
Hours = Hrs
End Property
Public Property Get Minutes() As Long
Minutes = Mins
End Property
Public Property Get Seconds() As Long
Seconds = Secs
End Property
'-------------------------------'
' Public Property - Read/Write '
'-------------------------------'
Public Property Get EventID() As String
EventID = pvtEventID
End Property
Public Property Let EventID(Event_ID As String)
pvtEventID = Event_ID
End Property
'----------------'
' Public Methods '
'----------------'
Public Sub StartWatch()
StartTime = Now()
Hrs = 0
Mins = 0
Secs = 0
End Sub
Public Sub StopWatch()
StopTime = Now()
End Sub
Public Function Elapsed() As String
ET = StopTime - StartTime
Elapsed = Format(ET, "hh:mm:ss")
ET = ET / (1 / 86400) 'Elapsed time is stored in seconds
Hrs = Int(ET / 3600)
Mins = Int(ET / 60)
Secs = ET Mod 60
End Function
StopWatch Example
Place this code in a Standard VBA module
Sub StopWatchTest()
Dim I As Long, J As Long
Dim H As Long, M As Long, S As Long
Dim ID As String
Dim MyStopWatch As New StopWatchCls
Dim X As String
MyStopWatch.StartWatch
For I = 1 To 5000
For J = 1 To 256
n = n + Val(Cells(I, J))
Next J
Next I
MyStopWatch.StopWatch
With MyStopWatch
.EventID = "Time Test 1"
X = .Elapsed
H = .Hours
M = .Minutes
S = .Seconds
ID = .EventID
End With
End Sub
Sincerely,
Leith Ross
Bookmarks