+ Reply to Thread
Results 1 to 24 of 24

Combine Worksheet_SelectionChange and Worksheet_Change events

  1. #1
    Registered User
    Join Date
    12-21-2012
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    16

    Combine Worksheet_SelectionChange and Worksheet_Change events

    Hello,

    I'm trying to merge a worksheet selection change event and a worksheet change event but don't seem to be getting anywhere. Each one will work without the other fine - but when I try to merge them only the Worksheet_change event works.

    The first macro I found on Contextures.com which allows the user to make multiple selections from a drop down list

    Please Login or Register  to view this content.
    The second macro allocates the active cell in a particular range the Name "ActiveP". This is then used to work out a persons age on a particular date.

    Please Login or Register  to view this content.
    I've searched the forums and found examples of multiple Worksheet_change events that have been merged but when I have tried to do something similar to combine these two events I am experiencing an issue where only the worksheet change event works and Excel seems to revert any new dates entered into american date format rather than the default UK format. I have no idea why it would change the date format?

    Any suggestions as to how to merge these two events successfully would be greatly welcome!

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Combine Worksheet_SelectionChange and Worksheet_Change events

    Do you really need to 'merge' them?

    SelectionChange and Change are 2 separate events that are triggered by different actions.
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    12-21-2012
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    16

    Re: Combine Worksheet_SelectionChange and Worksheet_Change events

    Hi Norie,

    That is what I originally thought.
    But having searched forums the suggestion seemed to be that a worksheet can only have 1 worksheet change event. This seemed to be confirmed to me by the fact that if I remove one of these events the other one works fine.
    Am I missing something?

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Combine Worksheet_SelectionChange and Worksheet_Change events

    A worksheet can only have one Change event, but you have two separate events - Change and SelectionChange.

    The former is triggered when a manual change is made to a value/values on the sheet, the latter when the selection changes on the sheet.

  5. #5
    Registered User
    Join Date
    12-21-2012
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    16

    Re: Combine Worksheet_SelectionChange and Worksheet_Change events

    Apologies maybe I've incorrectly worded my problem...my issue is that I am trying to run these two events but I am finding that not both will run at the same time. if I remove one of the events the other seems to work fine.

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Combine Worksheet_SelectionChange and Worksheet_Change events

    They are 2 different events triggered by 2 different actions, they won't run at the same time.

    That's not to say they might not interfere with each other, perhaps even call each other.

    What is the purpose of the second, SelectionChange, code anyway?

  7. #7
    Registered User
    Join Date
    12-21-2012
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    16

    Re: Combine Worksheet_SelectionChange and Worksheet_Change events

    Again apologies for my wording.

    Cells F10:F227 contain a date and the SelectionChange code assigns any selected cell in this range the name ActiveP. The ActiveP value is then used to calculate the age as of that selected date.

  8. #8
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Combine Worksheet_SelectionChange and Worksheet_Change events

    Do you only want that code to run when a selection is made in that range?

    No change is being made in that range?

    By the way, what's happening, or perhaps more importantly not happening, when you have the 2 codes separately in the worksheet module?

  9. #9
    Registered User
    Join Date
    12-21-2012
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    16

    Re: Combine Worksheet_SelectionChange and Worksheet_Change events

    Yes I only want the SelectionChange event to run when the active cell is the range F10:F227. Changes to the cells in this range can be made but the event should only be triggered when there is a change in the active cell.

    When both of the event codes are in the worksheet module the selectionchange event seems to be working ok but the worksheet_change event (which is the drop down list multi-select) does not seem to be working at all.

    If I remove the selectionchange event the worksheet_change event seems to work perfectly.

  10. #10
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Combine Worksheet_SelectionChange and Worksheet_Change events

    Sorry to ask again, when exactly do you want the code in the SelectionChange event to run?

    Is it when a cell in the range F10:F227 is manually changed or is it when a cell is selected in that range?

    If it's the former it should be easy to combine the 2 codes.

  11. #11
    Registered User
    Join Date
    12-21-2012
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    16

    Re: Combine Worksheet_SelectionChange and Worksheet_Change events

    I would like the selectionchange event to run when a cell in the range F10:F227 is selected.
    So they are as you first stated two different events triggered by two different actions.

  12. #12
    Registered User
    Join Date
    12-21-2012
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    16

    Re: Combine Worksheet_SelectionChange and Worksheet_Change events

    Out of curiousity would I be able to use

    Please Login or Register  to view this content.
    In the workbook module in which to have my selectionchange event for that particular worksheet?

  13. #13
    Registered User
    Join Date
    12-21-2012
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    16

    Re: Combine Worksheet_SelectionChange and Worksheet_Change events

    Scrap that idea - it has the same result. Only the selectionchange event runs

  14. #14
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Combine Worksheet_SelectionChange and Worksheet_Change events

    Are you sure the Change event isn't being run?

    You can check this by putting a break point on each sub's heading using F9.

    Once you've done that go back to the sheet and test.

    PS Any chance you could upload a sample workbook?

    Then we might be able to see why things aren't working as expected.

    Click on GO ADVANCED and use the paperclip icon to open the upload window.

  15. #15
    Registered User
    Join Date
    12-21-2012
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    16

    Re: Combine Worksheet_SelectionChange and Worksheet_Change events

    Hi Norie,
    Please find attached a sample workbook.
    Cells B2 and E3 are the ones where I would like to be able to utilise the multi-select worksheet_change event.
    Any cells selected in range A5:A20 are then used to provide the ActiveP value.
    Following your suggestion of using a break point I noticed when I try to select an additional value from the multiselect the code in the worksheet_change event jumps from "On Error GoTo exitHandler" to the Worksheet_SelectionChange event.
    Attached Files Attached Files

  16. #16
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,905

    Re: Combine Worksheet_SelectionChange and Worksheet_Change events

    Try this
    I move the Application.Enablevents around a little.
    I haven't really taken a look at what you want to accomplish but this is just a quick-and-dirty test
    Attached Files Attached Files
    ---
    Hans
    "IT" Always crosses your path!
    May the (vba) code be with you... if it isn't; start debugging!
    If you like my answer, Click the * below to say thank-you

  17. #17
    Registered User
    Join Date
    12-21-2012
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    16

    Re: Combine Worksheet_SelectionChange and Worksheet_Change events

    Hi Keebellah - thanks for the contribution!
    I've now found that in the worksheet_change event the code jumps from this statement "If Not Intersect(Target, rngDV) Is Nothing Then" to the end of the if statement however removing the "Not" here seems to have the desired affect.
    Does anyone know what this statement is saying in lay terms? The combination of the "Not" and "Is Nothing" is a little confusing.

  18. #18
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Combine Worksheet_SelectionChange and Worksheet_Change events

    It says if Target, the cell(s) that have changed, is within the range rngDV refers to continue with the code within the If...End If block.

  19. #19
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Combine Worksheet_SelectionChange and Worksheet_Change events

    Try replacing the code in the worksheet module with this.
    Please Login or Register  to view this content.

  20. #20
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,905

    Re: Combine Worksheet_SelectionChange and Worksheet_Change events

    Not .... Is Nothing means that the result is positive, the range is not empty
    Yes it is a little confusing but it works and you could even use the select case statement here

  21. #21
    Registered User
    Join Date
    12-21-2012
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    16

    Re: Combine Worksheet_SelectionChange and Worksheet_Change events

    Apologies for the delay managed to get it working, thank you kindly all

  22. #22
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,905

    Re: Combine Worksheet_SelectionChange and Worksheet_Change events

    Good to know.
    Happy to have been of assistance too

  23. #23
    Registered User
    Join Date
    10-29-2019
    Location
    USA
    MS-Off Ver
    2016
    Posts
    3

    Re: Combine Worksheet_SelectionChange and Worksheet_Change events

    I have been experiencing the same problem of "Combine Worksheet_SelectionChange and Worksheet_Change events"
    Im fairly new to all this coding and cant seem to find a way to combine them into one. My goal is to have a date picker in any columns i choose and then any DATA validation lists to have multiple selcetions. I can only get one at a time to work. Any guidence in the right direction would be appreciated. I psated my code below (Not sure if I did that properly for this forun.





    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 oldVal = "" Then
    'do nothing
    Else
    If newVal = "" Then
    'do nothing
    Else
    Target.Value = oldVal _
    & ", " & newVal
    End If
    End If

    End If

    exitHandler:
    Application.EnableEvents = True
    End Sub



    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    With Sheet1.DTPicker1
    .Height = 20
    .Width = 20
    If Not Intersect(Target, Range("F4:F50,I4:I50")) Is Nothing Then
    .Visible = True
    .Top = Target.Top
    .Left = Target.Offset(0, 1).Left
    .LinkedCell = Target.Address
    Else
    .Visible = False
    End If
    End With

  24. #24
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    52,942

    Re: Combine Worksheet_SelectionChange and Worksheet_Change events

    [QUOTE=ifreeroam;5257321]I have been experiencing the same problem of "Combine Worksheet_SelectionChange and Worksheet_Change events"
    Im fairly new to all this coding and cant seem to find a way to combine them into one. My goal is to have a date picker in any columns i choose and then any DATA validation lists to have multiple selcetions. I can only get one at a time to work. Any guidence in the right direction would be appreciated. I psated my code below (Not sure if I did that properly for this forun.....

    Administrative Note:

    Welcome to the forum.

    We are happy to help, however whilst you feel your request is similar to this thread, experience has shown that things soon get confusing when answers refer to particular cells/ranges/sheets which are unique to your post and not relevant to the original.

    Please see Forum Rule #4 about hijacking and start a new thread for your query.

    If you are not familiar with how to start a new thread see the FAQ: How to start a new thread
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools
    3. Click on the star if you think someone helped you

    Regards
    Ford

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Combing two events Worksheet_Change
    By MTS4 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-13-2015, 11:38 AM
  2. easy .... Combine 2 Worksheet_Change Events
    By bushybob in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 05-20-2015, 10:20 AM
  3. Combining two Worksheet_Change Events
    By Fett2oo5 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 09-09-2013, 01:05 PM
  4. [SOLVED] Merging Two Worksheet_Change Events
    By swordswinger710 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 05-04-2012, 04:18 PM
  5. WorkSheet_Change or Events
    By fsgg69 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-28-2010, 04:09 AM
  6. Replies: 0
    Last Post: 01-21-2006, 11:20 PM
  7. [SOLVED] Worksheet_Change or Worksheet_SelectionChange
    By Bret in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-26-2005, 08:05 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1