Comparing Filtered Columns

wvanbusk111

New Member
Joined
Jun 18, 2014
Messages
1
Hey all-been working at this for a couple days and could use some input.

What I'm attempting to accomplish with this code is to compare two sets of data on separate sheets(same workbook) and populate a third sheet with exceptions. I'm looking to have a list of what's missing in both of the two sheets compared(with respect to each other). The good news is that I've found/figured out how to do that fairly efficiently. So here's the challenge-I'm also filtering the values prior to running the compare vba and would only like to compare and display filtered values.

So...Goal:

1.)Compare and return exceptions of filtered values only

Here's my code:

Code:
Sub comparevalue()    
Dim sh1 As Worksheet, sh2 As Worksheet, sh3 As Worksheet, lr1 As Long, lr2 As Long, rng1 As Range, rng2 As Range, c As Range 
    Set sh1 = Sheets("SHEET1") 
    Set sh2 = Sheets("SHEET2") 
    Set sh3 = Sheets("SHEET3") 
    lr1 = sh1.Cells(Rows.Count, 1).End(xlUp).Row 'Get the last row with data for both list sheets
    lr2 = sh2.Cells(Rows.Count, 1).End(xlUp).Row 
    Set rng1 = sh1.Range("I2:I" & lr1) 'Establish the ranges on both sheets
    Set rng2 = sh2.Range("A2:A" & lr2) 
    With sh3 
        If .Range("A1") = "" And .Range("B1") = "" Then 
            .Range("A1") = "Extras in List 1" 
            .Range("B1") = "Extras in List 2" 
        End If 
    End With 
    For Each c In rng1 'Run a loop for each list ID mismatches and paste to sheet 3.
        If WorksheetFunction.CountIf(rng2, c.Value) = 0 Then 
            sh3.Cells(Rows.Count, 1).End(xlUp)(2) = c.Value 
        End If 
    Next 
    Stop 
    For Each c In rng2 
        If Application.CountIf(rng1, c.Value) = 0 Then 
            sh3.Cells(Rows.Count, 2).End(xlUp)(2) = c.Value 
        End If 
    Next 
End Sub

After some research, I've also figured out that I'll likely need to use specialcells(xlcelltypevisible) in some manner. As of right now, I can only get it to return either all the values, regardless of the filter, or 1 value, again regardless of the filter-depending on how I could specialcells into the vba. I didn't include it in my code above because I didn't want to "muddy" up what's working or present a rabbit trail I've been following as a solution....Thanks for any help in advance!

By the way-I'm using Excel 2010
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
If you want to compare unique values of the two columns in sheet 3, then you could use AdvancedFilter which has a feature to copy the unique values to another column. You could then run your compare macro against those two columns. He is the advanced filter macro for sheet 3, assuming that columns 4 and 5 are currently unused.
Code:
Sub filtstuff()
Dim sh3 As Worksheet, lr As Long
Set sh3 = Sheets("Sheet3")
lr = sh3.Cells.Find("*", sh3.Range("A2"), xlFormulas, xlPart, xlByRows, xlPrevious).Row
sh3.Range("A1:A" & lr).AdvancedFilter xlFilterCopy, , sh3.Range("D1"), True
sh3.Range("B1:B" & lr).AdvancedFilter xlFilterCopy, , sh3.Range("E1"), True
End Sub
With all that said, I am wondering if you could not achieve the same end by puttin column B data under column A data and using advanced filter to get the unique values between the two sheets. or maybe you would rather keep them separate so you can tell which has what. Just thinking out loud here.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,399
Latest member
alchavar

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