Adding worksheet change event when there is already a SelectionChange event associated with worksheet

I am a VBA novice, so my apologies for this basic question. How do you add a General Declarations + Worksheet Change event to a worksheet (called IAP_Charts_Pub) that already has a Worksheet SelectionChange event associated with it? I tried pasting the Worksheet Change code above the Worksheet SelectionChange code associated with IAP_Charts_Pub, but that is triggering a debug error (at the line "Private Sub Worksheet_SelectionChange(ByVal Target As Range)"). I've included both event codes below:

PRE-EXISTING CODE (Worksheet SelectionChange)
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Column = 31 Or Target.Column = 48 Or Target.Column = 65 Or Target.Column = 82 Or Target.Column = 99 Or Target.Column = 116 Then ' Col AD
        If Target.Value Like "##-####" Then
            If Target.Value Like "##-9###" Then
             addDND = "DND\"
          Else
                addDND = ""
          End If
          Application.EnableEvents = False
          Target.Formula = "=HYPERLINK(""Q:\20" & Left(Target, 2) & "\" & addDND & Target.Value & Chr(34) & "," & Chr(34) & Target.Value & Chr(34) & ")"
          Application.EnableEvents = True
    End If
End If
End Sub

Open in new window

NEW EVENT CODE TO BE ADDED (General Declarations + Worksheet Change):
Option Explicit
Dim rng As Range
Dim cl As Object
Dim boolChange As Boolean
Dim x As Integer

Private Sub Worksheet_Change(ByVal Target As Range)
    If boolChange Then
        boolChange = False
        Exit Sub
    End If
    Set rng = Range("AD2", Range("AD2").End(xlDown))
    For Each cl In rng
        For x = 2 To 27 Step 5
            If cl.Offset(0, x) = "NEW" Or cl.Offset(0, x) = "REVISED" Then
                boolChange = True
                cl.Value = Format(Now(), "dd-mmm-yy")
                Exit For
            End If
        Next x
    Next cl
    boolChange = False
End Sub

Open in new window

Thanks,
Andrea
AndreamaryAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Martin LissOlder than dirtCommented:
I'm confused about what you want to do since SelectionChange and Change are different events that both already exist. SelectionChange occurs when you move from one cell to another and Change occurs after you leave a cell that you have changed.
0
AndreamaryAuthor Commented:
The SelectionChange event was a solution from this website (let's call it hyperlinking) that I applied to my worksheet a few months ago. Recently, I needed another VBA solution (let's call it auto-date update) for a different section of the same worksheet, which again was provided to me through this website. Now I need to know how to add the second VBA solution to my worksheet, given that there is already the existing code associated with this worksheet.

I hope that helps.

Andrea
0
AndreamaryAuthor Commented:
Further to the above, I combined the code (see below) for both solutions, and I'm getting a Compile Error at line 29 for the phrase "addDND":

Option Explicit
Dim rng As Range
Dim cl As Object
Dim boolChange As Boolean
Dim x As Integer

Private Sub Worksheet_Change(ByVal Target As Range)
    If boolChange Then
        boolChange = False
        Exit Sub
    End If
    Set rng = Range("AD2", Range("AD2").End(xlDown))
    For Each cl In rng
        For x = 2 To 27 Step 5
            If cl.Offset(0, x) = "NEW" Or cl.Offset(0, x) = "REVISED" Then
                boolChange = True
                cl.Value = Format(Now(), "dd-mmm-yy")
                Exit For
            End If
        Next x
    Next cl
    boolChange = False
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Column = 31 Or Target.Column = 48 Or Target.Column = 65 Or Target.Column = 82 Or Target.Column = 99 Or Target.Column = 116 Then ' Col AD
        If Target.Value Like "##-####" Then
            If Target.Value Like "##-9###" Then
             addDND = "DND\"
          Else
                addDND = ""
          End If
          Application.EnableEvents = False
          Target.Formula = "=HYPERLINK(""Q:\20" & Left(Target, 2) & "\" & addDND & Target.Value & Chr(34) & "," & Chr(34) & Target.Value & Chr(34) & ")"
          Application.EnableEvents = True
    End If
End If
End Sub

Open in new window

0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Martin LissOlder than dirtCommented:
I'm still not sure I understand the question but you can use Intersect to do different things.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(ActiveCell, Range("A1:A6")) Is Nothing Then
    MsgBox "I selected " & Target.Address
Else
    MsgBox "I'm somewhere else"
End If
End Sub

Open in new window

0
Martin LissOlder than dirtCommented:
What is addDND?
0
Martin LissOlder than dirtCommented:
It also might help if you attached your workbook and described in words, rather than code, what you are trying to do.
0
AndreamaryAuthor Commented:
Good idea, Martin. I've created a sample worksheet to which I want to add the following functionality:

If the value in Column AF is updated to "NEW" or "REVISED", then the date in Column AD changes to "13 Nov 14".

I would like to have this solution in both VBA and as a macro, as I may decide to go the macro route.

Let me know if you need anything further...thanks!

 Thanks,
 Andrea
Sample-UpdateEFFDate.xlsm
0
Martin LissOlder than dirtCommented:
I'm going out for lunch and will get back to you within a couple of hours.
0
Martin LissOlder than dirtCommented:
Here's your workbook updated with a Worksheet_Change event that looks like this
Private Sub Worksheet_Change(ByVal Target As Range)

If Not Intersect(ActiveCell, Range("AF2:AF" & ActiveSheet.UsedRange.Rows.Count)) Is Nothing Then
    Select Case UCase(Target.Value)
        Case "NEW", "REVISED"
            Range("AD" & Target.Row).Value = #8/13/2014#
    End Select
End If
End Sub

Open in new window


and a macro (in Module1) that looks like this
Sub NewRevised()
Dim lngRow As Long

For lngRow = 2 To ActiveSheet.UsedRange.Rows.Count
    Select Case UCase(Cells(lngRow, 32).Value) ' 32 = row AF
        Case "NEW", "REVISED"
            Range("AD" & lngRow).Value = #8/13/2014#
    End Select
Next
End Sub

Open in new window

Q-28497661.xlsm
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AndreamaryAuthor Commented:
Terrific, Martin. All good. Thanks for your patience!
0
Martin LissOlder than dirtCommented:
You're welcome and I'm glad I was able to help. Note that while there's noting wrong with the way you coded your SelectionChange event, line 26 in post ID : 40261203 it could have been more easily coded using a Select Case structure like I did in my ChangeEvent code.

In my profile you'll find links to some articles I've written that may interest you.
Marty - MVP 2009 to 2014
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.