VBA Code comparing cell values

phi1456

New Member
Joined
Nov 19, 2019
Messages
9
I am looking for VBA code to compare sheet(1) cell A1 to Sheet(2) Cell B2, if the cells are not equal take the value of Sheet(2) cell B2 and put it in Sheet(1) cell C1. If Sheet(1) cell C1 already contains a value then loop checking until an empty cell is found in column C placing the value in the first empty cell. If sheet(1) cell A1 and sheet(2) cell B2 are equal then end the loop.

Thanks in advance.
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Try this....

VBA Code:
Sub Compare()
Dim Sht1 As Worksheet
Dim Sht2 As Worksheet
Set Sht1 = Sheets("Sheet1")
Set Sht2 = Sheets("Sheet2")

    With Sht1
    
        If .Cells(1, 1) = Sht2.Cells(2, 2) Then Exit Sub
        
        LR = .Cells(Rows.Count, 3).End(xlUp).Row
        If .Cells(1, 3) > "" Then LR = LR + 1
        .Cells(LR, 3) = Sht2.Cells(2, 2)
    
    End With
End Sub

Hope that helps.
 
Upvote 0

Forum statistics

Threads
1,213,546
Messages
6,114,251
Members
448,556
Latest member
peterhess2002

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