VBA Code:
Private Sub CommandButton1_Click()
Dim targetSheetName As String
targetSheetName = ComboBox1.Value
If targetSheetName = "" Then
MsgBox "Please select a target sheet.", vbExclamation
Exit Sub
End If
Dim wsTarget As Worksheet
Dim processNo As String
Dim productName As String
Dim totalHours As Double
Dim totalQty As Double
Dim foundRow As Long
Dim lastRow As Long
On Error Resume Next
Set wsTarget = ActiveWorkbook.Worksheets(targetSheetName)
On Error GoTo 0
If wsTarget Is Nothing Then
MsgBox "Worksheet '" & targetSheetName & "' not found in the workbook.", vbExclamation
Exit Sub
End If
On Error Resume Next
Set wsSummary2 = Workbooks("DASHBOARD.xlsm").Worksheets("TOTAL HOURS & QTY COMPLETED")
On Error GoTo 0
If wsSummary2 Is Nothing Then
MsgBox "Worksheet 'TOTAL HOURS & QTY COMPLETED' not found in workbook 'DASHBOARD.xlsm'.", vbExclamation
Exit Sub
End If
' Get data from TextBoxes
processNo = TextBox4.Value
productName = TextBox2.Value
' Check if TextBox2 value is "OFF DAY" or "PUBLIC HOLIDAY"
Dim isOffDay As Boolean
isOffDay = UCase(Trim(TextBox2.Value)) = "OFF DAY" Or UCase(Trim(TextBox2.Value)) = "PUBLIC HOLIDAY"
If Not isOffDay Then
' Convert textbox values to numeric format
totalHours = CDbl(TextBox7.Value)
totalQty = CDbl(TextBox8.Value)
End If
' Write data to the "form after completed work" sheet in SUMMARY2.xlsx
If Not isOffDay Then
wsSummary2LastRow = wsSummary2.Cells(wsSummary2.Rows.Count, 1).End(xlUp).Row
wsSummary2LastRow = wsSummary2LastRow + 1
foundRow = 0
' Calculate cumulative completed hours formula
Dim cumulativeFormula As String
cumulativeFormula = "=SUMIFS(G:G, B:B, " & Chr(34) & processNo & Chr(34) & ", D:D, " & Chr(34) & productName & Chr(34) & ")"
wsSummary2.Cells(foundRow, 5).Formula = cumulativeFormula
End If
' Check if the process number and product name already exist in the "form after completed work" sheet
Dim i As Long
For i = 2 To wsSummary2LastRow ' Start from row 2
If wsSummary2.Cells(i, 2).Value = processNo And wsSummary2.Cells(i, 1).Value = productName Then
foundRow = i
Exit For
End If
Next i
' If found, update the total hours and total quantity, otherwise, add a new row
If foundRow > 0 Then
wsSummary2.Cells(foundRow, 3).Value = wsSummary2.Cells(foundRow, 3).Value + totalHours
wsSummary2.Cells(foundRow, 4).Value = wsSummary2.Cells(foundRow, 4).Value + totalQty
Else
wsSummary2.Cells(wsSummary2LastRow, 1).Value = productName
wsSummary2.Cells(wsSummary2LastRow, 2).Value = processNo
wsSummary2.Cells(wsSummary2LastRow, 3).Value = totalHours
wsSummary2.Cells(wsSummary2LastRow, 4).Value = totalQty
End If
End If
' Write data to the target sheet (ComboBox2 selected sheet)
lastRow = wsTarget.Cells(wsTarget.Rows.Count, 1).End(xlUp).Row
lastRow = lastRow + 1
With wsTarget
.Cells(lastRow, 1).Value = TextBox1.Value
.Cells(lastRow, 2).Value = TextBox2.Value
.Cells(lastRow, 3).Value = TextBox3.Value
.Cells(lastRow, 4).Value = TextBox4.Value
.Cells(lastRow, 5).Value = TextBox5.Value
.Cells(lastRow, 6).Value = TextBox6.Value
.Cells(lastRow, 7).Value = IIf(isOffDay, " ", totalHours) ' Use " " if totalQty is 0, otherwise use the value
.Cells(lastRow, 8).Value = IIf(isOffDay, " ", totalQty) ' Use " " if totalQty is 0, otherwise use the value
.Cells(lastRow, 9).Value = TextBox9.Value
.Cells(lastRow, 10).Value = TextBox10.Value
' Get data from the new TextBox (TextBox11 in this example)
Dim newValue As String
newValue = TextBox11.Value
' Write the new TextBox value to Column M (Column 13) in the target sheet
wsTarget.Cells(lastRow, 13).Value = newValue
' Calculate the value for column K based on the formula (D/F/60*G)
If Not isOffDay Then
.Cells(lastRow, 11).Formula = "=E" & lastRow & "/G" & lastRow & "/60*H" & lastRow
End If
End With
' Check if TextBox2 value is "OFF DAY" or "PUBLIC HOLIDAY"
If isOffDay Then
With wsTarget
.Range(.Cells(lastRow, 1), .Cells(lastRow, 13)).Interior.Color = RGB(255, 192, 203) ' Pink color
End With
End If
MsgBox "Record added to sheet '" & targetSheetName & "' and 'TOTAL HOURS & QTY COMPLETED' in 'DASHBOARD.xlsm' successfully.", vbInformation
End Sub
Private Sub UserForm_Initialize()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
If ws.Visible = xlSheetVisible Then
ComboBox1.AddItem ws.Name
End If
Next ws
End Sub
I already have this coding, and i want to add formula which is for example for day 1(column A) and product name(column B) and process number(column D) and the working hours (column F) is 12 hours and hours completed(column G) is 0 and the quantity completed(column H) IS 0. and the day 2 the product name is same and the process number also same the working bours also same 12 but it finish 1 complete quantity so i want sum the working hours for both day 1 and day 2 which is will be 24 for completed hours. do you have any idea what can i add the formula in the userform coding? there is attachment picture for the references where you can see when complete one quantity it will sum the working hours and put the value in completed hours.
Attachments
Last edited by a moderator: