Excel单元格禁止输入重复值!


<span style="font-family: Arial, Helvetica, sans-serif; white-space: normal; background-color: rgb(255, 255, 255);">'如果想在某一列单元格中输入不重复的值,下列代码示例可以做到:</span>
Private Sub Worksheet_Change(ByVal Target As Range)
'A1-A8禁止输入已经录入过的值
Dim d
If Target.Count > 1 Then Exit Sub
If Target.Row < 1 Or Target.Rows > 8 Then Exit Sub
If Target.Column > 1 Then Exit Sub
Set d = CreateObject("scripting.dictionary")

For i = 1 To Cells(Rows.Count, 1).End(3).Row
  If Sheets(1).Cells(i, 1).Value <> "" And i <> CInt(Mid(Target.Address(0, 0), 2)) Then
    d(Sheets(1).Cells(i, 1).Value) = ""
  End If
Next
If Target.Value <> "" Then
If d.exists(Target.Value) Then
MsgBox "已录入过该值,重新录入"
Target.Value = ""
Else
d(Target.Value) = ""
End If
End If
End Sub


猜你喜欢

转载自blog.csdn.net/cxb2011/article/details/30709423