Add text to cell on condition

Apple08

Active Member
Joined
Nov 1, 2014
Messages
450
Hi everyone

I need to add a text 'P-' to the cell text in column F based on the value of column M. If column M's cell value is 'Test', then 'P-' have to add to the text in column F e.g. P-XYZ123

I have setup code as below, however it doesn't work:

Code:
Dim Lst As Long, r As Long
Lst = Range("A" & Rows.Count).End(xlUp).Row
For r = 1 To Lst
    With Sheets("Combined_Data").Range("M" & r)
        If .Value = "Test" Then
              .Range("F" & r).Value = "P-" & Range("F" & r).Value
              Exit For
        End If
    End With
 Next r

Please could anyone help? Many thanks.
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Code:
Dim Lst As Long, r As Long
Lst = Range("A" & Rows.Count).End(xlUp).Row
For r = 1 To Lst
    With Sheets("Combined_Data").Range("M" & r)
        If .Value = "Test" Then
              .Range("F" & r).Value = "P-" & Range("F" & r).Value
              [COLOR=#FF0000][B]Exit For[/B][/COLOR]
        End If
    End With
 Next r

Well, if this is doing anything at all, it looks like it is going exit the loop after the first occurrence of "Test". So, if there are multiple occurrences, it will only do one.

Is that what is happening? Or nothing at all? You weren't clear by "it doesn't work".
 
Upvote 0
As well as what dreid1011 has pointed out there are a few other potential problems
1) You are calculating Lst based on the active sheet rather than the Combined data sheet.
2) Because of the way you have used the with statement you are actually changing the value in col R not col F
3) The value in col R on the Combined sheet will equal the value in col F on the Active sheet = "P-"

Try
Code:
   Dim Lst As Long, r As Long
   With Sheets("Combined_Data")
      Lst = .Range("A" & Rows.Count).End(xlUp).Row
      For r = 1 To Lst
         If .Value = "Test" Then
            .Range("F" & r).Value = "P-" & .Range("F" & r).Value
         End If
      Next r
   End With
 
Upvote 0
Oops missed abit
Code:
   Dim Lst As Long, r As Long
   With Sheets("Combined_Data")
      Lst = .Range("A" & Rows.Count).End(xlUp).Row
      For r = 1 To Lst
         If .Range("M" & r).Value = "Test" Then
            .Range("F" & r).Value = "P-" & .Range("F" & r).Value
         End If
      Next r
   End With
 
Upvote 0
I think this code will do what your posted code attempted to do...
Code:
[table="width: 500"]
[tr]
	[td]Sub AddPDashIfTest()
  Dim LastRow As Long
  LastRow = Sheets("Combined Data").Cells(Rows.Count, "M").End(xlUp).Row
  Range("F1:F" & LastRow) = Evaluate(Replace("IF('Combined Data'!M1:M#=""Test"",""P-""&F1:F#,IF(F1:F#="""","""",F1:F#))", "#", LastRow))
End Sub[/td]
[/tr]
[/table]
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,209
Members
453,022
Latest member
RobertV1609

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