VBA MsgBox question

Pinaceous

Well-known Member
Joined
Jun 11, 2014
Messages
1,124
Office Version
  1. 365
Platform
  1. Windows
Hi All,

How do you run a timed pop up to pop up on certain opened tabs of your workbook?

For example, if I'm running
Code:
YourSub1
to execute at ie. 12:00pm I want it exclusive for sheet 1 and no other tabs.

Thank you all in advance!

R/
Pinaceous
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Not certain what you mean by
exclusive for sheet1

This code (paste into ThisWorkbook module) will initiate the "timer" when the workbook is first opened:

Code:
Private Sub Workbook_Open()


    Application.OnTime TimeValue("12:00:00"), "MyMacro"   '<-- create another macro in a Routine Module called MyMacro
    
     'in the MyMacro you'll create code for the MsgBox.


End Sub


Code:
Sub MyMacro()
     MsgBox"This is your message"
End Sub
 
Last edited:
Upvote 0
Hello Logit,

Many thanks for your reply!

Can the time be adjusted for the
Code:
MyMacro
code to work only between for example Midnight and 12PM?

For example;

Code:
Application.OnTime TimeValue("00:00:01"), "MyMacro"
to
Code:
Application.OnTime TimeValue("12:00:00"), "MyMacro
??

Thank you!
Pinaceous
 
Upvote 0
Would this work ??



Code:
Application.OnTime TimeValue("0:00:01"), , TimeValue("12:00:00"), "MyMacro"
 
Last edited:
Upvote 0
.
I believe your questions are leading to a larger goal.

Please describe what it is you are ultimately attempting to do. This will avoid going back and forth and get to the heart of the matter.
 
Upvote 0
Hi Logit,

Your very perceptive. What I'm trying to do is provide for a button to execute a specific macro where when clicked it will execute one macro1 only between midnight to 12pm and a different macro2 from 12:01pm to 23:59.

I was reading up on this sort of application and would like it for what I'm working on.

Thanks,
Pinaceous
 
Upvote 0
.
This is one method to accomplish your goal :

Paste into a Routine Module:

Code:
Option Explicit


Public Function InRange(startTime As Date, endTime As Date) As Boolean


    Dim dtNow As Date


    dtNow = Now


    Select Case TimeValue(dtNow)
        Case Is > TimeValue(startTime)
            If TimeValue(endTime) > TimeValue(dtNow) Then
                InRange = True
            End If
        Case Is > TimeValue(startTime)
            If TimeValue(endTime) > TimeValue(dtNow) Then
                InRange = True
            Else
                InRange = False
            End If
        Case Is < TimeValue(startTime) And (TimeValue(endTime) > TimeValue(dtNow))
                InRange = True
        Case Else
            InRange = False
    End Select


End Function


Sub WatTyme()
'If the time is between midnight and noon, the first macro will be run - otherwise any other time, the second macro is run.


    If InRange(CDate("00:00:01"), CDate("12:00:00")) Then
        Macro1
    Else
        Macro2
    End If


End Sub
 
Upvote 0
Logit!!!!!!!!!

That worked out fantastic!!

Many thanks for posting that code!

-Pinaceous
 
Upvote 0
.
You are most welcome. Thanks.
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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