Macro Code for set time schedule

yncip

New Member
Joined
Jul 18, 2014
Messages
9
Hello everyone, I've been figuring out VB code to set out time schedule for each date.
Here is my raw table.

NoQtyDateTime
1218/8/2014
221219/8/2014
321119/8/2014
429419/8/2014
546319/8/2014
625319/8/2014
72119/8/2014
825019/8/2014
91120/8/2014
108820/8/2014
11220/8/2014
1294520/8/2014
1379020/8/2014
1412120/8/2014

<tbody>
</tbody>


and what I expect the table will shown after running the macro code is like this.
NoQtyDateTime
1218/8/20141:00
221219/8/20141:00
321119/8/20141:00
429419/8/20142:00
546319/8/20143:00
625319/8/20144:00
72119/8/20144:00
825019/8/20145:00
91120/8/20141:00
108820/8/20141:00
11220/8/20141:00
1294520/8/20142:00
1379020/8/20143:00
1412120/8/20144:00

<tbody>
</tbody>

so there is several several criteria to decide the time:
1. if the date is different then the time will start again from 1:00
2. if the quantity is more than 500 it will add an hour to fill the time.

i hope my explanation is clear and really looking forward for your help as I've been trying to find the code for days.

Here is the code i've been trying but it keeps on showing the wrong time.

Code:
Sub try()Dim i, j, k As Integer
Dim cum As Integer
rowz = Range("B6").End(xlDown).Row


Cells(7, 17).Value = ("1")


For i = 7 To rowz - 1
cum = Cells(i, 10)
k = 1
If Range("P" & i) <> Range("P" & i + 1) Then
Cells(i + 1, 17).Value = ("1")


    Else
    cum = cum + Cells(i + 1, 10)
    'k = k + 1
    j = 1


    
    Do While cum < 500
        Cells(i + j, 17) = k
        i = i + 1
        cum = cum + Cells(i + j, 10)
        j = j + 1


        Loop
k = k + 1


End If
Next i




End Sub

Thank you
 
Last edited:

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off

dermie_72

Well-known Member
Joined
Sep 4, 2012
Messages
1,540
I put 1 Hr (1:00) in G1 and 2 hrs (2:00) in G2,
then used a formula to get what you wanted instead of using a macro.
D1 will be 1:00 always, so that was manually entered.
D2 was
Code:
=IF(C2<>C1,$G$1,IF(B1>500,E1+$G$2,E1+$G$1))
then i copied down.

If you need this by macro, you can populate the range
Code:
range("E3:E" & rowz).formula = "=IF(C2<>C1,$G$1,IF(B1>500,E1+$G$2,E1+$G$1))"

hope that helps.
 
Upvote 0

WarPigl3t

Well-known Member
Joined
May 25, 2014
Messages
1,611
I would like to point out first that your dataSet doesn't make sense. You said that the time should increment by 1 hour if the quantity was over 500. That was one of your criteria. Well look at your row 4 for example. That row quantity is under 500 and yet your output in the Time column still incremented an hour. Guess you messed up there.

So now for my solution. I prefer to use Functions rather than Subroutines. I find them easier to code because I don't have to keep writing "Range.("A1").value" every 2 seconds. Start by entering this code into your VBA macros.
Code:
Function myFunction(myQty, myDate, myPrevDate, myPrevTime, myChngeVal)
    If myQty > myChngeVal _
    And myDate = myPrevDate Then
        leftTime = Split(myPrevTime, ":")
        myFunction = leftTime(0) + 1 & ":00"
    Else
        myFunction = myPrevTime
    End If
End Function

Now enter this formula into your second Time cell. I'm assuming it's cell D3. Don't use absolute references. Just copy paste exactly what I put below...
Code:
=myFunction(B2,C2,C1,D1,500)
Now just copy the formula down the column. Easy.
 
Last edited:
Upvote 0

WarPigl3t

Well-known Member
Joined
May 25, 2014
Messages
1,611
I put 1 Hr (1:00) in G1 and 2 hrs (2:00) in G2,
then used a formula to get what you wanted instead of using a macro.
D1 will be 1:00 always, so that was manually entered.
D2 was
Code:
=IF(C2<>C1,$G$1,IF(B1>500,E1+$G$2,E1+$G$1))
then i copied down.

If you need this by macro, you can populate the range
Code:
range("E3:E" & rowz).formula = "=IF(C2<>C1,$G$1,IF(B1>500,E1+$G$2,E1+$G$1))"

hope that helps.

Why didn't I think of that. Duh!
 
Upvote 0

yncip

New Member
Joined
Jul 18, 2014
Messages
9
Thanks for your answer.

I think I didn't explain it clearly, What i mean with point number 2, 500 is for the cumulative quantity not the quantity for each row.

That's what happened with point number 2 and 3 ; 6 and 7

NoQtyDateTime
1218/8/20141:00
221219/8/20141:00
321119/8/20141:00
429419/8/20142:00
546319/8/20143:00
625319/8/20144:00
72119/8/20144:00

<tbody>
</tbody>

If we sum up the quantity for no.2 and 3 we will get 423 which is still below 500, that's why both of them still scheduled at 1:00.
So I think, we need 1 variable memory to record the cumulative quantity and compare it with the limit ("500"). But I still get stumbled with the looping and the coding.

Is there any other suggestions?
 
Upvote 0

dermie_72

Well-known Member
Joined
Sep 4, 2012
Messages
1,540
Cell D1 = 1:00
Cell D2 = =IF(C2<>C1,"1:00",IF(SUMIFS($B1:B$2,$C1:C$2,C1,$D1:D$2,D1)+B2<500,D1,D1+0.04166667))

That should provide you with the information you need.

Again, the same rule applies with the macro, for this formula.

Let me know if this works.
 
Upvote 0

Forum statistics

Threads
1,191,123
Messages
5,984,780
Members
439,910
Latest member
Flyingjoblo

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
Top