Yodelayheewho
Member
I'm new to VBA. The following code works. When I click on the command button, it saves my entries to the "Master" worksheet.
However, I understand there is a better way to do this that is more efficient. Based on some research, I came up with this code and can't get it to work.
I would appreciate your insight.
Code:
'Update and Save Button'
Private Sub cmbUpdate_Click()
On Error Resume Next
Dim Shop_Order_Number As String
Shop_Order_Number = Trim(txtShopOrdNum)
lastrow = Worksheets("Master").Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To lastrow
'CDbl(txtText.Value) is needed for currency, does not work for percent
If Worksheets("Master").Cells(i, 4).Value = Shop_Order_Number Then
Worksheets("Master").Cells(i, 1).Value = txtPrefix
Worksheets("Master").Cells(i, 2).Value = cboStatus
Worksheets("Master").Cells(i, 3).Value = txtSuffix
Worksheets("Master").Cells(i, 4).Value = txtShopOrdNum
Worksheets("Master").Cells(i, 5).Value = txtEmailSubLine
Worksheets("Master").Cells(i, 6).Value = txtNotes
Worksheets("Master").Cells(i, 7).Value = cboStage
Worksheets("Master").Cells(i, 8).Value = CDate(Me.txtStartDate.Value)
Worksheets("Master").Cells(i, 9).Value = CDate(Me.txtStageDue.Value)
Worksheets("Master").Cells(i, 10).Value = CDate(Me.txtEndDate.Value)
Worksheets("Master").Cells(i, 11).Value = txtDays
Worksheets("Master").Cells(i, 12).Value = cboProcess
Worksheets("Master").Cells(i, 13).Value = txtPropNum
Worksheets("Master").Cells(i, 14).Value = cboSalesPer
Worksheets("Master").Cells(i, 15).Value = txtPrimSalesTerr
Worksheets("Master").Cells(i, 16).Value = CDate(Me.txtPropDate.Value)
Worksheets("Master").Cells(i, 17).Value = txtLT
Worksheets("Master").Cells(i, 18).Value = CDate(Me.txtPromDate.Value)
Worksheets("Master").Cells(i, 19).Value = CDate(Me.txtExpDate.Value)
Worksheets("Master").Cells(i, 20).Value = CDbl(Me.txtCost.Value)
'redacted columns 21-102'
End If
Next
ThisWorkbook.Save
MsgBox ("Your work is saved")
End Sub
However, I understand there is a better way to do this that is more efficient. Based on some research, I came up with this code and can't get it to work.
Code:
'Update and Save Button'
Private Sub cmbUpdate_Click()
On Error Resume Next
Dim son As String
son = Trim(txtShopOrdNum)
Dim ws As Worksheet
Set ws = Worksheets("Master")
lastrow = ws.Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To lastrow
'CDbl(txtText.Value) is needed for currency, does not work for percent
If ws.Cells(i, 4).Value = Shop_Order_Number Then
ws.Cells(i, 1).Value = txtPrefix
ws.Cells(i, 2).Value = cboStatus
ws.Cells(i, 3).Value = txtSuffix
ws.Cells(i, 4).Value = txtShopOrdNum
ws.Cells(i, 5).Value = txtEmailSubLine
ws.Cells(i, 6).Value = txtNotes
ws.Cells(i, 7).Value = cboStage
ws.Cells(i, 8).Value = CDate(Me.txtStartDate.Value)
ws.Cells(i, 9).Value = CDate(Me.txtStageDue.Value)
ws.Cells(i, 10).Value = CDate(Me.txtEndDate.Value)
ws.Cells(i, 11).Value = txtDays
ws.Cells(i, 12).Value = cboProcess
ws.Cells(i, 13).Value = txtPropNum
ws.Cells(i, 14).Value = cboSalesPer
ws.Cells(i, 15).Value = txtPrimSalesTerr
ws.Cells(i, 16).Value = CDate(Me.txtPropDate.Value)
ws.Cells(i, 17).Value = txtLT
ws.Cells(i, 18).Value = CDate(Me.txtPromDate.Value)
ws.Cells(i, 19).Value = CDate(Me.txtExpDate.Value)
ws.Cells(i, 20).Value = CDbl(Me.txtCost.Value)
'redacted columns 21-102'
End If
Next
ThisWorkbook.Save
MsgBox ("Your work is saved")
End Sub
I would appreciate your insight.