Home » excel » vba – Replace cells in an Excel sheets with a different value

vba – Replace cells in an Excel sheets with a different value

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have an excel sheet that has around 900 rows of data and I need to run through one of the columns and replace what that column has with a different value depending on what is currently in the column.

For example, say column F has these values entered:
Adj/Aviation & Transportation
Adj/Aviation & Transportation
Adj/Aviation & Transportation
Adj ESL Instructor
Adj/ESL
Adj/Aviation & Transportation
Professor/Aviation
Professor-Aviation
Associate Chair, Professor
Professor
Prof/Aviation & Transportation
Adjunct Professor
Professor
Professor
Adj/Aviation
Professor
Dir Master’s Prog/Assoc Prof
Chair/Assoc Prof/Aviation&TS
Adj Asst Prof/Aviation
Assistant Professor/Aviation
Adj.Asst. Prof-Aviation

I want to run through this column and if one of the cells contains the value “Aviation” I want to clear the content in that cell and enter “Aviation & Transportation” instead.

If anyone can show me how to accomplish this task that would mean the world!

How to&Answers:

Actually, I have a better solution. You can use wildcards in the Find..Replace menu.

Hit Ctrl F > Replace > Enter *Aviation* then write whatever you want to replace the cell value to. I tested it and it works. You can enable or disable case sensitivity.

Answer:

Add a new column and populate it with a formula something like:

=IF(NOT(ISERROR(SEARCH("Aviation",B1))),"Aviation & Transportation",B1)
  • This is untested, might need some syntax adjustment as I haven’t put it in excel. But it should point you in the right direction.

Answer:

Try this

 Sub replace()
    Dim Rws As Long, Rng As Range, c As Range
    Rws = Cells(Rows.Count, "F").End(xlUp).Row
    Set Rng = Range(Cells(1, 6), Cells(Rws, 6))

    For Each c In Rng.Cells
        If c Like "*Aviation*" Then c = "Aviation & Transportation"
    Next c
End Sub

You can also use the Autofilter in your VBA code.

Start
enter image description here
result
enter image description here

Use the autofilter code.

Sub Button1_Click()
    Dim Rws As Long, Rng As Range, c As Range
    Rws = Cells(Rows.Count, "F").End(xlUp).Row

    Columns("F:F").AutoFilter Field:=1, Criteria1:="=*Aviation*"
    Set Rng = Range(Cells(2, 6), Cells(Rws, 6)).SpecialCells(xlCellTypeVisible)
    Rng.Value = "Aviation & Transportation"
    ActiveSheet.AutoFilterMode = 0

End Sub