2016-08-26 50 views
0

這裏是我的excel表例如:防止在列中輸入次數超出給定數量更多的字符範圍,Excel的VBA

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 
` 
+2

你的問題是? –

+0

@JohnColeman我認爲標題本身就是個問題,嘿嘿 – Sgdva

+1

@Sddva無疑與它有關,但是代碼的問題是什麼?我沒有動力來運行它,並猜測*問題是什麼。 –

回答

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 
相關問題