| day | product name | product assign | process number | estimate time | working hours | total hours completed | total qty completed |
|------|--------------|----------------|----------------|---------------|--------------|----------------------|--------------------|
| 1 | ABC | - | 25 | 200 | 12 | 0 | 0 |
| 2 | ABC | - | 25 | 200 | 8 | 20 | 1 |
| 3 | ABC | - | 25 | 200 | 10 | 10 | 2 |
| 1 | ABC | - | 15 | 100 | 12 | 12 | 1 |
| 2 | DEF | - | 16 | 300 | 8 | 0 | 0 |
| 3 | DEF | - | 16 | 300 | 10 | 10 | 2 |
Above is the result which i will get when i submit the data in my userform.
Below is my coding:
Private Sub CommandButton3_Click()
Dim ws As Worksheet
Dim lastRow As Long
Dim productName As String
Dim processNumber As String
Dim estimateTime As Double
Dim workingHours As Double
Dim totalQty As Double
' Get the selected worksheet from the combobox
Set ws = ThisWorkbook.Sheets(Me.ComboBox1.Value)
' Find the last used row in the worksheet
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row + 1
' Retrieve values from textboxes
productName = Me.TextBox2.Value
processNumber = Me.TextBox4.Value
estimateTime = CDbl(Me.TextBox5.Value)
workingHours = CDbl(Me.TextBox6.Value)
totalQty = CDbl(Me.TextBox7.Value)
' Find the previous row with the same product and process
Dim foundRow As Range
Set foundRow = ws.Range("B11:B" & lastRow).Find(productName & processNumber, LookIn:=xlValues, LookAt:=xlWhole)
' Calculate total working hours and update columns
If totalQty > 0 Then
Dim totalWorkingHours As Double
If Not foundRow Is Nothing Then
totalWorkingHours = ws.Cells(foundRow.Row, "G").Value ' Use the existing total working hours
Else
totalWorkingHours = 0
End If
' Check if totalQty is 1 to accumulate working hours
If totalQty = 1 Then
totalWorkingHours = totalWorkingHours + workingHours
End If
ws.Cells(lastRow, "G").Value = totalWorkingHours
Else
ws.Cells(lastRow, "G").Value = 0 ' Clear total working hours when quantity is 0
End If
' Add data to the worksheet in a table-like format
ws.Cells(lastRow, "A").Value = TextBox1.Value ' Day
ws.Cells(lastRow, "B").Value = productName
ws.Cells(lastRow, "C").Value = TextBox3.Value ' Product Assign
ws.Cells(lastRow, "D").Value = processNumber
ws.Cells(lastRow, "E").Value = estimateTime
ws.Cells(lastRow, "F").Value = workingHours
ws.Cells(lastRow, "H").Value = totalQty ' Total Qty Completed
' Clear textboxes
Me.TextBox2.Value = ""
Me.TextBox4.Value = ""
Me.TextBox5.Value = ""
Me.TextBox6.Value = ""
Me.TextBox7.Value = ""
' Inform user
MsgBox "Data submitted successfully!", vbInformation
End Sub
Private Sub UserForm_Initialize()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
If ws.Visible = xlSheetVisible Then
ComboBox1.AddItem ws.Name
End If
Next ws
End Sub
From coding above, i encountered error and the error we can see from the image i attached below. i hope you guys can help me correct my coding and get like the result above.
|------|--------------|----------------|----------------|---------------|--------------|----------------------|--------------------|
| 1 | ABC | - | 25 | 200 | 12 | 0 | 0 |
| 2 | ABC | - | 25 | 200 | 8 | 20 | 1 |
| 3 | ABC | - | 25 | 200 | 10 | 10 | 2 |
| 1 | ABC | - | 15 | 100 | 12 | 12 | 1 |
| 2 | DEF | - | 16 | 300 | 8 | 0 | 0 |
| 3 | DEF | - | 16 | 300 | 10 | 10 | 2 |
Above is the result which i will get when i submit the data in my userform.
Below is my coding:
Private Sub CommandButton3_Click()
Dim ws As Worksheet
Dim lastRow As Long
Dim productName As String
Dim processNumber As String
Dim estimateTime As Double
Dim workingHours As Double
Dim totalQty As Double
' Get the selected worksheet from the combobox
Set ws = ThisWorkbook.Sheets(Me.ComboBox1.Value)
' Find the last used row in the worksheet
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row + 1
' Retrieve values from textboxes
productName = Me.TextBox2.Value
processNumber = Me.TextBox4.Value
estimateTime = CDbl(Me.TextBox5.Value)
workingHours = CDbl(Me.TextBox6.Value)
totalQty = CDbl(Me.TextBox7.Value)
' Find the previous row with the same product and process
Dim foundRow As Range
Set foundRow = ws.Range("B11:B" & lastRow).Find(productName & processNumber, LookIn:=xlValues, LookAt:=xlWhole)
' Calculate total working hours and update columns
If totalQty > 0 Then
Dim totalWorkingHours As Double
If Not foundRow Is Nothing Then
totalWorkingHours = ws.Cells(foundRow.Row, "G").Value ' Use the existing total working hours
Else
totalWorkingHours = 0
End If
' Check if totalQty is 1 to accumulate working hours
If totalQty = 1 Then
totalWorkingHours = totalWorkingHours + workingHours
End If
ws.Cells(lastRow, "G").Value = totalWorkingHours
Else
ws.Cells(lastRow, "G").Value = 0 ' Clear total working hours when quantity is 0
End If
' Add data to the worksheet in a table-like format
ws.Cells(lastRow, "A").Value = TextBox1.Value ' Day
ws.Cells(lastRow, "B").Value = productName
ws.Cells(lastRow, "C").Value = TextBox3.Value ' Product Assign
ws.Cells(lastRow, "D").Value = processNumber
ws.Cells(lastRow, "E").Value = estimateTime
ws.Cells(lastRow, "F").Value = workingHours
ws.Cells(lastRow, "H").Value = totalQty ' Total Qty Completed
' Clear textboxes
Me.TextBox2.Value = ""
Me.TextBox4.Value = ""
Me.TextBox5.Value = ""
Me.TextBox6.Value = ""
Me.TextBox7.Value = ""
' Inform user
MsgBox "Data submitted successfully!", vbInformation
End Sub
Private Sub UserForm_Initialize()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
If ws.Visible = xlSheetVisible Then
ComboBox1.AddItem ws.Name
End If
Next ws
End Sub
From coding above, i encountered error and the error we can see from the image i attached below. i hope you guys can help me correct my coding and get like the result above.