Is it possible to combine the date and time together via VBA?

danbates

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

I was wondering if it was possible to combine the date function and a time from 06:00 to 18:00 together?

This is my code:
VBA Code:
Sub DateandTime()
Dim D1 As Date
D1 = Date
With Sheets("Sheet1")
If .Range("A1") = D1 And Time > TimeValue("06:00:00") And Time < TimeValue("18:00:00") Then MsgBox "Yes"
End With
End Sub

I can get my desired result but I was wondering if it can be shortened so I could have the time included with the D1 or something similar?

Any help would be appreciated.

Thanks
Dan
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
if you don't Dim D1 then you can D1 = Date & " " & Time

However, you will create more lines of code testing the date and tome of D1 than your current code.
 
Upvote 0
Hi,

Thank you for you response.

If there isn't a shorter way of doing it then I will just carry on the way I was before.

Thanks again,
Dan
 
Upvote 0

Forum statistics

Threads
1,213,546
Messages
6,114,253
Members
448,556
Latest member
peterhess2002

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