I have found a code online to be able to choose multiple items from a drop down list in excel. The field shows a list of counties. This works to let me choose one or more items, each separated by the word "And".
Is there any way to change when there are three or more chosen to show commas as the separator for all but the last one? Example
Able AND Baker AND Charlie and Delta
would become
Able, Baker, Charlie, and Delta
If there were only two selected it would just be Able and Baker
If that can be accomplished, is there anyway to repeat the action in another cell. I am currently using it in cell B4 but would like to repeat the process in cell B27.
Below is the code that I am using that I found online. I would appreciate any help because I really don't understand how to write vba.
Fred
Private Sub Worksheet_Change(ByVal Target As Range)
'Code by Sumit Bansal from Online Excel Tips & Tutorials
' To make mutliple selections in a Drop Down List in Excel
Dim Oldvalue As String
Dim Newvalue As String
On Error GoTo Exitsub
If Target.Address = "$B$4" Then
If Target.SpecialCells(xlCellTypeAllValidation) Is Nothing Then
GoTo Exitsub
Else: If Target.Value = "" Then GoTo Exitsub Else
Application.EnableEvents = False
Newvalue = Target.Value
Application.Undo
Oldvalue = Target.Value
If Oldvalue = "" Then
Target.Value = Newvalue
Else
Target.Value = Oldvalue & " and " & Newvalue
End If
End If
End If
Application.EnableEvents = True
Exitsub:
Application.EnableEvents = True
End Sub
Is there any way to change when there are three or more chosen to show commas as the separator for all but the last one? Example
Able AND Baker AND Charlie and Delta
would become
Able, Baker, Charlie, and Delta
If there were only two selected it would just be Able and Baker
If that can be accomplished, is there anyway to repeat the action in another cell. I am currently using it in cell B4 but would like to repeat the process in cell B27.
Below is the code that I am using that I found online. I would appreciate any help because I really don't understand how to write vba.
Fred
Private Sub Worksheet_Change(ByVal Target As Range)
'Code by Sumit Bansal from Online Excel Tips & Tutorials
' To make mutliple selections in a Drop Down List in Excel
Dim Oldvalue As String
Dim Newvalue As String
On Error GoTo Exitsub
If Target.Address = "$B$4" Then
If Target.SpecialCells(xlCellTypeAllValidation) Is Nothing Then
GoTo Exitsub
Else: If Target.Value = "" Then GoTo Exitsub Else
Application.EnableEvents = False
Newvalue = Target.Value
Application.Undo
Oldvalue = Target.Value
If Oldvalue = "" Then
Target.Value = Newvalue
Else
Target.Value = Oldvalue & " and " & Newvalue
End If
End If
End If
Application.EnableEvents = True
Exitsub:
Application.EnableEvents = True
End Sub