Looking for help in Excel Macros scripts

Adnan199

New Member
Joined
Dec 9, 2024
Messages
1
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
Dear expert,
I have a problem to solve
I have written a script in Macros which will monitor a column value let's say C14 is column, if value get changes after 1 minute its fine if not then restart workbook. this script work properly for few days but after that script gets stuck and monitoring gets stop . for this i have implemented a solution i have also written script in another module Module7 which never stops triggering i have made call on application on time to module7 to ensure if my monitoring script is running or not if not running then start otherwise stop existing one and re initialized monitoring. and its not detecting if module2 start monitoring is running or not . i am stuck because of this
monitoring code:
VBA Code:
Dim previousValue As Variant
Dim logFilePath As String
Dim checkInterval As Double

Sub StartMonitoring()
    ' Initialize monitoring
    previousValue = ThisWorkbook.Sheets(1).Range("C14").Value
    logFilePath = GenerateLogFilePath() ' Set daily log file path
    checkInterval = 1
    Application.OnTime Now + TimeValue("00:" & checkInterval & ":00"), "CheckValue"
End Sub

Sub CheckValue()
    Dim currentValue As Variant
    Dim cell As Range
    Dim ws As Worksheet

    On Error GoTo ErrorHandler ' Error handler for unexpected issues
   
    ' Get current value from cell C14
    currentValue = ThisWorkbook.Sheets(1).Range("C14").Value

    ' Check if the current value is empty or invalid
    If IsEmpty(currentValue) Or currentValue = "" Then
        LogUpdate "Error: C14 is empty at " & Now
        currentValue = 0
    End If

    ' Compare current value with previous value
    If currentValue <> previousValue Then
        LogUpdate "C14 Previous Value: " & previousValue & " Updated to: " & currentValue & " at " & Now
        previousValue = currentValue ' Update the previous value
    Else
        LogUpdate "Reopening Workbook due to Error: C14 did not update at " & Now
        ReOpenWorkbook ' Reopen the workbook
        previousValue = ThisWorkbook.Sheets(1).Range("C14").Value ' Update previous value after reopening
        Exit Sub
    End If

    ' Schedule the next check
    Application.OnTime Now + TimeValue("00:" & checkInterval & ":00"), "CheckValue"
    Exit Sub

ErrorHandler:
    ' Log unexpected errors and attempt recovery
    LogUpdate "Error occurred in CheckValue: " & Err.Description & " at " & Now
    'ReOpenWorkbook
    Exit Sub
End Sub


Sub StopMonitoring()
    On Error Resume Next
    Application.OnTime Now + TimeValue("00:00:10"), "CheckValue", , True
    On Error GoTo 0
End Sub

Sub ReOpenWorkbook()
    Dim filePath As String

    filePath = "D:\MOL Pakistan\DataReadSheet.xls"
    Application.DisplayAlerts = False
    Application.AskToUpdateLinks = False

    On Error Resume Next
    Application.OnTime Now + TimeValue("00:00:20"), "StartMonitoring"
    Workbooks.Open filePath
End Sub

Sub LogUpdate(message As String)
    Dim logFile As Integer
    logFilePath = GenerateLogFilePath() ' Update log file path daily
    On Error GoTo ErrorHandler
    logFile = FreeFile
    Open logFilePath For Append As #logFile
    Print #logFile, message
    Close #logFile
    Exit Sub

ErrorHandler:
    LogError "Error Writing to logfile: " & Err.Description & " at " & Now
    If logFile > 0 Then Close #logFile
End Sub

Sub LogError(message As String)
    Dim logFile As Integer
    logFilePath = GenerateLogFilePath() ' Update log file path daily
    On Error GoTo ErrorHandler
    logFile = FreeFile
    Open logFilePath For Append As #logFile
    Print #logFile, message
    Close #logFile
    Exit Sub

ErrorHandler:
    If logFile > 0 Then Close #logFile
End Sub

Function GenerateLogFilePath() As String
    ' Generates a log file path based on the current date
    Dim dateStr As String
    dateStr = Format(Date, "YYYYMMDD")
    GenerateLogFilePath = "D:\MOL Pakistan\monitor_log_" & dateStr & ".txt"
End Function

Private Sub Workbook_Open()
Application.OnTime Now + TimeValue("00:00:05"), "StartMonitoring"
End Sub


And
ensuring if monitring scrit is runiing or not code below


VBA Code:
Sub EnsureMonitoringRestarted()
    Dim isScheduled As Boolean
     MsgBox "In Ensure method", vbInformation, "Monitoring Active"
    On Error Resume Next ' Prevent runtime errors
   
      
'Application.OnTime EarliestTime:=Now, Procedure:="Module3.StartMonitoring", Schedule:=False
'Application.OnTime EarliestTime:=Now, Procedure:="Module3.CheckValue", Schedule:=False
    ' Start fresh monitoring
    Call Module3.CheckValue
    MsgBox "Monitoring restarted successfully.", vbInformation, "Monitoring Active"
End Sub
 
Last edited by a moderator:

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK

Forum statistics

Threads
1,225,467
Messages
6,185,149
Members
453,279
Latest member
MelissaOsborne

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