+ Reply to Thread
Results 1 to 5 of 5

Combine two Worksheet_Change Codes

  1. #1
    Registered User
    Join Date
    06-10-2014
    Posts
    5

    Combine two Worksheet_Change Codes

    Hi guys. I know that I can't run two worksheet_change events in one workbook. I was wondering if anyone can help me combine these two codes into one.

    I found these from contextures and want to implement them in a personal worksheet.

    The first one will allows the user to double click a data validation cell and type in a new entry which is added to the list.

    The second one allows me to add multiple entries from a data validation list into one cell.

    Ideally I would like the first code to apply to all of my data validation columns while the second code is only needed for one column. (i.e. all my columns would have the first code while one column had both.)


    Private Sub Worksheet_Change(ByVal Target As Range)
    On Error Resume Next
    Dim ws As Worksheet
    Dim str As String
    Dim i As Integer
    Dim rngDV As Range
    Dim rng As Range

    If Target.Count > 1 Then Exit Sub
    Set ws = Worksheets("Lists")

    If Target.Row > 1 Then
    On Error Resume Next
    Set rngDV = Cells.SpecialCells(xlCellTypeAllValidation)
    On Error GoTo 0
    If rngDV Is Nothing Then Exit Sub

    If Intersect(Target, rngDV) Is Nothing Then Exit Sub

    str = Target.Validation.Formula1
    str = Right(str, Len(str) - 1)
    On Error Resume Next
    Set rng = ws.Range(str)
    On Error GoTo 0
    If rng Is Nothing Then Exit Sub

    If Application.WorksheetFunction _
    .CountIf(rng, Target.Value) Then
    Exit Sub
    Else
    i = ws.Cells(Rows.Count, rng.Column).End(xlUp).Row + 1
    ws.Cells(i, rng.Column).Value = Target.Value
    rng.Sort Key1:=ws.Cells(1, rng.Column), _
    Order1:=xlAscending, Header:=xlNo, _
    OrderCustom:=1, MatchCase:=False, _
    Orientation:=xlTopToBottom
    End If

    End If

    End Sub





    Option Explicit

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim rngDV As Range
    Dim oldVal As String
    Dim newVal As String
    Dim lUsed 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
    lUsed = InStr(1, oldVal, newVal)
    If lUsed > 0 Then
    If Right(oldVal, Len(newVal)) = newVal Then
    Target.Value = Left(oldVal, Len(oldVal) - Len(newVal) - 2)
    Else
    Target.Value = Replace(oldVal, newVal & ", ", "")
    End If
    Else
    Target.Value = oldVal _
    & ", " & newVal
    End If

    End If
    End If
    End If
    End If

    exitHandler:
    Application.EnableEvents = True
    End Sub


    Thank you so much guys.

  2. #2
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: Combine two Worksheet_Change Codes

    What version of Excel are you using?
    Hope this helps

    Sometimes its best to start at the beginning and learn VBA & Excel.

    Please dont ask me to do your work for you, I learnt from Reading books, Recording, F1 and Google and like having all of this knowledge in my head for the next time i wish to do it, or wish to tweak it.
    Available for remote consultancy work PM me

  3. #3
    Registered User
    Join Date
    06-10-2014
    Posts
    5

    Re: Combine two Worksheet_Change Codes

    Excel 2010

  4. #4
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: Combine two Worksheet_Change Codes

    Please Login or Register  to view this content.
    use some logic on the sh, the sheet object to determine which route to take.

  5. #5
    Registered User
    Join Date
    06-10-2014
    Posts
    5

    Re: Combine two Worksheet_Change Codes

    Thanks! That will help me out alot.

    Is there any way to make both codes applicable to one column though? I have one column where I will need to be able to add in new entries and the ability to add multiple old entries from my data validation list.

+ 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] Combine two Worksheet_Change macros
    By moongazer208 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-11-2013, 06:14 AM
  2. Combine 2 worksheet_change event macros
    By RebelVampire in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-08-2013, 12:37 AM
  3. Combine Private Sub Worksheet_Change VBA Code on same worksheet
    By stanlelma in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-06-2013, 12:42 PM
  4. [SOLVED] How to Combine 3 instances of Worksheet_Change(ByVal Target As Range)
    By Grumpygrandad in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-04-2013, 11:12 PM
  5. How to combine two Worksheet_Change subs into one?
    By zozo23 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-26-2012, 09:07 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