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

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
58,950
Office Version
  1. 365
Platform
  1. Windows
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?
 

djalexr

New Member
Joined
Jan 14, 2011
Messages
33
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!
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
58,950
Office Version
  1. 365
Platform
  1. Windows
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
 
Solution

djalexr

New Member
Joined
Jan 14, 2011
Messages
33
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)😘
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
58,950
Office Version
  1. 365
Platform
  1. Windows
You are welcome. :)
 

Forum statistics

Threads
1,141,068
Messages
5,704,093
Members
421,327
Latest member
Msh

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
Top