keep macro running

pawanhsharma

Board Regular
Joined
Feb 12, 2013
Messages
64
Hello Everyone, does anybody have a solution for me, I have below code which record Idle time. it work fine only if I am working in this file only. if I move to oter file or application it stops recording the time.

Private Type LASTINPUTINFO
cbSize As Long
dwTime As Long
End Type
Private Declare Sub GetLastInputInfo Lib "user32" (ByRef plii As LASTINPUTINFO)
Private Declare Function GetTickCount Lib "kernel32" () As Long

Public Sub IdleTime()
Dim a As LASTINPUTINFO
a.cbSize = LenB(a)
GetLastInputInfo a
IdleTicks = 5

Counter_Flag = ThisWorkbook.Sheets(1).Cells(1, 5)
If Counter_Flag = "" Then Counter_Flag = 1
idleT = ((GetTickCount - a.dwTime) / 1000)
If idleT > IdleTicks Then
If Counter_Flag = 1 Then
ThisWorkbook.Sheets(1).Cells(1, 4) = ThisWorkbook.Sheets(1).Cells(1, 4) + 1
RowIdx = ThisWorkbook.Sheets(1).Cells(1, 4)
ThisWorkbook.Sheets(1).Cells(RowIdx, 3) = "Idle"
ThisWorkbook.Sheets(1).Cells(1, 5) = 0
ThisWorkbook.Sheets(1).Cells(RowIdx, 2).NumberFormat = "m/d/yyyy h:mm:ss"
ThisWorkbook.Sheets(1).Cells(RowIdx, 2) = VBA.Now
ThisWorkbook.Sheets(1).Cells(RowIdx, 1) = idleT
End If
End If

If idleT < IdleTicks Then
If Counter_Flag = 0 Then
ThisWorkbook.Sheets(1).Cells(1, 4) = ThisWorkbook.Sheets(1).Cells(1, 4) + 1
RowIdx = ThisWorkbook.Sheets(1).Cells(1, 4)
ThisWorkbook.Sheets(1).Cells(RowIdx, 3) = "Active"
ThisWorkbook.Sheets(1).Cells(1, 5) = 1
ThisWorkbook.Sheets(1).Cells(RowIdx, 2).NumberFormat = "m/d/yyyy h:mm:ss"
ThisWorkbook.Sheets(1).Cells(RowIdx, 2) = VBA.Now
ThisWorkbook.Sheets(1).Cells(RowIdx, 1) = idleT
End If
End If

Application.OnTime Now + TimeValue("00:00:05"), "IdleTime"
End Sub
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"

Forum statistics

Threads
1,223,911
Messages
6,175,326
Members
452,635
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top