Application.Caller when clicking a shape

Av8tordude

Well-known Member
Joined
Oct 13, 2007
Messages
1,074
Office Version
  1. 2019
Platform
  1. Windows
I have this code that I want to run that checks the days and times are within these parameters. I currently have it set when the sheet activates and when I click a shape button (Named: rStats)
When I click the button, it triggers a msgbox, which is to my expectation, however, when the sheet activates, the msgbox is triggered, which is not what I want.. I only want the msgbox to trigger if the shape is clicked.

VBA Code:
On Error Resume Next
If ActiveSheet.Shapes(Application.Caller).Name = "sStats" Then MsgBox "Stop"
On Error GoTo 0

If Weekday(Now()) = vbSunday Or Weekday(Now()) = vbSaturday Or _
    TimeValue(Now) < TimeValue("09:30:00 AM") - TimeSerial(2, 0, 0) Or _
        TimeValue(Now) > TimeValue("04:00:00 PM") - TimeSerial(2, 0, 0) Then Exit Sub
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
VBA Code:
Sub sStats_Click()
'Whatever code you want to run
End Sub
 
Upvote 0
Not sure I understand what you're suggesting. The timeframe needs to check when either the button is clicked or the sheet is activated. However, the msgbox should only trigger when I click the button. It should not trigger the msgbox when the sheet is activated.
 
Upvote 0
From you OP
" I only want the msgbox to trigger if the shape is clicked."
If you have other conditions that weren't expressed please let me know. It would also help to post the whole subroutine.
VBA Code:
Sub sStats_Click()
MsgBox "Stop"
End Sub
 
Upvote 0
Maybe I gave a bad explanation. When the sheet is activated, the code will run (Name: Sub Macro) to check if the current time is within the set parameters. If so, it will run the rest of the code, if not, do nothing (no msgbox). Like wise, if I click the button that is on the sheet (which has the same Sub Macro code assigned to the button), it too will use the same code to check the time is within parameters. Only difference is, if I click the button, it should check the time parameters and if the current time is not the parameters run the rest of the code. If not, a msgbox should trigger when I click the button, and the code stops.
 
Upvote 0
Maybe I gave a bad explanation. When the sheet is activated, the code will run (Name: Sub Macro) to check if the current time is within the set parameters. If so, it will run the rest of the code, if not, do nothing (no msgbox). Like wise, if I click the button that is on the sheet (which has the same Sub Macro code assigned to the button), it too will use the same code to check the time is within parameters. Only difference is, if I click the button, it should check the time parameters and if the current time is not the parameters run the rest of the code. If not, a msgbox should trigger when I click the button, and the code stops.
Show full code for more assistance.
 
Upvote 0
I don't know man... try this.
VBA Code:
On Error Resume Next
If ActiveSheet.Shapes(Application.Caller).Name = "sStats" Then
 MsgBox "Stop"
Exit Sub
On Error GoTo 0

If Weekday(Now()) = vbSunday Or Weekday(Now()) = vbSaturday Or _
    TimeValue(Now) < TimeValue("09:30:00 AM") - TimeSerial(2, 0, 0) Or _
        TimeValue(Now) > TimeValue("04:00:00 PM") - TimeSerial(2, 0, 0) Then Exit Sub
 
Upvote 0
Kinda crude, but I put something...


VBA Code:
On Error GoTo here
If Weekday(Now()) = vbSunday Or Weekday(Now()) = vbSaturday Or _
    TimeValue(Now) < TimeValue("09:30:00 AM") - TimeSerial(2, 0, 0) Or _
        TimeValue(Now) > TimeValue("04:00:00 PM") - TimeSerial(2, 0, 0) Then
    If ActiveSheet.Shapes(Application.Caller).Name = "rStats" Then MsgBox "Stop"
here:
Exit Sub
End If
 
Upvote 0
I try not to use On Error statements unless I'm expecting an error. I would suggest removing those statements and seeing where the code errors.
 
Upvote 0

Forum statistics

Threads
1,215,327
Messages
6,124,289
Members
449,149
Latest member
mwdbActuary

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