Multiple Fault Code Mapping Based on Time Slot Ranges

Surendra Bhilare

New Member
Joined
Mar 29, 2025
Messages
13
Office Version
  1. 2013
Platform
  1. Windows
Dear Team,

We have two sheets, Sheet1 and Sheet3.

Our requirement is to extract multiple fault codes from Sheet3 and display them in Sheet1, based on the following parameters:
  • Posting Date
  • Work Center
  • Time Slot
Key Requirement:
  • If the time slot in Sheet3 is mentioned as 07-15, the corresponding fault codes should be distributed across the time slots 07:00-08:00 to 14:00-15:00 in Sheet1.
  • The fault codes should be correctly placed within the respective time slot ranges.
  • Multiple codes added with "," separator.
  • We are using excel version 2013.
  • Please take for midnight range slot.
Please let us know the best way to achieve this.

Sheet 1 - Data require in sheet 1.


Posting DateWork CenterDepartmentBay07-0808-0909-1010-1111-1212-1313-1414-1515-1616-1717-1818-1919-2020-2121-2222-2323-0000-0101-0202-0303-0404-0505-0606-07
01-03-2025SCMSCS02SOD Cutting0


Sheet 3 - Data to be pick from Sheet 3


Start TimeEnd TimeTime SlotWork centerFault Code DescrDown time date.
07:00:0007:30:0007-08SCMSCS02MECH BREAK DOWN01-03-2025
07:30:0008:00:0007-08SCMSCS02NO MANPOWER01-03-2025
08:00:0008:30:0008-09SCMSCS02NO PLANNING01-03-2025
08:30:0009:00:0008-09SCMSCS02DIE SETUP01-03-2025
09:00:0010:00:0009-10SCMSCS02HEAT ISSUE01-03-2025
10:00:0011:00:0010-11SCMSCS02MECH BREAK DOWN01-03-2025
11:00:0012:00:0011-12SCMSCS02LINE CHANGE01-03-2025
12:00:0013:00:0012-13SCMSCS02TEA BREAK01-03-2025
18:00:0019:00:0018-19SCMSCS02RING PANA CHANGE01-03-2025
19:00:0019:30:0019-20SCMSCS02NO MANPOWER01-03-2025
20:00:0020:30:0020-21SCMSCS02TEA BREAK01-03-2025
23:00:0007:00:0023-07SCMSCS02ELECTRICAL BREAKDOWN01-03-2025
1743226232590.png
1743226275809.png



Thanks & Regards,
Surendra Bhilare
 
We're here to provide you with the best possible solution, are you willing to follow a few simple steps?
Is your Excel version 2013?
Dear Sam,


Yes, my Excel version is 2013.


We need the output to display fault codes in the respective time slots where the downtime is mentioned in Sheet3. The fault codes should be correctly placed in the corresponding time slots for the same time period.


Additionally, in cases where the time slot spans multiple hours (e.g., 07-15 or 08-15), the fault code should be allocated to each and every time slot within that range.


Please let us know if you need any further details.
 
Upvote 0
Dear Team,
I would like to inform you that the issue has been successfully resolved using a VBA code solution. Thank you for your support and cooperation.

Sub PopulateFaultCodesAndSumYield()
Dim wsDelay As Worksheet, wsCutting As Worksheet, wsOutput As Worksheet
Dim lastRowDelay As Long, lastRowCutting As Long, lastRowOutput As Long, lastColOutput As Long
Dim i As Long, j As Long, outputRowNum As Long
Dim startTime As Double, endTime As Double, slotStart As Double, slotEnd As Double
Dim workCenter As String, faultCode As String, department As String, bayNo As String
Dim downTimeDate As Variant, postingDate As Variant, existingData As String, timeSlot As String
Dim combinedValue As String, uniqueFaultCodes As Object
Dim yieldSum As Double, yieldDict As Object

' Set worksheets
Set wsDelay = ThisWorkbook.Sheets("Delay")
Set wsCutting = ThisWorkbook.Sheets("Cutting")
Set wsOutput = ThisWorkbook.Sheets("Cutting Summary")

' Find last row & column dynamically
lastRowDelay = wsDelay.Cells(wsDelay.Rows.Count, 1).End(xlUp).Row
lastRowCutting = wsCutting.Cells(wsCutting.Rows.Count, 1).End(xlUp).Row
lastRowOutput = wsOutput.Cells(wsOutput.Rows.Count, 1).End(xlUp).Row
lastColOutput = wsOutput.Cells(2, wsOutput.Columns.Count).End(xlToLeft).Column

' Clear previous fault codes & yield data in Summary (Only in Time Slot Columns)
wsOutput.Range(wsOutput.Cells(3, 5), wsOutput.Cells(lastRowOutput, lastColOutput)).ClearContents

' Initialize dictionaries for unique fault codes and yield summation
Set uniqueFaultCodes = CreateObject("Scripting.Dictionary")
Set yieldDict = CreateObject("Scripting.Dictionary")

' Loop through Delay sheet (Fault Codes)
For i = 2 To lastRowDelay
If IsNumeric(wsDelay.Cells(i, 4).Value) Then startTime = CDbl(wsDelay.Cells(i, 4).Value) Else GoTo SkipFaultRow
If IsNumeric(wsDelay.Cells(i, 6).Value) Then endTime = CDbl(wsDelay.Cells(i, 6).Value) Else GoTo SkipFaultRow

workCenter = Trim(wsDelay.Cells(i, 8).Value)
faultCode = Trim(wsDelay.Cells(i, 15).Value)
downTimeDate = wsDelay.Cells(i, 21).Value
If Not IsDate(downTimeDate) Then GoTo SkipFaultRow
downTimeDate = CDate(downTimeDate)

' Loop through all rows in Summary to find a match
For outputRowNum = 3 To lastRowOutput
If wsOutput.Cells(outputRowNum, 1).Value = downTimeDate And wsOutput.Cells(outputRowNum, 2).Value = workCenter Then
' Loop through time slots in Summary
For j = 5 To lastColOutput
timeSlot = wsOutput.Cells(2, j).Value
slotStart = ConvertTimeSlotToNumber(Left(timeSlot, 2))
slotEnd = ConvertTimeSlotToNumber(Right(timeSlot, 2))

' Fix: Ensure 23-00 slot is handled correctly
If slotEnd = 0 Then slotEnd = 24 ' Handle midnight transition

' Check if fault code falls within time slot
If (startTime < slotEnd And endTime > slotStart) Then
' Store fault codes uniquely
If Not uniqueFaultCodes.Exists(outputRowNum & "-" & j) Then
uniqueFaultCodes(outputRowNum & "-" & j) = faultCode
Else
If InStr(1, uniqueFaultCodes(outputRowNum & "-" & j), faultCode, vbTextCompare) = 0 Then
uniqueFaultCodes(outputRowNum & "-" & j) = uniqueFaultCodes(outputRowNum & "-" & j) & ", " & faultCode
End If
End If
End If
Next j
End If
Next outputRowNum

SkipFaultRow:
Next i

' Loop through Cutting sheet (Yield Summation)
For i = 2 To lastRowCutting
postingDate = wsCutting.Cells(i, 5).Value
workCenter = Trim(wsCutting.Cells(i, 14).Value)
department = Trim(wsCutting.Cells(i, 30).Value)
bayNo = Trim(wsCutting.Cells(i, 31).Value)
If IsNumeric(wsCutting.Cells(i, 8).Value) And wsCutting.Cells(i, 8).Value <> "" Then
yieldSum = CDbl(wsCutting.Cells(i, 8).Value)
Else
yieldSum = 0
End If
timeSlot = wsCutting.Cells(i, 28).Value
If Not IsDate(postingDate) Then GoTo SkipYieldRow
postingDate = CDate(postingDate)

' Loop through Summary to find a match
For outputRowNum = 3 To lastRowOutput
If wsOutput.Cells(outputRowNum, 1).Value = postingDate And wsOutput.Cells(outputRowNum, 2).Value = workCenter And wsOutput.Cells(outputRowNum, 3).Value = department And wsOutput.Cells(outputRowNum, 4).Value = bayNo Then
' Loop through time slots
For j = 5 To lastColOutput
If wsOutput.Cells(2, j).Value = timeSlot Then
' Accumulate Yield in Dictionary
If Not yieldDict.Exists(outputRowNum & "-" & j) Then
yieldDict(outputRowNum & "-" & j) = yieldSum
Else
yieldDict(outputRowNum & "-" & j) = yieldDict(outputRowNum & "-" & j) + yieldSum
End If
Exit For
End If
Next j
End If
Next outputRowNum

SkipYieldRow:
Next i

' Populate the Summary sheet with final combined values
For outputRowNum = 3 To lastRowOutput
For j = 5 To lastColOutput
Dim key As String
key = outputRowNum & "-" & j

' Fetch stored values
existingData = ""
If uniqueFaultCodes.Exists(key) Then existingData = uniqueFaultCodes(key)
If yieldDict.Exists(key) Then yieldSum = yieldDict(key) Else yieldSum = 0

' Combine yield and fault codes in the same cell
If yieldSum > 0 And existingData <> "" Then
combinedValue = yieldSum & ", " & existingData
ElseIf yieldSum > 0 Then
combinedValue = yieldSum
ElseIf existingData <> "" Then
combinedValue = existingData
Else
combinedValue = ""
End If

wsOutput.Cells(outputRowNum, j).Value = combinedValue
Next j
Next outputRowNum

MsgBox "Fault codes and Yield(KG) updated successfully!", vbInformation
End Sub

' Convert time slot (e.g., "07" -> 7.0, "23" -> 23.0)
Function ConvertTimeSlotToNumber(hourStr As String) As Double
ConvertTimeSlotToNumber = CDbl(TimeValue(hourStr & ":00"))
End Function
 
Upvote 0

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