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

danbates

Active Member
Joined
Oct 8, 2017
Messages
357
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

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.

WhoCanDo

Board Regular
Joined
Dec 18, 2007
Messages
183
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.
 

danbates

Active Member
Joined
Oct 8, 2017
Messages
357
Office Version
  1. 2016
Platform
  1. Windows
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
 

Watch MrExcel Video

Forum statistics

Threads
1,129,515
Messages
5,636,793
Members
416,941
Latest member
shazzaxyz

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
Top