Find and replace a cell value if another cell value is higher

simonharry

New Member
Joined
Jun 17, 2016
Messages
8
Hi All,

Hopefully you can assist please as my VBA skills are unfortunately lacking in this area.

I have a spreadsheet with 2 values in different columns,

What i'm trying to do is...

Where the value in column S is greater than the value in column F, i want to replace the column F value with the column S value. In the example below, the bold entries in "Column S" would replace the values in "Column F".

Column FColumn S
2376
42
2535
21

My data starts on row 4 and can run down any number of rows so i'd want to repeat this "check and replace" down the entire list where applicable.

I have a number of macro's in use here so i'd be looking to do this within the VBA script.

Thank you in advance.
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Your example contradicts your explanation (you bolded values in column S LESS THAN the value in column F, instead of GREATER THAN).
No matter, you can alter this code very easily to go either way.
VBA Code:
Sub MyReplace()

    Dim lr As Long
    Dim r As Long
    
    Application.ScreenUpdating = False
    
'   Find last row in column S with data
    lr = Cells(Rows.Count, "S").End(xlUp).Row
    
'   Loop through all rows
    For r = 1 To lr
'       See if value in S greater than column F
        If Cells(r, "S").Value > Cells(r, "F").Value Then
'           Replace column F value with column S value
            Cells(r, "F").Value = Cells(r, "S").Value
        End If
    Next r
    
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,161
Members
453,021
Latest member
Justyna P

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