Results 1 to 18 of 18

Thread: New to VBA and require assistance modifying code.

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    VBAX Regular
    Joined
    Jun 2024
    Posts
    9
    Location

    New to VBA and require assistance modifying code.

    Hi,

    Could someone assist me in modifying the code below so that it uses a line break instead of a comma delimited, I want to insert this into different cells, not just one specific one.

    
    Private Sub Worksheet_Change(ByVal Target As Range)
    'Code by Sumit Bansal from https://trumpexcel.com
    ' To allow multiple selections in a Drop Down List in Excel (without repetition)
    '  Edited to allow deselection of item (courtesy of Jamie Counsell)
    Application.EnableEvents = True
    On Error GoTo Exitsub
    If Target.Address = "$A$14" Then
        If Target.SpecialCells(xlCellTypeAllValidation) Is Nothing Then
            GoTo Exitsub
        ElseIf Target.Value = "" Then
            GoTo Exitsub
        Else
            Application.EnableEvents = False
            Newvalue = Target.Value
            Application.Undo
            Oldvalue = Target.Value
            Target.Value = Newvalue
           If Oldvalue <> "" Then
                If Newvalue <> "" Then
                    If InStr(1, Oldvalue, ", " & Newvalue & ",") > 0 Then
                        Oldvalue = Replace(Oldvalue, Newvalue & ", ", "") ' If it's in the middle with comma
                        Target.Value = Oldvalue
                        GoTo jumpOut
                    End If
                    If Left(Oldvalue, Len(Newvalue & ", ")) = Newvalue & ", " Then
                        Oldvalue = Replace(Oldvalue, Newvalue & ", ", "") ' If it's at the start with comma
                        Target.Value = Oldvalue
                        GoTo jumpOut
                    End If
                    If Right(Oldvalue, Len(", " & Newvalue)) = ", " & Newvalue Then
                        Oldvalue = Left(Oldvalue, Len(Oldvalue) - Len(", " & Newvalue)) ' If it's at the end with a comma in front of it
                        Target.Value = Oldvalue
                        GoTo jumpOut
                    End If
                    If Oldvalue = Newvalue Then ' If it is the only item in string
                        Oldvalue = ""
                        Target.Value = Oldvalue
                        GoTo jumpOut
                    End If
                    Target.Value = Oldvalue & ", " & Newvalue
                End If
    jumpOut:
            End If
        End If
    End If
    Application.EnableEvents = True
    Exitsub:
    Application.EnableEvents = True
    End Sub
    
    [/CODE]

    I have posted this on the following Group, https://chandoo.org/forum/threads/mu...own-box.57155/
    Last edited by SOUL; 06-13-2024 at 04:25 AM.

Posting Permissions

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