# Time difference in VBA vs Worksheet

#### lordriel

##### Board Regular
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

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
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.

=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

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?

Replies
6
Views
256
Replies
1
Views
264
Replies
3
Views
267
Replies
3
Views
236
Replies
2
Views
298

1,203,052
Messages
6,053,233
Members
444,648
Latest member
sinkuan85

### 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.

### Which adblocker are you using?

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

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