简体   繁体   中英

Excel VBA Comparison with Two Attributes

I want to compare data between two worksheets using VBA. Normally, I would just use a COUNTIF() function or use a loop, but in this case I want to compare values from two different columns (combined, they would represent a distinct value) to the corresponding values in two different columns on another sheet.

在此处输入图片说明

In the example shown, I want to compare the 4 different permutations highlighted to the other sheet, to see if those 4 unique permutations exist in the other sheet. For example, if in the other sheet there is "AD7 - Abidjan" I want that to flag as a difference and have the cells highlighted or stored somewhere else. The other sheet may contain more or less (or zero) variations.

I'm not sure how to use a COUNTIF using data like this. I prefer not to concatenate data/move it into another range. Is there a way to run this analysis without manipulating the data?

Thanks!

The following loop can be used to compare both fields in each sheet:

For Each i In Sheets("First_Sheet_Name").Range("First_Column")

Set rng = Sheets("Second_Sheet_Name").Range("First_Column")
Set rng2 = Sheets("Second_Sheet_Name").Range("Second_Column")

If Application.WorksheetFunction.CountIfs(rng, i, rng2, i.Offset(0, 2)) = 0 Then

  [Do something here]

End If

Next i

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM