View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
OssieMac OssieMac is offline
external usenet poster
 
Posts: 2,510
Default Allow only one value in range.

Hi Michael,

Try the following. There was no test for additional values equal to input
value. Also inputval was dimensioned then inputvalue used in the code. If you
include Option Explicit at the very top of the VBA editor page and then
before running the code, Select Debug - Compile. The variables not properly
dimensioned will be highlighted before running the code.

The On Error should be used with Application.EnableEvents = False. If you
get an error in the code and it does not complete running to
Application.EnableEvents = True then events remain turned off until you
either turn them on with code or restart Excel.

Private Sub Worksheet_Change(ByVal Target As Range)
Dim isec As Range
Dim inputvalue As Variant

inputvalue = Target.Value
Set isec = Application.Intersect _
(Range("eventRngStable"), Target)

On Error GoTo ReEnableEvents
Application.EnableEvents = False
If isec Is Nothing Then
MsgBox "out of range"
Else

If WorksheetFunction.CountIf _
(Range("eventRngStable"), _
inputvalue) 1 Then

Range("eventRngStable").ClearContents
Target.Value = inputvalue
End If

End If

ReEnableEvents:
Application.EnableEvents = True

End Sub


--
Regards,

OssieMac