因此,我正在使用VBA在excel中创建表单,并且所有代码都已完成。我想测试表格并保存表格,以便将数据导出到同一工作簿中的另一张纸上。我填写了表单,然后单击“保存”,但它什么也不做。我进入vba并收到运行时错误'424',它没有说代码在哪里损坏或问题。
我尝试中断运行这些函数,但无法缩小范围。
Function ValidateForm() As Boolean
txtDA.BackColor = vbWhite
txtTB.BackColor = vbWhite
txtPN.BackColor = vbWhite
cmbMO.BackColor = vbWhite
txtLO.BackColor = vbWhite
cmbDT.BackColor = vbWhite
txtPO.BackColor = vbWhite
txtQU.BackColor = vbWhite
txtMR.BackColor = vbWhite
ValidateForm = True
If Trim(txtDA.Value) = "" Then
MsgBox "Date can't be left blank.", vbOKOnly + vbInformation, "Date"
txtDA.BackColor = vbRed
txtDA.Activate
ValidateForm = False
ElseIf Trim(txtTB.Value) = "" Then
MsgBox "Transferred By name can't be left blank.", vbOKOnly + vbInformation, "Transferred By"
txtTB.BackColor = vbRed
txtTB.Activate
ValidateForm = False
ElseIf Trim(txtPN.Value) = "" Then
MsgBox "Part Number can't be left blank.", vbOKOnly + vbInformation, "Part Number"
txtPN.BackColor = vbRed
txtPN.Activate
ValidateForm = False
ElseIf cmbMO.Text <> "Inventory" And cmbMO.Text <> "Production" And _
cmbMO.Text <> "Box Assembly Area" And cmbMO.Text <> "Other" Then
MsgBox "Please select the correct Material Origin from drop down and explain in the Material Movement Rationale section.", vbOKOnly + vbInformation, "Material Origin"
cmbMO.BackColor = vbRed
cmbMO.Activate
ValidateForm = False
ElseIf Trim(txtLO.Value) = "" Then
MsgBox "Lot can't be left blank.", vbOKOnly + vbInformation, "Lot"
txtLO.BackColor = vbRed
txtLO.Activate
ValidateForm = False
ElseIf cmbDT.Text <> "Damaged" And cmbDT.Text <> "Expired" And _
cmbDT.Text <> "Unapproved Item" And cmbDT.Text <> "Obsolete" And _
cmbDT.Text <> "Other" Then
MsgBox "Please select the correct Defect Type if unknown select Other from drop down and explain in the Material Movement Rationale section.", vbOKOnly + vbInformation, "Qualification"
cmbDT.BackColor = vbRed
cmbDT.Activate
ValidateForm = False
ElseIf Trim(txtPO.Value) = "" Then
MsgBox "P.O. can't be left blank if not applicable enter N/A.", vbOKOnly + vbInformation, "P.O."
txtPO.BackColor = vbRed
txtPO.Activate
ValidateForm = False
ElseIf Trim(txtQU.Value) = "" Then
MsgBox "Quantity can't be left blank.", vbOKOnly + vbInformation, "Quantity"
txtQU.BackColor = vbRed
txtQU.Activate
ValidateForm = False
ElseIf Trim(txtMR.Value) = "" Then
MsgBox "Material Movement Rationale can't be left blank.", vbOKOnly + vbInformation, "Material Movement Rationale"
txtMO.BackColor = vbRed
txtMO.Activate
ValidateForm = False
End If
End Function
Function Reset()
Application.ScreenUpdating = False
txtDA.Value = ""
txtDA.BackColor = vbWhite
txtTB.Value = ""
txtTB.BackColor = vbWhite
txtPN.Value = ""
txtPN.BackColor = vbWhite
cmbMO.Text = ""
cmbMO.BackColor = vbWhite
txtLO.Value = ""
txtLO.BackColor = vbWhite
cmbDT.Text = ""
cmbDT.BackColor = vbWhite
txtPO.Value = ""
txtPO.BackColor = vbWhite
txtQU.Value = ""
txtQU.BackColor = vbWhite
txtMR.Value = ""
txtMR.BackColor = vbWhite
Application.ScreenUpdating = True
End Function
Private Sub cmdReset_Click()
Dim i As Integer
i = MsgBox("Do you want to reset this form?", vbQuestion + vbYesNo + vbDefaultButton2, "Form Reset")
If i = vbYes Then
Call Reset
End If
End Sub
Private Sub cmdSave_Click()
Application.ScreenUpdating = False
Dim iRow As Long
iRow = Sheets("Data").Range("A1048576").End(xlUp).Row + 1
If ValidateForm = True Then
With ThisWorkbook.Sheets("Data")
.Range("A" & iRow).Value = iRow - 1
.Range("B" & iRow).Value = txtDA.Value
.Range("C" & iRow).Value = txtTB.Value
.Range("D" & iRow).Value = txtPN.Value
.Range("E" & iRow).Value = cmbMO.Text
.Range("F" & iRow).Value = txtLO.Value
.Range("G" & iRow).Value = cmbDT.Text
.Range("H" & iRow).Value = txtPO.Value
.Range("I" & iRow).Value = txtQU.Value
.Range("J" & iRow).Value = txtMR.Value
End With
Call Reset
Else
Application.ScreenUpdating = True
Exit Sub
End If
Application.ScreenUpdating = True
End Sub
因此,当您单击保存时,它应该将表单上输入的数据传输到数据工作表,并且表单应该重置。或相反,如果您单击“重置”,它会提示您是否要继续;如果您单击“是”,则应重置表格。
现在无论使用哪个选项都不会执行任何操作。除了重设直到提示外,如果您继续重设表单,则不会执行任何操作。