I have some confusion about excel vba. My aim is to determine which shipments are exceed of the daily carton pick capacity or pallet pick capacity. Each month has different carton and pallet amount.
For Each cell In Range("AB2:AB10000") For i = 2 To 10000 If Mid(cell.Value, 4, 2) = "01" Then toplam_karton_ocak = toplam_karton_ocak - Cells(i, 20) toplam_palet_ocak = toplam_palet_ocak - cell(i, 19) If toplam_palet_ocak < 0 Or toplam_karton_ocak < 0 Then MsgBox (Cells(i, 1).Value & " nolu shipment kapasiteyi aşmaktadır.") End If
I used "for each " to check every cell in the column and I used 12 "if" conditions to assign date to the right capacity. I tried to decrease the capacity when every shipment came. My aim is that if pallet or carton pick is less than zero, giving a message on the screen.
Mid(cell.Value, 4, 2) --> It takes 4th and 5th digits of the day. i.e the date is "22.05.2020".It receives "05" and that means "may"My problem is I could not do this for every day. I just did it for month but it is useless because i have to check it separately for every single day. What should i do?
You can find the full code below:
Sub New_DC()New_DC MakroRange("Z1").SelectActiveCell.FormulaR1C1 = "BOOKING ONLY DATES"Range("Z2").SelectActiveCell.FormulaR1C1 = "=LEFT([@[BOOKING_DATE_OR]],8)"Range("AA1").SelectActiveCell.FormulaR1C1 = "Column1 "Range("AA2").SelectActiveCell.FormulaR1C1 = _"=DATE(RIGHT([@[BOOKING ONLY DATES]],4),MID([@[BOOKING ONLY DATES]],3,2),LEFT([@[BOOKING ONLY DATES]],2))"Range("AB1").SelectActiveCell.FormulaR1C1 = "New DC"Range("AB2").SelectActiveCell.FormulaR1C1 = _"=IF(RC[-14]=""1-YES"",IF(ISBLANK([@[Sum of Carrier Lead Time]]),[@[Column1 ]]-[@[Sum of TRANSPORT_DURATION]],[@[Column1 ]]-[@[Sum of Carrier Lead Time]]),IF(ISBLANK([@[Sum of Carrier Lead Time]]),[@[END_TIME_UTC]]-[@[Sum of TRANSPORT_DURATION]],[@[END_TIME_UTC]]-[@[Sum of Carrier Lead Time]]))"Columns("AB:AB").SelectSelection.NumberFormat = "m/d/yyyy"Columns("Z:AA").SelectSelection.EntireColumn.Hidden = TrueRange("AE11").Select Range("Table1[[#Headers],[New DC]]").SelectActiveWorkbook.Worksheets("Data").ListObjects("Table1").Sort.SortFields.ClearActiveWorkbook.Worksheets("Data").ListObjects("Table1").Sort.SortFields.Add2 _ Key:=Range("Table1[[#Headers],[New DC]]"), SortOn:=xlSortOnValues, Order _ :=xlAscending, DataOption:=xlSortNormalWith ActiveWorkbook.Worksheets("Data").ListObjects("Table1").Sort .Header = xlYes .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .ApplyEnd WithRange("AC2").SelectActiveCell.FormulaR1C1 = _"=IF([@[Booking_Appointment_Made (groups)]]=""1-YES"",1,0)"Range("AC2").SelectRange(Selection, Selection.End(xlDown)).SelectActiveWorkbook.Worksheets("Data").ListObjects("Table1").Sort.SortFields.ClearActiveWorkbook.Worksheets("Data").ListObjects("Table1").Sort.SortFields.Add2 _ Key:=Range("AC2"), SortOn:=xlSortOnValues, Order:=xlDescending, _ DataOption:=xlSortNormalWith ActiveWorkbook.Worksheets("Data").ListObjects("Table1").Sort .Header = xlYes .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .ApplyEnd WithColumns("AC:AC").SelectSelection.EntireColumn.Hidden = True Range("AB2").SelectDim toplam_karton_ocak As Long, toplam_karton_subat As Long, toplam_karton_mart As Long, toplam_karton_nisan As LongDim toplam_karton_mayis As Long, toplam_karton_haziran As Long, toplam_karton_temmuz As Long, toplam_karton_agustos As LongDim toplam_karton_eylul As Long, toplam_karton_ekim As Long, toplam_karton_kasim As Long, toplam_karton_aralik As LongDim toplam_palet_ocak As Integer, toplam_palet_subat As Integer, toplam_palet_mart As Integer, toplam_palet_nisan As IntegerDim toplam_palet_mayis As Integer, toplam_palet_haziran As Integer, toplam_palet_temmuz As Integer, toplam_palet_agustos As IntegerDim toplam_palet_eylul As Integer, toplam_palet_ekim As Integer, toplam_palet_kasim As Integer, toplam_palet_aralik As Integertoplam_karton_ocak = 15000toplam_palet_ocak = 300toplam_karton_subat = 15000toplam_palet_subat = 300toplam_karton_mart = 15000toplam_palet_mart = 300toplam_karton_nisan = 17000toplam_palet_nisan = 400toplam_karton_mayis = 17500toplam_palet_mayis = 600toplam_karton_haziran = 18000toplam_palet_haziran = 300toplam_karton_temmuz = 20000toplam_palet_temmuz = 300toplam_karton_agustos = 25000toplam_palet_agustos = 500toplam_karton_eylul = 42000toplam_palet_eylul = 900toplam_karton_ekim = 35000toplam_palet_ekim = 750toplam_karton_kasim = 27000toplam_palet_kasim = 750toplam_karton_aralik = 22500toplam_palet_aralik = 750For Each cell In Range("AB2:AB10000")For i = 2 To 10000 If Mid(cell.Value, 4, 2) = "01" Then toplam_karton_ocak = toplam_karton_ocak - Cells(i, 20) toplam_palet_ocak = toplam_palet_ocak - cell(i, 19) If toplam_palet_ocak < 0 Or toplam_karton_ocak < 0 Then MsgBox (Cells(i, 1).Value & " nolu shipment kapasiteyi aşmaktadır.") End If ElseIf Mid(cell.Value, 4, 2) = "02" Then toplam_karton_subat = toplam_karton_subat - Cells(i, 20) toplam_palet_subat = toplam_palet_subat - Cells(i, 19) If toplam_palet_subat < 0 Or toplam_karton_subat < 0 Then MsgBox (Cells(i, 1).Value & " nolu shipment kapasiteyi aşmaktadır.") End If ElseIf Mid(cell.Value, 4, 2) = "03" Then toplam_karton_mart = toplam_karton_mart - Cells(i, 20) toplam_palet_mart = toplam_palet_mart - Cells(i, 19) If toplam_palet_mart < 0 Or toplam_karton_mart < 0 Then MsgBox (Cells(i, 1).Value & " nolu shipment kapasiteyi aşmaktadır.") End If ElseIf Mid(cell.Value, 4, 2) = "04" Then toplam_karton_nisan = toplam_karton_nisan - Cells(i, 20) toplam_palet_nisan = toplam_palet_nisan - Cells(i, 19) If toplam_palet_nisan < 0 Or toplam_karton_nisan < 0 Then MsgBox (Cells(i, 1).Value & " nolu shipment kapasiteyi aşmaktadır.") End If ElseIf Mid(cell.Value, 4, 2) = "05" Then toplam_karton_mayis = toplam_karton_mayis - Cells(i, 20) toplam_palet_mayis = toplam_palet_mayis - Cells(i, 19) If toplam_palet_mayis < 0 Or toplam_karton_mayis < 0 Then MsgBox (Cells(i, 1).Value & " nolu shipment kapasiteyi aşmaktadır.") End If ElseIf Mid(cell.Value, 4, 2) = "06" Then toplam_karton_haziran = toplam_karton_haziran - Cells(i, 20) toplam_palet_haziran = toplam_palet_haziran - Cells(i, 19) If toplam_palet_haziran < 0 Or toplam_karton_haziran < 0 Then MsgBox (Cells(i, 1).Value & " nolu shipment kapasiteyi aşmaktadır.") End If ElseIf Mid(cell.Value, 4, 2) = "07" Then toplam_karton_temmuz = toplam_karton_temmuz - Cells(i, 20) toplam_palet_temmuz = toplam_palet_temmuz - Cells(i, 19) If toplam_palet_temmuz < 0 Or toplam_karton_temmuz < 0 Then MsgBox (Cells(i, 1).Value & " nolu shipment kapasiteyi aşmaktadır.") End If ElseIf Mid(cell.Value, 4, 2) = "08" Then toplam_karton_agustos = toplam_karton_agustos - Cells(i, 20) toplam_palet_agustos = toplam_palet_agustos - Cells(i, 19) If toplam_palet_agustos < 0 Or toplam_karton_agustos < 0 Then MsgBox (Cells(i, 1).Value & " nolu shipment kapasiteyi aşmaktadır.") End If ElseIf Mid(cell.Value, 4, 2) = "09" Then toplam_karton_eylul = toplam_karton_eylul - Cells(i, 20) toplam_palet_eylul = toplam_palet_eylul - Cells(i, 19) If toplam_palet_eylul < 0 Or toplam_karton_eylul < 0 Then MsgBox (Cells(i, 1).Value & " nolu shipment kapasiteyi aşmaktadır.") End If ElseIf Mid(cell.Value, 4, 2) = "10" Then toplam_karton_ekim = toplam_karton_ekim - Cells(i, 20) toplam_palet_ekim = toplam_palet_ekim - Cells(i, 19) If toplam_palet_ekim < 0 Or toplam_karton_ekim < 0 Then MsgBox (Cells(i, 1).Value & " nolu shipment kapasiteyi aşmaktadır.") End If ElseIf Mid(cell.Value, 4, 2) = "11" Then toplam_karton_kasim = toplam_karton_kasim - Cells(i, 20) toplam_palet_kasim = toplam_palet_kasim - Cells(i, 19) If toplam_palet_kasim < 0 Or toplam_karton_kasim < 0 Then MsgBox (Cells(i, 1).Value & " nolu shipment kapasiteyi aşmaktadır.") End If ElseIf Mid(cell.Value, 4, 2) = "12" Then toplam_karton_aralik = toplam_karton_aralik - Cells(i, 20) toplam_palet_aralik = toplam_palet_aralik - Cells(i, 19) If toplam_palet_aralik < 0 Or toplam_karton_aralik < 0 Then MsgBox (Cells(i, 1).Value & " nolu shipment kapasiteyi aşmaktadır.") End If End IfNext iNext cellColumns("AB:AB").SelectRange("AF6").SelectEnd Sub
Thanks for your help :)