VBA Formula Not Removing 30 Minutes From Time

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,564
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I am looking for some help with the following vba formula ...
VBA Code:
mm2 = DateAdd("h", -0.5, sh.Cells(rw, 6))

I am trying to get a time value (mm2) that is 30 minutes less the time value in sh.cells(rw,6). This code isn't doing it.

The value in sh.Cells(rw,6) = 46061.8541666667 (2023-05-15 8:30 PM)
The resulting value of mm2 = 2023-05-15 8:30 PM

I was hoping to get mm2 = 2023-05-15 8:00 PM
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
While waiting for other experts, I will share my thought here.

Book1
BC
12026-02-08 20:302026-02-08 20:00
Sheet3


VBA Code:
Sub minutetest()
time1 = CDate([b1].Value)

mm2 = time1 - TimeSerial(0, 30, 0)

[c1].Value = mm2
End Sub

how about
mm2 =sh.Cells(rw, 6) - TimeSerial(0, 30, 0)
 
Upvote 0
.. or
VBA Code:
mm2 = sh.Cells(rw, 6) - 1 / 48
 
Upvote 0
@Ark68 The date and time you are expecting are not going to happen with the value that you posted. ;)
 
Upvote 0
... This code isn't doing it...
That's because the description of the DateAdd Function says: If number isn't a Long value, it is rounded to the nearest whole number before being evaluated.

The following does work:
VBA Code:
mm2 = DateAdd("n", -30, sh.Cells(rw, 6))
 
Upvote 0
Solution

Forum statistics

Threads
1,215,686
Messages
6,126,202
Members
449,298
Latest member
Jest

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