VBA help set Target.Row based weather it contains text

djalexr

New Member
Joined
Jan 14, 2011
Messages
33
Hi there! I'm trying to modify a bit of VBA code from Mr Trump Excel that allows the user to select more than one value from a drop down list. Each time you select a value, it adds a new one so you end up with many values in a single cell.

Currently, this code will run on any cells that are above Row 5 and above Column 6 ("If Target.Row > 5 And Target.Column > 6 Then...")

However, I don't want this, what I actually need is for the code to run ONLY on specific rows - only those rows that contain the text string "Including".


VBA Code:
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)
Dim Oldvalue As String
Dim Newvalue As String
Application.EnableEvents = True
On Error GoTo Exitsub
If Target.Row > 5 And Target.Column > 6 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 & vbNewLine & "• " & Newvalue
      Else:
        Target.Value = Oldvalue
      End If
    End If
  End If
End If
Application.EnableEvents = True
Exitsub:
Application.EnableEvents = True
End Sub


Is it possible to tweak this code so that it runs based on finding a string value?

An altrernative would be to specificy each individual row by its number, but there will be a lot so this would be a nightmare.

Thanks So much!

Alex
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
However, I don't want this, what I actually need is for the code to run ONLY on specific rows - only those rows that contain the text string "Including".
In which column is the word "Including" found in?
And in those cells, is the word "Including" by itself, or might it be combined with other words?
 
Upvote 0
In which column is the word "Including" found in?
And in those cells, is the word "Including" by itself, or might it be combined with other words?
It's column "E". And yes the word is just "Including" by itself, with no other words. Thanks for responding!
 
Upvote 0
See if this modification does what you want:
VBA Code:
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)
Dim Oldvalue As String
Dim Newvalue As String

On Error GoTo Exitsub

' Exit if multiple cells updated at once
If Target.CountLarge > 1 Then Exit Sub

' Check to see if column E in Target row is "Including"
If Cells(Target.Row, "E") = "Including" 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 & vbNewLine & "• " & Newvalue
      Else:
        Target.Value = Oldvalue
      End If
    End If
  End If
End If

Application.EnableEvents = True

Exitsub:
Application.EnableEvents = True

End Sub
 
Upvote 0
Solution
See if this modification does what you want:
VBA Code:
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)
Dim Oldvalue As String
Dim Newvalue As String

On Error GoTo Exitsub

' Exit if multiple cells updated at once
If Target.CountLarge > 1 Then Exit Sub

' Check to see if column E in Target row is "Including"
If Cells(Target.Row, "E") = "Including" 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 & vbNewLine & "• " & Newvalue
      Else:
        Target.Value = Oldvalue
      End If
    End If
  End If
End If

Application.EnableEvents = True

Exitsub:
Application.EnableEvents = True

End Sub
Yes that's sorted it perfectly! Thank You So Much!!!:):):)(y)?
 
Upvote 0

Forum statistics

Threads
1,213,536
Messages
6,114,202
Members
448,554
Latest member
Gleisner2

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