How to Find and Replace Values Greater Than or Less Than A Fixed Value in Excel

Suppose we have a list of scores, and we want to replace values equal and greater than 60 to text ‘PASS’, this work cannot be implemented by ‘Find and Replace’ function due to this function is only used for replacing a fixed value to another fixed value. Actually, we can edit VBA code to implement this special ‘Find and Replace’. Setting a fixed value in code, then after running macro, all values greater than this fixed value can be replaced with another value properly. This article will show you the details.

Precondition:

See screenshot below. We want to replace values >=60 with text ‘PASS’.

How to Find and Replace Values Greater Than or Less Than A Fixed Value 1

Method: Find and Replace Values Greater Than or Less Than A Fixed Value by VBA Code


Step 1: On current visible worksheet, right click on sheet name tab to load Sheet management menu. Select View Code, Microsoft Visual Basic for Applications window pops up.

How to Find and Replace Values Greater Than or Less Than A Fixed Value 2

Or you can enter Microsoft Visual Basic for Applications window via Developer->Visual Basic. You can also press Alt + F11 keys simultaneously to open it.

How to Find and Replace Values Greater Than or Less Than A Fixed Value 3

Step 2: In Microsoft Visual Basic for Applications window, click Insert->Module, enter below code in Module1:

Sub FindAndReplace()

Dim UpdateRng As Range

Dim SelectRng As Range

Set SelectRng = Application.Selection

Set SelectRng = Application.InputBox("Select  a Range", "Find and Replace", SelectRng.Address, Type:=8)

For Each UpdateRng In SelectRng

    If UpdateRng.Value >= 60 Then

        UpdateRng.Value = "PASS"

    End If

Next

End Sub

Step 3: Save the macro and then quit Microsoft Visual Basic for Applications.

Step 4: Click Developer->Macros to run Macro.

How to Find and Replace Values Greater Than or Less Than A Fixed Value 4

You can also press Alt+F8 to trigger Macro directly.

Step 5: Select ‘FindAndReplace’ and click Run.

How to Find and Replace Values Greater Than or Less Than A Fixed Value 5

Step 6: FindAndReplace dialog pops up. Enter Select Range $B$2:$B$8. In this step you can select the range you want to replace values.

How to Find and Replace Values Greater Than or Less Than A Fixed Value 6

Step 7: Click OK. Verify that values equal to or greater than 60 are replaced with ‘PASS’ properly.

How to Find and Replace Values Greater Than or Less Than A Fixed Value 7

Comment:

If you want to find and replace values less than a fixed value, just change below code:

For Each UpdateRng In SelectRng

    If UpdateRng.Value >= 60 Then

        UpdateRng.Value = "PASS"

    End If

Next

To:

For Each UpdateRng In SelectRng

    If UpdateRng.Value < 60 Then

        UpdateRng.Value = "FAIL"

    End If

Next

 

 

 

 

 

 

Related Posts

Excel Structure Reference

This tutorial introduces the basics of structured referencing in Excel and shows you how to use structured referencing in a table with examples. When you use formulas that reference tables, whether the formula references a part of the table or ...

Excel/Google Sheets: Full Row Reference

Excel or Google sheets supports both full row references and full column references. This article will talk about the usage of full row references. Full Row reference is another cell reference in Excel or Google Sheets, which is used to ...

Excel/Google Sheets: Full Column Reference

Full column reference is another cell reference in Excel or Google Sheets, which is used to reference the entire column, for example, if you want to reference the entire column A, you can use the following reference format A: A. ...

Excel/Google Sheets: Expanding Reference

An expanding reference is a reference type in Excel and Google worksheets that extends the range of a cell when a formula is copied to the cell below or to the right. In this article, we will explain how to ...

Excel/Google Sheets: Mixed Reference

In Microsoft Excel Spreadsheet or Google sheets, there is another cell reference, mixed references, where part of the reference is absolute, part of the relative. This article will describe how to use mixed references through specific examples. Mixed Reference When ...

Sidebar