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’.
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.
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.
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.
You can also press Alt+F8 to trigger Macro directly.
Step 5: Select ‘FindAndReplace’ and click Run.
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.
Step 7: Click OK. Verify that values equal to or greater than 60 are replaced with ‘PASS’ properly.
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