Need a little help with my code: Run my code if time falls in category. Thank you for helping.

pedie

Well-known Member
Joined
Apr 28, 2010
Messages
3,875
If time in Cells(1, 1) falls between 1 PM - 1:45 AM next day then call macro1 else no.

Code:
Sub timeTRY()
 If Cells(1, 1).Value < "12:00" Then
  MsgBox "Time < 12 PM :)"
   Exit Sub
     Else
    Call Macro1
  End If
End Sub
 

Some videos you may like

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.

Ruddles

Well-known Member
Joined
Aug 24, 2010
Messages
5,786
Is it something like this?
Code:
Sub timeTRY()
 
  If Cells(1, 1) < Date + TimeValue("13:00:00") Then
    MsgBox "Too early"
  ElseIf Cells(1, 1) > Date + 1 + TimeValue("01:45:00") Then
    MsgBox "Too late"
  Else
    MsgBox "In range"
  End If
 
End Sub
 

pedie

Well-known Member
Joined
Apr 28, 2010
Messages
3,875
a1 = 4:00:00 PM but the code does not work as it should/i thought should work...:biggrin:

msgbox "in range" does not show up:
 

Ruddles

Well-known Member
Joined
Aug 24, 2010
Messages
5,786
Are you running the macro by hand?

Is the date in there as well?
 

MickG

MrExcel MVP
Joined
Jan 9, 2008
Messages
14,841

ADVERTISEMENT

Hi, Possibly !!
Code:
If Date = "My Date" Then
Application.OnTime TimeValue("17:00:00"), "my_Procedure"
End If
Mick
 

pedie

Well-known Member
Joined
Apr 28, 2010
Messages
3,875
Code:
Private Sub Workbook_Open()
timeTRY
End Sub

Yes for now i am manually running it but latter it will as above.
in addition my issue...how can i structure this formula

=if(and(A1>A2,A1<A2),"Y","N")< p> by not putting it this way? but like the way you have done it???

Code:
Sub Macro1()
Range("B3").Select
    ActiveCell.FormulaR1C1 = _

        "=IF(AND(RC[-1]>R[1]C[-1],RC[-1].....<R[1]C[-1]),""Y"",""N"")"< p>     Range("B2").Select
End Sub
 
Last edited:

Ruddles

Well-known Member
Joined
Aug 24, 2010
Messages
5,786

ADVERTISEMENT

Yes for now i am manually running it but latter it will as above.
And cell A1 has a date in it as well as a time, yes?
 

pedie

Well-known Member
Joined
Apr 28, 2010
Messages
3,875
Hi, Possibly !!
Code:
If Date = "My Date" Then
Application.OnTime TimeValue("17:00:00"), "my_Procedure"
End If
Mick

Mike, thanks for suggestion but i want different user to manually run this code from their respective systems....however only between this time. 1:00 PM to 1:45 AM next day:

So running this code everyday @ 17hrs will create a problem.....
 

pedie

Well-known Member
Joined
Apr 28, 2010
Messages
3,875
And cell A1 has a date in it as well as a time, yes?

At the moment no date but if that can solve the problem then yes...there is Date and time 10/6/2010 12:00:00 AM format...:)

Thanks again!
 

Ruddles

Well-known Member
Joined
Aug 24, 2010
Messages
5,786
Hold on, let me check what you really want to do...

1) Check that the date/time in cell A1 falls between 13:00 on the day you open the workbook and 01:45 the following day?

2) Check that the date/time you open the workbook is before 01:45 or after 13:00 on the current day?
 

Watch MrExcel Video

Forum statistics

Threads
1,118,571
Messages
5,572,986
Members
412,493
Latest member
faram
Top