calculate date diff in a cell

  • Thread starter Thread starter Brian Shafer
  • Start date Start date
B

Brian Shafer

Hi.
I have a spread sheet that contains in the first column a date time
formatted field, in a few columns over, I have a field that I would like to
automatically calculate the days between the current rows date field and the
previous rows date field and put the number of days different in the days
diff column. Can anyone help me with this...?
Thanks,
Brian Shafer
 
If I understand your question right you want to calculate the difference between A1(Jan 1st) and B1(May 30th)... if so just us B1-A1....

Not sure if that is what your looking for or if your looking for something else.
 
yes, you are right.. but I want it to happen automatically when I enter a
valid date in t b1 write the diff in b2
 
Private Sub Worksheet_Change(ByVal Target As Range)
Const WS_RANGE As String = "A1:H10"

On Error GoTo ws_exit:
Application.EnableEvents = False
If Target.Row = 1 And Target.Column > 1 Then
With Target
.Offset(1, 0).Value = .Value - .Offset(0, -1).Value
End With
End If

ws_exit:
Application.EnableEvents = True
End Sub

'This is worksheet event code, which means that it needs to be
'placed in the appropriate worksheet code module, not a standard
'code module. To do this, right-click on the sheet tab, select
'the View Code option from the menu, and paste the code in.



--
HTH

Bob Phillips

(remove nothere from email address if mailing direct)
 
Awesome... Thanks

Bob Phillips said:
Private Sub Worksheet_Change(ByVal Target As Range)
Const WS_RANGE As String = "A1:H10"

On Error GoTo ws_exit:
Application.EnableEvents = False
If Target.Row = 1 And Target.Column > 1 Then
With Target
.Offset(1, 0).Value = .Value - .Offset(0, -1).Value
End With
End If

ws_exit:
Application.EnableEvents = True
End Sub

'This is worksheet event code, which means that it needs to be
'placed in the appropriate worksheet code module, not a standard
'code module. To do this, right-click on the sheet tab, select
'the View Code option from the menu, and paste the code in.



--
HTH

Bob Phillips

(remove nothere from email address if mailing direct)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Back
Top