防止在列范围-excel VBA中输入超过给定次数的字符数

时间:2016-08-26 18:22:46

标签: excel excel-vba vba

这是我的excel表格示例:

enter image description here

Private Sub test2()

Dim count As Long
Dim a As String
Dim Act As Integer

Worksheets("test").Activate
a = ActiveCell.Offset(0, -1).Value
' "count" counts the occorance of "b"
count = Application.WorksheetFunction.CountIf(Range("A2:A16"), a)
Act = ActiveCell
If count > Act Then
MsgBox "exceeded"
Else
 MsgBox count
End If

End Sub
`

1 个答案:

答案 0 :(得分:0)


需要的步骤
1。你需要Worksheet_Change Event
2。一旦你明白你需要做以下的事情

Private Sub Worksheet_Change(ByVal Target As Range)
Dim ItemMultipleData As Range
For Each ItemMultipleData In Target 'handles multiple cells, paste, del, etc
If Not Intersect(ItemMultipleData, Columns(1)) Is Nothing Then
If Application.WorksheetFunction.CountIf(Columns(1), ItemMultipleData.Value) > 3 Then MsgBox ("Not allowed"): ItemMultipleData.Value = ""
End If
Next ItemMultipleData
End sub