Time difference in VBA vs Worksheet

lordriel

Board Regular
Joined
Nov 1, 2005
Messages
68
Office Version
  1. 365
Platform
  1. Windows
Greetings all - let me start out by saying that this board has been a lifesaver for me many times by pointing out things I should have already seen as well as by teaching me things I'd never had the chance to learn. I've spent the past few months retraining an aging VAXBasic brain to think in VBA...and have only been partially successful. Much of the progress is due primarily to everyone (questioners and answerers alike) on this board. Many thanks to all!

On to my question...

I have spent a lot of time reviewing posts concerning determining 'time elapsed' or 'time difference' and have yet to find one that pointedly gives a VBA solution. Currently I'm posting data to a worksheet and using the {=IF(D3>0,MOD(A3-A1,1),"")} and {=IF((A1>A2),(A1-A2),(A2-A1))} equations, then displaying the data on the userform and posting it in various other worksheets.

However, I'd prefer to have it coded in VBA and be free of the worksheet functions altogether. Is this possible, or am I relegated to bouncing the data back and forth?

Thanks!

LR
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Hi LR,

Dates are stored the same way in VBA as in Excel. The formulas and operators must be coded a bit differently, but all the same functionality is available in VBA as in Excel formulas.

For example, your formula

=IF((A1>A2),(A1-A2),(A2-A1))

in VBA could be written:

X =IIF( A1>A2, A1-A2, A2-A1 )

or

IF A1 > A2 Then X = A1 - A2 Else X = A2 - A1

except that now it is assumed by VBA that X, A1 and A2 are variables declared As Date, not range references. If you want them to refer to cells on the currently active worksheet the formula would be

X = IIF([A1]>[A2],[A1]-[A2],[A2]-[A1])

where the brackets EVALUATE the A1, A2 as range references.

In your other function the main difference is that the MOD function is actually a dyadic operator, so is written as x MOD y rather than MOD(x,y). Your equation in this case could be expressed in VBA:

X = IIF([D3]>0, ([A3]-[A1]) Mod 1, "")

but in this case X must be undeclared or declared As Variant so that it can accept a null string if D3's value should be <= 0.

I hope this helps.

Damon
 
Upvote 0
Thanks, Damon!


Dates are stored the same way in VBA as in Excel. The formulas and operators must be coded a bit differently, but all the same functionality is available in VBA as in Excel formulas.

I'll check it out, but it obviously will work. See what I mean about pointing out things I should have already known? :roll:
 
Upvote 0

Forum statistics

Threads
1,226,241
Messages
6,189,833
Members
453,573
Latest member
adefonzo23

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