View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
Auric__ Auric__ is offline
external usenet poster
 
Posts: 538
Default How to obtain the transit time

Paul wrote:

thanks for the answer.


Don't thank me.

I got the code backward, if you can believe that. I thought you needed the
difference calculated between A & B, with the result inserted in A20. Upon
rereading your original post, I see that A20 is set by you and you just
want to add that value to A and put the result of *that* into B, right?
Goddammit. I'm not sure *how* I got it backwards.

(Also, what I posted before doesn't accurately take weekends into account.)

The reason of Vba and not a function is due to the fact that sometimes I
need to modify by hand the transit time. If the goods are late from the
supplier with 2 drivers I reduce the transit time.
So if there a function/formula in the cell, and I need to change the
result, it will cancel for the next use of the worksheet


Gotcha. The formula I posted wouldn't work anyway.

One more question if I am allowed.
Is possibile to run automatically the sub when I insert the date from
A30 down to A60?


Here's the code to do *what you actually need*, assuming I'm not getting
something else back-asswards. It will run any time *anything* is changed,
but will quickly exit if what was changed is outside the range A30:A60.
Just to clarify, this adds A20 to A30:A60 and puts the results in B30:B60.

Put this in the sheet's object in the VBA editor:

Private working As Boolean

Private Sub Worksheet_Change(ByVal Target As Range)
If working Then Exit Sub
Dim chk As Range, x As Date, deliveryTime As Long
Set chk = Intersect(Target, Range("A30:A60"))
If Not (chk Is Nothing) Then
deliveryTime = Range("A20").Value
working = True
For L0 = 30 To 60
If IsDate(Cells(L0, 1).Value) Then
x = Cells(L0, 1).Value + deliveryTime
If 1 = Weekday(Cells(L0, 1).Value) Then x = x - 1
If (x = Date) Then
If (deliveryTime 4) Or _
((Weekday(x) <= Weekday(Cells(L0, 1).Value)) And _
(deliveryTime 0)) Then
x = x + (((deliveryTime \ 7) + 1) * 2)
End If
Select Case Weekday(x)
Case 1, 7
Cells(L0, 2).Value = x + 2
Case Else
Cells(L0, 2).Value = x
End Select
End If
End If
Next
working = False
End If
Set chk = Nothing
End Sub

This *seems* to work, for me at least, in a few minutes of testing. It even
gives accurate dates if something is marked as having been shipped on the
weekend. (I'm assuming that if someone sends something out on a Saturday or
Sunday, it doesn't actually start moving until Monday, which is considered
day zero in such cases. If that's a bad assumption, the code will need
editing.)

--
I disapprove of what you say,
but I will defend to the death your right to say it.
-- Evelyn Beatrice Hall, summarizing Voltaire's philosophy