Help w/multiple criteria VBA If/Then - EX2007

Jeff5019

New Member
Joined
Apr 27, 2012
Messages
15
Hi,

Can't seem to find the answer to this one. I have a workbook in EX2007 with 2 sheets. I need something that will do the following.

If Sheet 1 Column A = Sheet 2 Column A AND Sheet 2 Column B > Sheet 1 Column B Then Copy Sheet 1 Column B to Sheet 2 Column B

This could potentially be in any row. Basically I have a cell with an account number in ColA on both sheets and I need to make sure that account number matches before it does anything. Then if it does match check the number in ColB and if the number is bigger on sheet 1 then copy that number to sheet 2 in the appropriate cell and row.

Any assistance would be very much appreciated.
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
I'm trying to follow you is this correct?

Code:
If Sheets("Sheet1").Range("A1") = Sheets("Sheet2").Range("A1") Then

    If Sheets("Sheet2").Range("B1") > Sheets("Sheet1").Range("B1") Then
    
        Sheets("Sheet2").Range("B1").Value = Sheets("Sheet1").Range("B1").Value
        
    End If
    
End If
 
Upvote 0
Hi John,

I think that will work. I'll reply later this afternoon when I have a chance to try it. My concern was that the rows might not necessarily match up but I will just have it sort by the account number before it runs your code. That way this code will work. Thanks a lot! I'll let you know.
 
Upvote 0
That works for that one row... so it's a step in the right direction! But I need it to go down the entire column and compare each row until it reaches a blank. I'm thinking some sort of WHILE LOOP statement might do it, but I'm still learning so I'm not 100% sure how to make it work. Isn't there some syntax that tells it to go down one cell?
 
Upvote 0
That works for that one row... so it's a step in the right direction! But I need it to go down the entire column and compare each row until it reaches a blank. I'm thinking some sort of WHILE LOOP statement might do it, but I'm still learning so I'm not 100% sure how to make it work. Isn't there some syntax that tells it to go down one cell?

Try this on a copy of your sheets firs before installing into your original workbook.

Code:
Sub iffy()
Dim sh As Worksheet, sh2 As Worksheet, rng As Range, lr As Long
Set sh = Sheets("Sheet1")
Set sh2 = Sheets("Sheet2")
lr = sh.Cells(Rows.Count, 1).End(xlUp).Row
Set rng = sh.Range("A2:A" & lr)
For i = 2 To lr
If sh.Cells(i, 1) = sh2.Cells(i, 1) And sh.Cells(i, 2) < sh2.Cells(i, 2) Then
sh2.Cells(i, 2) = sh.Cells(i, 2).Value
End If
Next
End Sub
Code:
 
Upvote 0
Try this on a copy of your sheets firs before installing into your original workbook.

Code:
Sub iffy()
Dim sh As Worksheet, sh2 As Worksheet, rng As Range, lr As Long
Set sh = Sheets("Sheet1")
Set sh2 = Sheets("Sheet2")
lr = sh.Cells(Rows.Count, 1).End(xlUp).Row
Set rng = sh.Range("A2:A" & lr)
For i = 2 To lr
If sh.Cells(i, 1) = sh2.Cells(i, 1) And sh.Cells(i, 2) < sh2.Cells(i, 2) Then
sh2.Cells(i, 2) = sh.Cells(i, 2).Value
End If
Next
End Sub
Code:
I received "Compile Error: Duplicate declaration in current scope" where is says "lr As Long" in the first line.
 
Upvote 0

Forum statistics

Threads
1,214,913
Messages
6,122,207
Members
449,074
Latest member
cancansova

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