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,783
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
CatCodeStockCode
AAROC18 3001NYA
AAROC18 3001NYA
AAROC22D 3001NYA
AAROC22D 3001NYA
AM40004D 1001NYA
AM40004D 1001NYA
AM40004D 1001NYA
AM50005 1001NYA
AM50005 7001TEST
AM50005 7001NYA
AM50005D 1001NYA
AM50005D 1001NYA
AM50005D 5001TEST
AM50005D 5001NYA
AM50005D 5001NYA
AM50005D 7001NYA
AMCIT04D 1001TEST
AMCIT04D 1001NYA

<colgroup><col style="width: 25pxpx"><col><col></colgroup><thead>
</thead><tbody>
[TD="align: center"]1[/TD]

[TD="align: center"]2[/TD]

[TD="align: center"]3[/TD]

[TD="align: center"]4[/TD]

[TD="align: center"]5[/TD]

[TD="align: center"]6[/TD]

[TD="align: center"]7[/TD]

[TD="align: center"]8[/TD]

[TD="align: center"]9[/TD]

[TD="align: center"]10[/TD]

[TD="align: center"]11[/TD]

[TD="align: center"]12[/TD]

[TD="align: center"]13[/TD]

[TD="align: center"]14[/TD]

[TD="align: center"]15[/TD]

[TD="align: center"]16[/TD]

[TD="align: center"]17[/TD]

[TD="align: center"]18[/TD]

[TD="align: center"]19[/TD]

</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

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
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,223,904
Messages
6,175,295
Members
452,632
Latest member
jladair

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