VBA add text to cell based on value of another cell

khanh_n

New Member
Joined
Jan 7, 2016
Messages
13
Hi all,
I'm a complete beginner with VBA. Hoping for your help.

I found the below VBA, which I tested and works fine.

Sub X()
Dim lngRow As Long
Dim BotRow As Long

Cells(Rows.Count, "K").Select
Selection.End(xlUp).Select
BotRow = Selection.Row
For lngRow = 1 To BotRow
If UCase(Cells(lngRow, "K")) = "YES" Then
Cells(lngRow, "F") = Cells(lngRow, "F") & "************"
End If
Next
End Sub
After modifying to below, it no longer works.

Sub AddText()

Dim lngRow As Long
Dim BotRow As Long

Cells(Rows.Count, "N").Select
Selection.End(xlUp).Select
BotRow = Selection.Row
For lngRow = 1 To BotRow
If UCase(Cells(lngRow, "N")) = "remove" Then
Cells(lngRow, "H") = Cells(lngRow, "H") & "-Removed"
End If
Next
End Sub

Basically I want to add '-Removed' to existing cell in column H on the same row if the word 'remove' is found in cell in column N.

Any help would be much appreciated. Thanks.
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
only issue I see between the two is that you are looking at the UCASE of a cell but looking for lower case word. UCASE changes the value in the cell to all upper case. So it would never match to "remove"

try:

Code:
Sub AddText()

 Dim lngRow As Long
 Dim BotRow As Long

 Cells(Rows.Count, "N").Select
 Selection.End(xlUp).Select
 BotRow = Selection.Row
 For lngRow = 1 To BotRow
 If UCase(Cells(lngRow, "N")) = "REMOVE" Then
 Cells(lngRow, "H") = Cells(lngRow, "H") & "-Removed"
 End If
 Next
 End Sub
 
Upvote 0
Hi RCBricker, I tested it after changing it to LCase and it works if the cell only contains "Remove". But I realize "Remove" is not always at the beginning of the cell, also there's sometimes other text in the cell as well, so it doesn't work properly.
I tried using wildcard "*removed*", doesn't seem to work. Any suggestions please?
 
Upvote 0
try this

Code:
Sub AddText()

 Dim lngRow As Long
 Dim BotRow As Long

 Cells(Rows.Count, "N").Select
 Selection.End(xlUp).Select
 BotRow = Selection.Row
 For lngRow = 1 To BotRow
 If InStr(1, Cells(lngRow, "N").Value, "Remove") > 0 Then
 Cells(lngRow, "H") = Cells(lngRow, "H") & "-Removed"
 End If
 Next
 End Sub
 
Upvote 0

Forum statistics

Threads
1,214,588
Messages
6,120,409
Members
448,959
Latest member
camelliaCase

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