If cell contains certain text then replace with another cell

MechEng_Yoshi

New Member
Joined
Oct 2, 2020
Messages
13
Office Version
  1. 365
Platform
  1. Windows
For example if I have range from G1:G1000 and with some ones name and another with their name and graduation year (EX: John Smith '19). How could I use vba to recognize the fields that don't have graduation year and replace with the cell that has the correct text. I have thousands of names and grad years from '60 - '20. Thank you
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Hi, one query - if you find name with graduation year would you like to replace it with name only. Ex: John Smith '19 will be converted to John Smith.
 
Upvote 0
If I find name with graduation year I would like to replace name with name+graduation year. EX John smith converts to John Smith ‘19
 
Upvote 0
Sorry, didn't understand the requirement.

IF cell contains John Smith ‘19, it would remain as is.
IF cell contains Matt Nelson, it should change to Matt Nelson '19.

Is my understanding correct ?
 
Upvote 0
Hi,

Please check below code. It will replace the text as per the requirement.

VBA Code:
Sub replace()
    Dim lastRow As Integer, rowno As Integer
    lastRow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
    
    For rowno = 2 To lastRow
        If InStr(1, ActiveSheet.Cells(rowno, 1), "'") = 0 Then
            ActiveSheet.Cells(rowno, 1) = ActiveSheet.Cells(rowno, 1) & " '19"
        End If
    Next

End Sub
 
Upvote 0
I think you misunderstood me. There are many graduation years ranging from '60 - '20 and some people don't even have a graduation year associated with their name. Sorry for the misunderstanding
 
Upvote 0
Is the graduation year in any column ?

Can you please share your data using XL2BB. It would help to copy the data and understand the requirement.
 
Upvote 0

Forum statistics

Threads
1,214,405
Messages
6,119,320
Members
448,887
Latest member
AirOliver

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