How to edit this VBA code include an If statement

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.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:

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)

Forum statistics

Threads
1,214,599
Messages
6,120,447
Members
448,966
Latest member
DannyC96

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