Code To Look At Column C And If All Data Matches In Column AC Copy To Sheet 2

Dazzawm

Well-known Member
Joined
Jan 24, 2011
Messages
3,748
Office Version
  1. 365
Platform
  1. Windows
I have a file as laid out below. I need the code to look at column C and if the data matches then look at column AC. If all the data in column AC says 'NYA' then copy to sheet 2 like the second example. Thanks.

Excel 2010
CAC
1CatCodeStockCode
2AAROC18 3001NYA
3AAROC18 3001NYA
4AAROC22D 3001NYA
5AAROC22D 3001NYA
6AM40004D 1001NYA
7AM40004D 1001NYA
8AM40004D 1001NYA
9AM50005 1001NYA
10AM50005 7001TEST
11AM50005 7001NYA
12AM50005D 1001NYA
13AM50005D 1001NYA
14AM50005D 5001TEST
15AM50005D 5001NYA
16AM50005D 5001NYA
17AM50005D 7001NYA
18AMCIT04D 1001TEST
19AMCIT04D 1001NYA

<colgroup><col style="width: 25pxpx"><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Sheet1



As you can see below I have highlighted in yellow what needs to be copied as the data in C matches and in column AC they say NYA.


Excel 2010
CAC
1CatCodeStockCode
2AAROC18 3001NYA
3AAROC18 3001NYA
4AAROC22D 3001NYA
5AAROC22D 3001NYA
6AM40004D 1001NYA
7AM40004D 1001NYA
8AM40004D 1001NYA
9AM50005 1001NYA
10AM50005 7001TEST
11AM50005 7001NYA
12AM50005D 1001NYA
13AM50005D 1001NYA
14AM50005D 5001TEST
15AM50005D 5001NYA
16AM50005D 5001NYA
17AM50005D 7001NYA
18AMCIT04D 1001TEST
19AMCIT04D 1001NYA
Sheet2
 
Last edited:
Thanks Leith that did it, although I had to change the ranges from sheet 2 to 1 and A2 to C2. Thanks again and also Worf for your interest.
 
Upvote 0

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Hello Dazzawm,,

This macro highlights the entire row.
Code:
Sub TestMacro2()

    Dim Cell    As Range
    Dim col     As Long
    Dim Dict    As Object
    Dim Key     As String
    Dim Item    As Variant
    Dim Rng     As Range
    Dim Wks     As Worksheet
    
        Set Wks = Worksheets("Sheet2")
        
        Set Rng = Wks.Range("A2")
        
      ' Offset from Rng column to validation column.
        col = 26
        
        EndRow = Wks.Cells(Rows.Count, Rng.Column).End(xlUp).Row
        If EndRow > Rng.Row Then Set Rng = Rng.Resize(EndRow - Rng.Row + 1, 1)
        
        Set Dict = CreateObject("Scripting.Dictionary")
        Dict.CompareMode = vbTextComapre
        
        Application.ScreenUpdating = False
        
            For Each Cell In Rng
                Key = Trim(Cell)
                If Key <> "" And Cell.Offset(0, col) = "NYA" Then
                    If Not Dict.exists(Key) Then
                        Set Item = Cell
                        Dict.Add Key, Item
                    Else
                        Set Item = Dict(Key)
                            Item.EntireRow.Interior.Color = vbYellow
                            Cell.EntireRow.Interior.Color = vbYellow
                        Set Dict(Key) = Cell
                    End If
                End If
            Next Cell
            
        Application.ScreenUpdating = True
        
End Sub

Actually Leith this didn't work correctly. If there was a group of 4 for example it highlighted the 3 rows where NYA was in AC but left the 4th row clear where there was other data. These 4 rows should have been left clear as all 4 cells in AC were not 'NYA'. Please see below.


Excel 2010
CAC
1725AUA420 3032BTest
1726AUA420 3032BNYA
1727AUA420 3032BNYA
1728AUA420 3032BNYA
1729AUA420 3036ATest
1730AUA420 3036ANYA
1731AUA420 3036ANYA
1732AUA420 3036ANYA
1733AUA420 3036BTest
1734AUA420 3036BNYA
1735AUA420 3036BNYA
1736AUA420 3036BNYA
Sheet1


As you can see 4 cells match in column C but not all 4 cells in column AC say 'NYA' So all these must be left clear.
 
Upvote 0
Can I add an extra problem please?! If the last character in column C is a letter like above then all these need to be ignored.
 
Upvote 0
Don't go to any further trouble Leith, it's all sorted. Thanks everyone.
 
Upvote 0

Forum statistics

Threads
1,215,237
Messages
6,123,800
Members
449,127
Latest member
Cyko

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