Nested FOR loop to comare 2 diffrent cell.

ittan

New Member
Joined
Oct 23, 2013
Messages
37
Hi,

I want to:
1. Compare between 2 cells in first row of a big table.
2. If it matches (red color is the match words) copy the content of 'CustomerB to CustomerA' row by row.
3. Code inside the curly bracket {} , i'm not sure how to do this.


CustomerA Dose Step 2 Target

<tbody>
</tbody>

CustomerA Dose Step 1 Target

<tbody>
</tbody>
CustomerB Dose Step 2 Target
CustomerB Dose Step 1 Target
2
4
6
8
3
5
7
9

<tbody>
</tbody>

Code:
For i = 17 To 298 Step 1 'range of CustomerA
    For j = 300 To 385 Step 1 'range of CustomerB

      if { cells(1 , i) = cells(1 , j) } Then     '(I am not comparing two cells entirely, I'm comparing the last past of the value. not sure how to do this)
         
           for x = 2 To LastColumn
                cells (x , i) = cells (x , j)
           next x
      next j
 next i

thanks in advance.
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Pleas e give an example a match (what the word are and where they are0 and what exactly you want to do. Still unclear abt ur request
 
Upvote 0
Pleas e give an example a match (what the word are and where they are0 and what exactly you want to do. Still unclear abt ur request
i know it sounds bit confusing. :(
how do i put it?

i have an excell sheet with 385 columns
from column 17 to 298 its customerA
from 300 to 385 its customerB
eg:
cell(17,1) is "CustomerA {parameter}"
some where in the range 300 to 385, there is another CustomerB with the same "{parameter}"
If those words match, Then copy the content of 'CustomerB {parameter}' to 'CustomerA {parameter}' row by row.
The{parameter} changes in every cell.
 
Upvote 0
this should work, although I think I need to exit the loop at some point, give it a try
Code:
Sub findparam()
    For I = 17 To 288
        For j = 300 To 385
            a = InStr(1, Cells(1, I), " ")
            txt1 = Mid(Cells(1, I), a + 1, Len(Cells(1, I)) - a)
            b = InStr(1, Cells(1, j), " ")
            txt2 = Mid(Cells(1, j), a + 1, Len(Cells(1, j)) - a)
            
                If txt1 = txt2 Then
                    For k = 2 To Cells(Rows.Count, j).End(xlUp).Row
                        Cells(k, I) = Cells(k, j)
                    Next k
                    
                End If
            
        Next j
    Next I
            
End Sub
I also think that the 3rd For loop can be avoided, since you are just copying from one range to another, so we can use something like
Code:
Range("A1").copy [destination]
 
Last edited:
Upvote 0
Well, this is more like it
Code:
Sub findparam()
    For I = 17 To 298
        For j = 300 To 385
            a = InStr(1, Cells(1, I), " ")
            txt1 = Mid(Cells(1, I), a + 1, Len(Cells(1, I)) - a)
            b = InStr(1, Cells(1, j), " ")
            txt2 = Mid(Cells(1, j), a + 1, Len(Cells(1, j)) - a)
            
                If txt1 = txt2 Then
                
                    For k = 2 To Cells(Rows.Count, j).End(xlUp).Row
                        Cells(k, I) = Cells(k, j)
                    Next k
        Exit For
                    
                End If
            
        Next j
    Next I
            
End Sub
 
Upvote 0
YES. :) it works like a champ. thanks a lot. i used the 3rd loop to run an If statement. for some other purpose. thanks again.
 
Upvote 0
You are very much welcome. Thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,216,028
Messages
6,128,382
Members
449,445
Latest member
JJFabEngineering

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