Macro button to work between specific times and dates

Pauljj

Well-known Member
Joined
Mar 28, 2004
Messages
2,046
Hi All

I want a macro which is currently controlled by a button, to only work on specific dates throughout the year, ie

3 and 4 June
6 and 7 July
21 and 22 August


and so on, combined with that, I need it to work from 1pm on the first day to 6 pm on the last..........so 1pm on the 3rd June to 6pm on the 4th June etc etc

Is there a practical way to go about this.

I had throught about setting up a table in excel with the dates and times then having the macro check if the computer time fits with any of those dates/times

Does anyone have any bright ideas ?
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
If you create a date table in 'Sheet1' cells 'A1:B4' then this sort of thing will work. Dates in the table must be true dates not text.

Code:
Sub aaaa()

Dim myDay As Double
Dim mySheet As Worksheet
Dim myNum As Integer

myDay = Int(Now)
Set mySheet = Sheets("Sheet1") 'set sheet as correct sheet name of date table

myNum = WorksheetFunction.CountIfs(mySheet.Range("A1:A100"), "<=" & myDay, mySheet.Range("B1:B100"), ">=" & myDay)

If myNum = 0 Then 'exit if dates not in range
    MsgBox "Dates not in range. Macro exits"
    Exit Sub
End If

MsgBox "Macro Continues" 'continue macro if dates ok

End Sub
 
Upvote 0

Forum statistics

Threads
1,203,242
Messages
6,054,350
Members
444,718
Latest member
r0nster

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