+ Reply to Thread
Results 1 to 9 of 9

Merging 2 VBA Codes

Hybrid View

crusader0011 Merging 2 VBA Codes 04-23-2013, 10:38 AM
crusader0011 Re: Merging 2 VBA Codes 04-27-2013, 06:54 PM
abousetta Re: Merging 2 VBA Codes 04-27-2013, 07:02 PM
crusader0011 Re: Merging 2 VBA Codes 04-29-2013, 06:01 AM
crusader0011 Re: Merging 2 VBA Codes 04-29-2013, 09:49 AM
crusader0011 Re: Merging 2 VBA Codes 04-29-2013, 01:56 PM
abousetta Re: Merging 2 VBA Codes 04-29-2013, 02:12 PM
crusader0011 Re: Merging 2 VBA Codes 04-30-2013, 05:30 PM
abousetta Re: Merging 2 VBA Codes 04-30-2013, 05:36 PM
  1. #1
    Registered User
    Join Date
    11-02-2010
    Location
    Los Angeles
    MS-Off Ver
    Ms Excel 2010
    Posts
    6

    Merging 2 VBA Codes

    Hi Guys,

    I'm new to VBA coding, so please bare with me

    I want to implement multiple selections within several drop down list, after googling and searching here and there i managed to find the following coding lines which work great in any given column:

    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 = 9 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
    Now, The issue is that I want the multiple selection to apply to at least 2 Columns, I've tried several combinations wit If/End if/Else & Else If, and after cleaning up I seem to disable the loop effect,

    Can anyway help?

    kindest regards!

    Cruz
    Last edited by arlu1201; 04-23-2013 at 11:37 AM. Reason: Use code tags in future.

  2. #2
    Registered User
    Join Date
    11-02-2010
    Location
    Los Angeles
    MS-Off Ver
    Ms Excel 2010
    Posts
    6

    Re: Merging 2 VBA Codes

    Hi Guys, Can anyone help?

  3. #3
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Merging 2 VBA Codes

    I can try to look at it later, but I'm not sure what you are trying to do. Could you elaborate in words and post a sample workbook? You'll tend to get more responses if you take both actions.

    Good luck.

    abousetta
    Please consider:

    Thanking those who helped you. Click the star icon in the lower left part of the contributor's post and add Reputation.
    Cleaning up when you're done. Mark your thread [SOLVED] if you received your answer.

  4. #4
    Registered User
    Join Date
    11-02-2010
    Location
    Los Angeles
    MS-Off Ver
    Ms Excel 2010
    Posts
    6

    Re: Merging 2 VBA Codes

    Thanks abousetta,

    the following Code:

    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 = 9 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
    enables me to apply mutliple selections out of a Drop Down List but only in Column '9', now what I need is to apply the macro to Column 8 & 9 at once, I tried to replicate the code inside the code using IF and END IF but somehow it always ends up applying the macro only to one Column,

    thank you for your help
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    11-02-2010
    Location
    Los Angeles
    MS-Off Ver
    Ms Excel 2010
    Posts
    6

    Re: Merging 2 VBA Codes

    Can someone help please?

  6. #6
    Registered User
    Join Date
    11-02-2010
    Location
    Los Angeles
    MS-Off Ver
    Ms Excel 2010
    Posts
    6

    Re: Merging 2 VBA Codes

    +1
    Can anyway assist me?

  7. #7
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Merging 2 VBA Codes

    Hi,

    I can't test at the moment, but replace this line

      If Target.Column = 9 Then
    with

      If Target.Column = 8 or Target.Column = 9 Then
    abousetta

  8. #8
    Registered User
    Join Date
    11-02-2010
    Location
    Los Angeles
    MS-Off Ver
    Ms Excel 2010
    Posts
    6

    Re: Merging 2 VBA Codes

    Work Great, Thanks!

  9. #9
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Merging 2 VBA Codes

    Thanks for the feedback.

    Good luck.

    abousetta

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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