select different cells depening on the time of day?

danbates

Active Member
Joined
Oct 8, 2017
Messages
377
Office Version
  1. 2016
Platform
  1. Windows
Hi,

Please can someone help me?

I would like a vba code that selects cell D1 if the time is between 05:40 and 17:40,
and if the time is between 17:41 and 05:39 then it selects E1.

thnaks

Dan
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Hi,

You could test following :

Code:
Sub TestT()
  If Now - Int(Now) > 0.2361 And Now - Int(Now) < 0.7361 Then
      MsgBox Range("D1").Value
  Else
      MsgBox Range("E1").Value
  End If
End Sub

Hope this will help
 
Upvote 0
It is no better than what James006 has posted but might be a bit easier if you ever have to amend the times to use Timevalue...

Code:
Sub TestT2()
  If Now - Int(Now) > TimeValue("05:40") And Now - Int(Now) < TimeValue("17:40") Then
      MsgBox Range("D1").Value
  Else
      MsgBox Range("E1").Value
  End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,495
Messages
6,125,149
Members
449,208
Latest member
emmac

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