所以我试图将更新按钮连接到包含员工编号,姓名和部门的Excel工作表。
用户输入员工号码的功能如何,然后它会显示姓名和员工的部门(我已经用它完成了)然后用户可以更改名称和部门,然后按更新按钮,这是现在问题所在。
每次按下更新按钮,它都会显示"编译错误:如果没有阻止则结束如果"
Private Sub btn_Update_Click()
Dim lastRow As Integer
Dim empNum As Integer
Dim rowSelect As Integer
Dim x As Integer
If Trim(txt_EmpNo.Value) = vbNullString Then
MsgBox "The Employee's No cannot be blank!"
Else
empNum = txt_EmpNo.Value
Sheets("LUNCH ORDER").Select
Set ws = Worksheets("LUNCH ORDER")
lastRow = ws.Cells(Rows.Count, 3).End(xlUp).Row
For x = 2 To lastRow
If ws.Cells(x, 3).Values = empNum Then Rows(x).Select
End If
Next
rowSelect = ActiveCell.Row
Cells(rowSelect, 2) = Me.txt_Name.Value
Cells(rowSelect, 3) = Me.txt_Dept.Value
End If
End Sub
答案 0 :(得分:2)
如果您的声明与If
位于同一行,则不应遵循End If
。删除该行:
For x = 2 To lastRow
If ws.Cells(x, 3).Values = empNum Then Rows(x).Select
End If ' <--------------------------------------------- Remove this line
Next
同样纠正这个错误:
If ws.Cells(x, 3).Value = empNum Then Rows(x).Select
' ^^^^^^ not .Values