The condition in the Do..Loop doesn't change with each iteration of the loop. myOffset needs to be incorperated into the test for the Do.
Also,myOffset needs to be reset to 0 for each new row, lest the Do Loop start with the myOffset value from the last row.
If the Offset(0,1) value is present on Row1, but the Offset(0,2) is not, myOffset will increase without limit. A test to make sure that it exits should be added.
This worked for me.
Dim c As Range, r As Range
Dim isDuty As Boolean, myOffset As Long
Dim Status As Variant
For Each c In Sheets("Staff Monday").Range("B2:B100")
isDuty = IsNumeric(c.Value)
If isDuty = False Then
Status = c.Value
With Range(c.Offset(0, 5), c.Offset(0, 148))
If IsNumeric(Application.Match(c.Offset(0, 2).Value, .EntireColumn.Rows(1), 0)) Then
For Each r In .Cells
If Cells(1, r.Column).Value = c.Offset(0, 1) Then
r.Value = Status
myOffset = 0
Do Until Cells(1, r.Column + myOffset).Value = c.Offset(0, 2).Value
r.Offset(0, myOffset).Value = Status
myOffset = myOffset + 1
Loop
End If
Next r
Else
MsgBox "No stop value for row " & c.Row
End If
End With
End If
Next c
You may want change the Do test to
Do Until Cells(1, r.Column + myOffset - 1).Value = c.Offset(0, 2).Value
in order to write to the column that matches the c.Offset(0,2).Value
I hope this helps.
Bookmarks