我在表單上設了一個新增按鈕,如果不小心連續按了好幾次-.-
資料就會新增了好幾筆,請問要如何避免重複新增資料的問題??
'員工編號不重複
Private Sub no_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Sheets("人事資料").Select
If KeyCode = vbKeyTab Or KeyCode = vbKeyReturn Then
If no.Text <> "" And Application.WorksheetFunction.CountIf(Columns(1), no.Text) >= 1 Then
no.Text = ""
KeyCode = 0
End If
End If
End Sub
'新增人事資料
Private Sub add_Click()
Sheets("人事資料").Select
Range("A1").Select
Range("A65536").Select
Selection.End(xlUp).Select
ActiveCell.Offset(0, 0).Range("A2") = no.Text
ActiveCell.Offset(0, 0).Range("B2") = fullname.Text
ActiveCell.Offset(0, 0).Range("c2") = birthday.Text
ActiveCell.Offset(0, 0).Range("d2") = id.Text
End Sub作者: luhpro 時間: 2010-9-16 22:03
'假设 ID.Text是"员工编号",先对 ID.Text进行判断,是否在d列存在.
Private Sub add_Click()
With Sheets("人事資料")
If Application.WorksheetFunction.CountIf(.Range("d:d"), ID.Text) > 0 Then
MsgBox "员工编号已存在!", vbCritical, "错误"
Exit Sub
End If
With .Range("A65536").End(xlUp)
.Range("A2") = no.Text
.Range("B2") = FullName.Text
.Range("c2") = birthday.Text
.Range("d2") = ID.Text
End With
End With
End Sub
'没有附件,没有验证过哈.提问最好要有附件作者: solely 時間: 2010-9-17 00:16