Adding IF statements to this VBA code

Indominus

Board Regular
Joined
Jul 11, 2020
Messages
160
Office Version
  1. 2016
Platform
  1. Windows
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.A then increasing in sequential order number wise. STG.A01, STG.A02, 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. And move the rest up also. So then 7:45:00 ones "STG.C". 8:15:00 ones D. 8:45:00 ones DD. Etc. If there are 6:45:00 ones, keep 7:15:00 ones C and the rest the same. Thank you to anyone willing to help. Here is the full 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
]
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).

Forum statistics

Threads
1,215,054
Messages
6,122,895
Members
449,097
Latest member
dbomb1414

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