atditiljazi
New Member
- Joined
- Nov 22, 2022
- Messages
- 41
- Office Version
- 365
- Platform
- Windows
hello, below is a macro i created that doesn't seem to work. i get a "runtime error 6 overflow" message. can someone figure out why i am getting the message by looking at the macro?
Rich (BB code):
Sub CreateSummaryTable()
Dim ws2 As Worksheet, ws1 As Worksheet
Dim lastRow As Long, supplierRow As Long, summaryRow As Long
Dim supplier As String
Dim onTimeCount As Long, lateCount As Long
Dim onTimePercent As Double, latePercent As Double
' Set worksheets
Set ws2 = ThisWorkbook.Sheets("Sheet2")
Set ws1 = ThisWorkbook.Sheets("Sheet3")
' Determine the last row with data in Column A of Sheet2
lastRow = ws2.Cells(ws2.Rows.Count, "A").End(xlUp).Row
' Loop through each supplier in Column A of Sheet2
For supplierRow = 2 To lastRow ' Assuming header row is in Row 1
' Get the supplier name
supplier = ws2.Cells(supplierRow, "A").Value
' Reset the counters
onTimeCount = 0
lateCount = 0
' Loop through each row in Column E (Delivery Status) of Sheet2
For Each cell In ws2.Range("F2:F" & lastRow)
' Check if the supplier name matches
If cell.Offset(0, -4).Value = supplier Then
' Count the "on time" and "late" deliveries
If cell.Value = "On Time" Then
onTimeCount = onTimeCount + 1
ElseIf cell.Value = "Late" Then
lateCount = lateCount + 1
End If
End If
Next cell
' Calculate the percentages
onTimePercent = (onTimeCount / (onTimeCount + lateCount)) * 100
latePercent = (lateCount / (onTimeCount + lateCount)) * 100
' Find the first empty row in Column A of Sheet1
summaryRow = ws1.Cells(ws1.Rows.Count, "A").End(xlUp).Row + 1
' Write the supplier name and percentages to Sheet1
ws1.Cells(summaryRow, "A").Value = supplier
ws1.Cells(summaryRow, "B").Value = onTimePercent
ws1.Cells(summaryRow, "C").Value = latePercent
' Update the progress
DoEvents
Next supplierRow
' Clear any previous data in Sheet1 starting from row 2
ws1.Range("A2:C" & ws1.Cells(ws1.Rows.Count, "A").End(xlUp).Row).ClearContents
' Display a message when finished
MsgBox "Summary table created successfully."
End Sub