Speak a sentence if time condition is met

chrisgarcia78

New Member
Joined
Jun 15, 2024
Messages
31
Office Version
  1. 2021
Platform
  1. Windows
Hi, I need a VBA to speak a sentence if cell A1 time is 9:59:59 minutes after current time.

Thank you
 
Last edited:

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
I don't know what you're after and how do you expect this to work.
Here are two options:
VBA Code:
Option Explicit

Private Const deltaText As String = "09:59:59"

Sub runOnTime()
    Dim timeNow As Variant, timeCell As Date, delta As Date
    Dim sh As Worksheet
    Set sh = ActiveSheet
    timeCell = sh.Range("A1").Value
    delta = TimeValue(deltaText)
    If Not IsDate(timeCell) Then Exit Sub
    If timeCell > 1 Then
        timeNow = Now()
    Else
        timeNow = Time()
    End If
    If timeNow + TimeValue(deltaText) = timeCell Then
        saySomething sh.Range("B1")
    Else
        Debug.Print "time difference is: " & CDate(timeCell - timeNow),
        If timeCell < timeNow + delta Then
            MsgBox "Time slot missed.", vbOKOnly + vbExclamation
        Else
            MsgBox "try again in " & CDate(timeCell - delta - timeNow), vbOKOnly + vbInformation
        End If
    End If
End Sub

Sub saySomething(ByVal something As String)
    Application.Speech.Speak something
End Sub

Sub scheduleSpeech()
    Dim timeCell As Date, delta As Date, runTime As Date
    Dim sh As Worksheet
    Set sh = ActiveSheet
    timeCell = sh.Range("A1").Value
    delta = TimeValue(deltaText)
    If Not IsDate(timeCell) Then Exit Sub
    If timeCell < 1 Then
        runTime = CDate(Date + timeCell - delta)
    Else
        runTime = CDate(timeCell + delta)
    End If
    Debug.Print runTime
    If runTime < Now Then
        MsgBox "Scheduling time has passed.", vbOKOnly + vbInformation
    Else
        MsgBox "Speech scheduled for " & runTime
        Application.OnTime runTime, "'saysomething """ & sh.Range("B1").Value & """'"
    End If
End Sub
put the sentence you want to speak in cell B1.
procedure runOnTime() does as your question asks - you run it and then it either speaks or not.
procedure scheduleSpeech() schedules a speech for 09:59:59 before the time in cell A1.
 
Upvote 0

Forum statistics

Threads
1,224,552
Messages
6,179,488
Members
452,917
Latest member
MrsMSalt

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