VBA code working in one workbook but not another

Makr1

New Member
Joined
Apr 5, 2017
Messages
4
Hi

I apologise if this has already been solved somewhere on this forum, however I am unable to find it.

I have two workbooks in which I am trying to use the same code. In one it is working but in the other it is not. I am using Excel 2007.

Here is the code:

Option Explicit
' Developed by Contextures Inc.
' www.contextures.com
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rngDV As Range
Dim oldVal As String
Dim newVal As String
Dim lUsed As Long
Dim lOld As Long
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
Else
If newVal = "" Then
'do nothing
Else
lOld = Len(oldVal)
If Left(newVal, lOld) = oldVal Then
Target.Value = newVal
Else
lUsed = InStr(1, oldVal, newVal)
If lUsed > 0 Then
Target.Value = oldVal
Else
Target.Value = oldVal _
& ", " & newVal
End If
End If
End If
End If
End If
End If
exitHandler:
Application.EnableEvents = True
End Sub


Thanks
 

Some videos you may like

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.

RCBricker

Well-known Member
Joined
Feb 4, 2003
Messages
1,560
In the first workbook (the one that works) where is the code found?

What about the second workbook?

This is a worksheet change event so it needs to be inserted into the worksheet VBE that will have the change that triggers the event.

Also please use code tags when posting code. Saves space on the page and is easier to read.

open code = [ code ] (no spaces)
close code = [ /code ] (no spaces).
 

Makr1

New Member
Joined
Apr 5, 2017
Messages
4
Thank you for your reply.

In both cases the code is in the object for the sheet required.
 

RCBricker

Well-known Member
Joined
Feb 4, 2003
Messages
1,560
ok what is it that the code is supposed to do? I am looking at it and it is a bit confusing.
 

Makr1

New Member
Joined
Apr 5, 2017
Messages
4
The code is supposed to allow multiple options to be selected in a dropdown menu. If an option is selected that has already been selected then it will not appear again in the cell. The options that have been selected will appear in the cell separated by commas.
 

RCBricker

Well-known Member
Joined
Feb 4, 2003
Messages
1,560
this might be a quick error fix. It is possible that the enable events has been set to false in the new workbook. open VBE and under the Thisworkbook insert the following:

Code:
Private Sub Workbook_Open()
Application.EnableEvents = True
End Sub
save close and reopen.

See if that fixes the issue.
 

Makr1

New Member
Joined
Apr 5, 2017
Messages
4
Thank you very much for the help. I have managed to get it working by using a slightly different code and your code.

The new code

Rich (BB code):
Option Explicit
Private Sub DTPicker1_CallbackKeyDown(ByVal KeyCode As Integer, ByVal Shift As Integer, ByVal CallbackField As String, CallbackDate As Date)
End Sub
' Developed by Contextures Inc.
' www.contextures.com
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rngDV As Range
Dim oldVal As String
Dim newVal As String
Dim lOld As Long
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
    Else
      If newVal = "" Then
        'do nothing
      Else
        lOld = Len(oldVal)
        If Left(newVal, lOld) = oldVal Then
          Target.Value = newVal
        Else
           Target.Value = oldVal _
                & ", " & newVal
        End If
      End If
    End If
  End If
End If
 
If newVal = "" Then
  'do nothing
Else
  lOld = Len(oldVal)
  If Left(newVal, lOld) = oldVal Then
    Target.Value = newVal
  Else
     Target.Value = oldVal _
          & ", " & newVal
  End If
End If

exitHandler:
  Application.EnableEvents = True
End Sub
 

Watch MrExcel Video

Forum statistics

Threads
1,095,350
Messages
5,443,948
Members
405,258
Latest member
daveyf

This Week's Hot Topics

Top