Making two columns equal using double for each

Moonbeam111

Board Regular
Joined
Sep 24, 2018
Messages
85
Office Version
  1. 365
  2. 2010
I have a code that isn't working. I'm trying to make the two ranges match each other IF there is a difference then it needs to make that cell equal to the other one.
VBA Code:
cell1 = Range("D22:D25")
cell2 = Range("D135:D138")
For Each cell2 In Range("D135:D138")
For Each cell1 In Range("D22:D25")
If cell1 <> cell2 Then
cell1 = cell2
End If
Next cell1
Next cell2
Please assist.
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
If you want to always match range1 then you can just make range2=range1. Don't need to check.
 
Upvote 0
Yes this is what i did.

VBA Code:
If Range("D22") <> Range("D135") Then
Range("D135") = Range("D22")
End If

If Range("D23") <> Range("D136") Then
Range("D136") = Range("D23")
End If


If Range("D24") <> Range("D137") Then
Range("D137") = Range("D22")
End If

If Range("D25") <> Range("D138") Then
Range("D138") = Range("D25")
End If
 
Upvote 0
My first reaction was the same a that of Cubist. If you are setting the first range = to the second range you don't need to the IF. There is no harm in replacing the value when they are equal since you won't see any difference anyway.
So @Cubist suggestion was actually this:
VBA Code:
Sub SetRangesEqual()
    Range("D22:D25").Value = Range("D135:D138").Value
End Sub

There are a number of ways to change what you have into a Loop, here is one:
VBA Code:
Sub LoopingThroughRange()
    Dim rng1 As Range, rng2 As Range
    Dim i As Long
    
    Set rng1 = Range("D22:D25")
    Set rng2 = Range("D135:D138")
    
    For i = 1 To rng1.Cells.Count
        If rng1.Cells(i, 1).Value <> rng2.Cells(i, 1).Value Then
            ' Do something
            rng1.Cells(i, 1).Value = rng2.Cells(i, 1).Value
        End If
    Next i

End Sub
 
Upvote 0
@Moonbeam111 just
VBA Code:
Range("D135:D138").Value = Range("D22:D25").Value

Edit: Oops I see Alex has just posted the same for what @Cubist suggested
 
Last edited:
Upvote 0
Upvote 0
Post #3 is the correct one, my bad.
In that case for the 1 line version use what Mark posted, for the looping version here it is reversed.
Note: I could have left the If statement unchanged but I think its easier to follow if they are both the same way around.
Same goes for the For loop I changed it to use rng2 as the primary.

VBA Code:
Sub LoopingThroughRange()
    Dim rng1 As Range, rng2 As Range
    Dim i As Long
 
    Set rng1 = Range("D22:D25")
    Set rng2 = Range("D135:D138")
 
    For i = 1 To rng2.Cells.Count
        If rng2.Cells(i, 1).Value <> rng1.Cells(i, 1).Value Then
            ' Do something
            rng2.Cells(i, 1).Value = rng1.Cells(i, 1).Value
        End If
    Next i

End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,225,969
Messages
6,188,111
Members
453,460
Latest member
Cjohnson3

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