Help With VBA target range instead of cell

JohnBell79

New Member
Joined
Jul 19, 2023
Messages
7
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have managed to write a code to allow multiple selections in a cell from a drop down list. However It only works for 1 cell and not all the cells in that column. Whenever I try to apply a range to the code it stops working. Any tips appreciated.


Private Sub Worksheet_Change(ByVal Target As Range)
Dim old_val As String
Dim new_val As String
Application.EnableEvents = True
On Error GoTo Exitsub
If Target.Address = "$C$2" Then
If Target.SpecialCells(xlCellTypeAllValidation) Is Nothing Then
GoTo Exitsub
Else: If Target.Value = "" Then GoTo Exitsub Else
Application.EnableEvents = False
new_val = Target.Value
Application.Undo
old_val = Target.Value
If old_val = "" Then
Target.Value = new_val
Else
If InStr(1, old_val, new_val) = 0 Then
Target.Value = old_val & vbNewLine & new_val
Else:
Target.Value = old_val
End If
End If
End If
End If
Application.EnableEvents = True
Exitsub:
Application.EnableEvents = True
End Sub

Thanks
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Welcome to the Board!

If you want to apply it to all of column C, excluding row 1 (the header), try changing this line in your code:
VBA Code:
If Target.Address = "$C$2" Then
to this:
VBA Code:
If (Target.Column = 3) And (Target.Row > 1) Then
 
Upvote 0
Solution
Thank you! That worked perfectly. I knew it would be simple I just hit a stumbling block :)
 
Upvote 0
You are welcome.
Glad I was able to help!
 
Upvote 0
Note one other thing you may want to do before that line of code is add the following:
VBA Code:
If Target.CountLarge > 1 Then Exit Sub
That ensures that the code runs only when a single cell is updated at a time, and avoid errors when you do things like delete a whole row (which updates a whole row of cells at once).
 
Upvote 0
You may be able to assist with one more thing! from an aesthetic POV. The code is working perfectly however it is automatically wrapping the text in the cell and making me have to then unwrap it. Wondering if I can get in just to put a "," in between each selection??

1689778174724.png


Like this???
 
Upvote 0
You may be able to assist with one more thing! from an aesthetic POV. The code is working perfectly however it is automatically wrapping the text in the cell and making me have to then unwrap it. Wondering if I can get in just to put a "," in between each selection??

View attachment 95548

Like this???
That is really a whole different question, and as such it would be best to post it to a new question, so everyone can see it as a new unanswered questions in the "Unanswered threads" listing that many people use to look for new, unanswered questions to help on.
 
Upvote 0

Forum statistics

Threads
1,215,143
Messages
6,123,277
Members
449,093
Latest member
Vincent Khandagale

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top