Duette
New Member
- Joined
- Feb 19, 2009
- Messages
- 17
I've been trying to find a way to use the MID and LEN functions in VB. I have an Excel 2003 spreadsheet that uses multiple dropdown selections placed in one cell. What I would like to do is every time the user accidentally selects something that has already been put in the cell it returns nothing, as opposed to placing a duplicate in the cell. When there are duplicates there's nothing you can do but delete the entire cell and start over with your selections. Here's my code:
The line of code that gets tripped is bolded. Any suggestions?
Rich (BB code):
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rngDV As Range
Dim oldVal As String
Dim newVal As String
If Target.Count > 1 Then GoTo exitHandler
On Error Resume Next
Set rngDV = Cells.SpecialCells(xlCellTypeAllValidation)
On Error GoTo exitHandler
If rngDV Is Nothing Then GoTo exitHandler
If Intersect(Target, rngDV) Is Nothing Then
'do nothing
Else
Application.EnableEvents = False
newVal = Target.Value
Application.Undo
oldVal = Target.Value
Target.Value = newVal
If Target.Column = 3 Then
If oldVal = "" Then
'do nothing
ElseIf newVal = Application.WorksheetFunction.Mid(oldVal, _
Application.WorksheetFunction.Find(newVal, oldVal), _
Application.WorksheetFunction.Len(oldVal)) Then
'do nothing
ElseIf newVal = "" Then
'do nothing
Else
Target.Value = oldVal & "," & Chr(10) & newVal
End If
End If
End If
exitHandler:
Application.EnableEvents = True
End Sub
The line of code that gets tripped is bolded. Any suggestions?