Hello. I have this VBA code to change the values of cells in column D depending on what time are in the cells in column A. 6:15:00's are STG.A01 then increasing in sequential order number wise. STG.A02, STG.A03, etc. 6:45:00's are STG.B. 7:15:00's are STG.C. 7:45:00's are STG.D. 8:15:00's are STG.DD. 8:45:00's are STG.DDD. This is fine. However sometimes there are not 6:45:00 times. Therefore, in this situation I would like to make 7:15:00 ones STG.B. So if there are no cells with time "6:45:00" in column A, have the cells in column D be STG.B01, STG.B02, STG.B03, etc, when it detects 7:15:00 in column A. If there are 6:45:00 ones, keep 7:15:00 ones C. Thank you to anyone willing to help. Here is the code.
VBA Code:
Sub PickIF()
For Each w In Workbooks
If UCase(w.Name) Like UCase("*Pick*order*") Then
Windows(w.Name).Activate
Exit For
End If
Next w
On Error Resume Next
Dim c As Long, cOver As Long, i As Long, iA As Long, iB As Long, iC As Long, iD As Long, iE As Long, iG As Long, iH As Long, lRow As Long
lRow = Range("A1").End(xlDown).Row
c = 1
iA = Application.WorksheetFunction.CountIf(Range("A2:A" & lRow), "6:15:00")
iB = Application.WorksheetFunction.CountIf(Range("A2:A" & lRow), "6:45:00")
iC = Application.WorksheetFunction.CountIf(Range("A2:A" & lRow), "7:15:00")
iD = Application.WorksheetFunction.CountIf(Range("A2:A" & lRow), "7:45:00")
iE = Application.WorksheetFunction.CountIf(Range("A2:A" & lRow), "8:15:00")
iG = Application.WorksheetFunction.CountIf(Range("A2:A" & lRow), "8:45:00")
iH = Application.WorksheetFunction.CountIf(Range("A2:A" & lRow), "9:45:00")
For i = 2 To iA + 1
If c < 10 Then
Cells(i, 4) = "STG.A" & 0 & c
c = c + 1
Else
Cells(i, 4) = "STG.A" & c
c = c + 1
End If
Next i
c = 1
cOver = 1
For i = iA + 2 To iA + iB + 1
If c > 36 Then
If cOver < 10 Then
Cells(i, 4) = "STG.BB" & 0 & cOver
cOver = cOver + 1
Else
Cells(i, 4) = "STG.BB" & cOver
cOver = cOver + 1
End If
ElseIf c < 10 Then
Cells(i, 4) = "STG.B" & 0 & c
c = c + 1
Else
Cells(i, 4) = "STG.B" & c
c = c + 1
End If
Next i
c = 1
For i = iA + iB + 2 To iA + iB + iC + 1
If c < 10 Then
Cells(i, 4) = "STG.C" & 0 & c
c = c + 1
Else
Cells(i, 4) = "STG.C" & c
c = c + 1
End If
Next i
c = 1
For i = iA + iB + iC + 2 To iA + iB + iC + iD + 1
If c < 10 Then
Cells(i, 4) = "STG.D" & 0 & c
c = c + 1
Else
Cells(i, 4) = "STG.D" & c
c = c + 1
End If
Next i
c = 1
For i = iA + iB + iC + iD + 2 To iA + iB + iC + iD + iE + 1
If c < 10 Then
Cells(i, 4) = "STG.DD" & 0 & c
c = c + 1
Else
Cells(i, 4) = "STG.DD" & c
c = c + 1
End If
Next i
c = 1
For i = iA + iB + iC + iD + iE + 2 To iA + iB + iC + iD + iE + iG + 1
If c < 10 Then
Cells(i, 4) = "STG.DDD" & 0 & c
c = c + 1
Else
Cells(i, 4) = "STG.DDD" & c
c = c + 1
End If
Next i
c = 1
For i = iA + iB + iC + iD + iE + iG + 2 To iA + iB + iC + iD + iE + iG + iH + 1
If c < 10 Then
Cells(i, 4) = "STG.AA" & 0 & c
c = c + 1
Else
Cells(i, 4) = "STG.AA" & c
c = c + 1
End If
Next i
End Sub
Last edited by a moderator: