1

The statement If Target.Address = "$I$2" Then selects just the I2 cell.

How can I select the entire column instead of just one cell? Here is my code

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
'Code by Sumit Bansal from https://trumpexcel.com
' To Select Multiple Items from a Drop Down List in Excel
Dim Oldvalue As String
Dim Newvalue As String
Application.EnableEvents = True
On Error GoTo Exitsub
If Target.Address = "$I$2" 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
        If InStr(1, Oldvalue, Newvalue) = 0 Then
            Target.Value = Oldvalue & ", " & Newvalue
      Else:
        Target.Value = Oldvalue
      End If
    End If
  End If
End If
Application.EnableEvents = True
Exitsub:
Application.EnableEvents = True
End Sub
3
  • Hint: Range() Commented Oct 17, 2017 at 15:58
  • ok so like range(I:I) ? Commented Oct 17, 2017 at 16:01
  • If Target.Address = "$I$2" Then does not select a cell. ... it only checks if Target's address is I2 Commented Oct 17, 2017 at 16:33

1 Answer 1

1

You probably want to check the entire column "I" instead of cell "I2". This condition shall be changed from

    If Target.Address = "$I$2" Then

to

    If Target.Column = 9 Then

So that macro will consider it for complete column.

Sign up to request clarification or add additional context in comments.

9 Comments

you're a genius! thank you. Is there a way to select the entire sheet? or more than one column?
If you want to do it for complete sheet then just remove this condition. You will have to explain more than one part by suitable example as code may differ.
"You will have to explain more than one part by suitable example as code may differ." - Could I write ` If Target.Column = 9,10 Then` ? Should I copy and paste the code one column at a time?
There are many ways to write this e.g. If Target.Column = 9 Or Target.Column = 10 Then However this may become complicated when you have many such conditions.
@cucombrehombre - please accept the answer if it has solved your problem.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.