VBA to find first available slot

Hi Peter,

Thanks for this code. Could you please adjust it a little bit?

Please find attached a mini sheet as requested.

Sample.xlsm
ABCDEF
5START DATESTART TIMEHRS PREDICTDAYS PREDICTFINISH DATEFINISH TIME
621/04/212:00 PM19.22.4027/04/218:42 AM
727/04/219:00 AM8.51.0628/04/219:30 AM
829/04/218:00 AM15.751.9730/04/213:45 PM
930/04/213:00 PM6.750.843/05/211:15 PM
104/05/212:00 PM212.637/05/2110:30 AM
SCHEDULE
Cell Formulas
RangeFormula
D6:D10D6=IF(C6>0,C6/8,"")
E6:E10E6=IF(A6<>"",IF(B6<>"",IF(D6<>"",IF((D6-TRUNC(D6))/3+B6>0.6875,IF(D6<>"",IF(A6<>"",WORKDAY(A6,D6+1,Holidays),""),""),IF(D6<>"",IF(A6<>"",WORKDAY(A6,D6,Holidays),""),"")),""),""),"")
F6:F10F6=IF(B6<>"",IF(D6<>"",IF((D6-TRUNC(D6))/3+B6>0.6875,(D6-TRUNC(D6))/3+B6-0.6875+0.3333333,(D6-TRUNC(D6))/3+B6),""),"")
Named Ranges
NameRefers ToCells
Holidays=Sheet2!$A$2:$A$8E6:E10
Cells with Conditional Formatting
CellConditionCell FormatStop If True
A7:B10Expression=AND($A7<=$E6,$B7<$F6)textNO
Cells with Data Validation
CellAllowCriteria
B6:B10Timebetween 8:00:00 AM and 4:00:00 PM
 
Upvote 0

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Could you please adjust it a little bit?
Try ..

VBA Code:
Sub ScheduleJobs_v2()
  Dim SL As Object
  Dim a As Variant
  Dim i As Long
  Dim st As Date, prevend As Date
  
  Set SL = CreateObject("System.Collections.Sortedlist")
  With Sheets("SCHEDULE")
    With .Range("A6", .Range("C" & Rows.Count).End(xlUp))
      a = .Value
      For i = 1 To UBound(a)
        SL.Add a(i, 1) + a(i, 2) & Format(i, "|0000"), i
      Next i
      For i = 0 To SL.Count - 1
        st = Split(SL.GetKey(i), "|")(0)
        If i > 0 Then prevend = .Cells(i, 5) + .Cells(i, 6)
        If st < prevend Then st = WorksheetFunction.Ceiling(prevend, 1 / 48)
        .Rows(1).Offset(i).Value = Array(Int(st), st - Int(st), a(SL.GetByIndex(i), 3))
      Next i
    End With
  End With
End Sub
 
Upvote 0
Hi mate,

Thanks for that. Unfortunately, Excel shows this error when I try to run this code.

1623824770784.png
 
Upvote 0
Excel shows this error when I try to run this code.
Looks like your Windows system might be missing something required for that method. Rather than trying to track that down, now that I have a better feel for what you have, maybe this alternative (simpler) approach might be better anyway, ?

VBA Code:
Sub ScheduleJobs_v3()
  Dim i As Long
  Dim st As Date, prevend As Date
  
  Application.ScreenUpdating = False
  With Sheets("SCHEDULE").Range("A6", Sheets("SCHEDULE").Range("C" & Rows.Count).End(xlUp))
    .Sort Key1:=.Columns(1), Order1:=xlAscending, Key2:=.Columns(2), Order2:=xlAscending, Header:=xlNo
    For i = 2 To .Rows.Count
      st = .Cells(i, 1).Value + .Cells(i, 2).Value
      prevend = .Cells(i - 1, 5).Value + .Cells(i - 1, 6).Value
      If st < prevend Then
        st = WorksheetFunction.Ceiling(prevend, 1 / 48)
        .Cells(i, 1).Resize(, 2).Value = Array(Int(st), st - Int(st))
      End If
    Next i
  End With
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
Hi mate,

Thank you very much for your help. It all seems to work well.
 
Upvote 0

Forum statistics

Threads
1,214,784
Messages
6,121,538
Members
449,038
Latest member
Guest1337

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