{"id":4172,"date":"2019-04-03T16:56:25","date_gmt":"2019-04-03T16:56:25","guid":{"rendered":"http:\/\/officetuts.net\/excel\/?p=4172"},"modified":"2024-03-26T10:56:27","modified_gmt":"2024-03-26T10:56:27","slug":"compare-columns-in-excel","status":"publish","type":"post","link":"https:\/\/officetuts.net\/excel\/examples\/compare-columns-in-excel\/","title":{"rendered":"Compare Two Columns in Excel"},"content":{"rendered":"\n
Compare-Two-Columns-in-Excel<\/a>Download File<\/a><\/div>\n\n\n\n

There are a few ways to compare columns in Excel. In this tutorial, I\u2019ll present different ways you can\nachieve this.<\/p>\n\n\n\n

Row by row<\/h2>\n\n\n\n

The first method is to compare columns row by row – if the\nrows match then display the information in the third column.<\/p>\n\n\n\n

In this case, we are going to use the following formula:<\/p>\n\n\n\n

=IF(A1=B1,\"Match\",\"\")<\/code><\/pre>\n\n\n\n

This formula returns \u201cMatch\u201d if the condition is met.<\/p>\n\n\n\n

Let\u2019s see it in the following example.<\/p>\n\n\n\n

\"\"<\/figure>\n\n\n\n

What you have to remember is that this formula is case-insensitive,\nso Germany=Germany<\/strong>, but also Austria=austria<\/strong>.<\/p>\n\n\n\n

In order to make a case-sensitive comparison, we are going to use the EXACT function<\/a><\/strong>. This function checks whether the strings are exactly the same and returns TRUE if the condition is met, otherwise it returns FALSE.<\/p>\n\n\n\n

=IF(EXACT(A1,B1),\"Match\",\"\")<\/code><\/pre>\n\n\n\n
\"\"<\/figure>\n\n\n\n

Row by row using VBA<\/h2>\n\n\n\n

Case sensitive<\/h3>\n\n\n\n

So far, we had to use the third column in order to check\nmatching rows.<\/p>\n\n\n\n

This time, we are going to use VBA to highlight matching rows.<\/p>\n\n\n\n

Sub CompareColumnsCaseSensitive()\n    Set myRange = Selection\n    Set firstColumn = myRange.Columns(1)\n    Set secondColumn = myRange.Columns(2)\n    \n    counter = 1\n    For Each fcCell In firstColumn.Rows\n        Set scCell = secondColumn.Rows(counter)\n        If fcCell.Value = scCell.Value And fcCell.Value <> \"\" Then\n            fcCell.Interior.Color = vbGreen\n            scCell.Interior.Color = vbGreen\n        End If\n        counter = counter + 1\n    Next\nEnd Sub<\/code><\/pre>\n\n\n\n
\"\"<\/figure>\n\n\n\n

Code explanation:<\/strong><\/p>\n\n\n\n

2.<\/strong> We assign the\ncurrent selection to myRange<\/strong>.<\/p>\n\n\n\n

3-4.<\/strong> Assign\ncolumns to variables<\/a>.<\/p>\n\n\n\n

6.<\/strong> VBA created a\ncounter variable. We are going to use it to refer\nto the rows in the second column.<\/p>\n\n\n\n

7 – 14.<\/strong> For each\nCell (in our case Row) in the first column we are going to compare the cell in\nthe same row, but the second column. What\nwe have to remember is that the comparison is case-sensitive.<\/p>\n\n\n\n

There is also a condition (fcCell.Value\n<> “”) that the cells\ncan\u2019t be empty, otherwise, they would\nhighlight cells in the selection that are empty.<\/p>\n\n\n\n

10 – 11.<\/strong> If all\nconditions are met both cells are highlighted.<\/p>\n\n\n\n

As I mentioned earlier, the comparison works only if both\nvalues are exactly the same, so Austria<\/strong>\nis not austria<\/strong>\nin this case.<\/p>\n\n\n\n

Case insensitive<\/h3>\n\n\n\n

There are two simple ways, you can make case-insensitive\ncomparisons.<\/p>\n\n\n\n

First option<\/strong><\/p>\n\n\n\n

While making a comparison make the case of both values\nlowercase (or uppercase) run the code.<\/p>\n\n\n\n

Change<\/p>\n\n\n\n

If fcCell.Value = scCell.Value<\/code><\/pre>\n\n\n\n

To<\/p>\n\n\n\n

If LCase(fcCell.Value) = LCase(scCell.Value)<\/code><\/pre>\n\n\n\n

Second option<\/strong><\/p>\n\n\n\n

Instead of changing values to lowercase, you can add the following code as the very first line of our code (before subroutine).<\/p>\n\n\n\n

Option Compare Text<\/code><\/pre>\n\n\n\n

It\u2019s going to make all text comparisons case-insensitive.<\/p>\n\n\n\n

\"\"<\/figure>\n\n\n\n

Highlight all matching cells<\/h2>\n\n\n\n

In the last part of this tutorial, let\u2019s create a subroutine that\u2019s going to highlight every cell that has a match in the other column, but doesn\u2019t have to be restricted to the same row.<\/p>\n\n\n\n

Sub CompareColumnsCaseSensitive()\n    Set myRange = Selection\n    myRange.Interior.Color = xlNone\n    Set firstColumn = myRange.Columns(1)\n    Set secondColumn = myRange.Columns(2)\n    \n    For Each fcCell In firstColumn.Rows\n        For Each scCell In secondColumn.Rows\n            If fcCell.Value = scCell.Value And fcCell.Value <> \"\" Then\n                fcCell.Interior.Color = vbGreen\n                scCell.Interior.Color = vbGreen\n            End If\n        Next\n    Next\nEnd Sub<\/code><\/pre>\n\n\n\n

This give us the following result.<\/p>\n\n\n\n

\"\"<\/figure>\n\n\n\n

Add this line at the beginning of the script.<\/p>\n\n\n\n

Option Compare Text<\/code><\/pre>\n\n\n\n
\"\"<\/figure>\n\n\n\n

If you want to see more example on how to deal with\nduplicates, you can read this\ntutorial<\/a>. There, you\u2019ll learn how to highlight columns, that have different\nsizes, with different colors.<\/p>\n","protected":false},"excerpt":{"rendered":"

There are a few ways to compare columns in Excel. In this tutorial, I\u2019ll present different ways you can achieve this. Row by…<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21],"tags":[170,190],"yoast_head":"\nCompare Two Columns in Excel<\/title>\n<meta name=\"description\" content=\"Learn how to compare two columns in Excel using different methods. Use the formula presented in this tutorial to easily match rows in columns.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/officetuts.net\/excel\/examples\/compare-columns-in-excel\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Compare Two Columns in Excel\" \/>\n<meta property=\"og:description\" content=\"Learn how to compare two columns in Excel using different methods. Use the formula presented in this tutorial to easily match rows in columns.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/officetuts.net\/excel\/examples\/compare-columns-in-excel\/\" \/>\n<meta property=\"article:published_time\" content=\"2019-04-03T16:56:25+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-03-26T10:56:27+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2019\/04\/matching-columns.png\" \/>\n<meta name=\"author\" content=\"Tomasz Decker\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Tomasz Decker\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/officetuts.net\/excel\/examples\/compare-columns-in-excel\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/officetuts.net\/excel\/examples\/compare-columns-in-excel\/\"},\"author\":{\"name\":\"Tomasz Decker\",\"@id\":\"https:\/\/officetuts.net\/excel\/#\/schema\/person\/41b0b6996aaa4c4127f86f3d24452d42\"},\"headline\":\"Compare Two Columns in Excel\",\"datePublished\":\"2019-04-03T16:56:25+00:00\",\"dateModified\":\"2024-03-26T10:56:27+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/officetuts.net\/excel\/examples\/compare-columns-in-excel\/\"},\"wordCount\":460,\"publisher\":{\"@id\":\"https:\/\/officetuts.net\/excel\/#\/schema\/person\/41b0b6996aaa4c4127f86f3d24452d42\"},\"image\":{\"@id\":\"https:\/\/officetuts.net\/excel\/examples\/compare-columns-in-excel\/#primaryimage\"},\"thumbnailUrl\":\"http:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2019\/04\/matching-columns.png\",\"keywords\":[\"file\",\"pinterest\"],\"articleSection\":[\"examples\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/officetuts.net\/excel\/examples\/compare-columns-in-excel\/\",\"url\":\"https:\/\/officetuts.net\/excel\/examples\/compare-columns-in-excel\/\",\"name\":\"Compare Two Columns in Excel\",\"isPartOf\":{\"@id\":\"https:\/\/officetuts.net\/excel\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/officetuts.net\/excel\/examples\/compare-columns-in-excel\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/officetuts.net\/excel\/examples\/compare-columns-in-excel\/#primaryimage\"},\"thumbnailUrl\":\"http:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2019\/04\/matching-columns.png\",\"datePublished\":\"2019-04-03T16:56:25+00:00\",\"dateModified\":\"2024-03-26T10:56:27+00:00\",\"description\":\"Learn how to compare two columns in Excel using different methods. Use the formula presented in this tutorial to easily match rows in columns.\",\"breadcrumb\":{\"@id\":\"https:\/\/officetuts.net\/excel\/examples\/compare-columns-in-excel\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/officetuts.net\/excel\/examples\/compare-columns-in-excel\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/officetuts.net\/excel\/examples\/compare-columns-in-excel\/#primaryimage\",\"url\":\"http:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2019\/04\/matching-columns.png\",\"contentUrl\":\"http:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2019\/04\/matching-columns.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/officetuts.net\/excel\/examples\/compare-columns-in-excel\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/officetuts.net\/excel\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Compare Two Columns in Excel\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/officetuts.net\/excel\/#website\",\"url\":\"https:\/\/officetuts.net\/excel\/\",\"name\":\"\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/officetuts.net\/excel\/#\/schema\/person\/41b0b6996aaa4c4127f86f3d24452d42\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/officetuts.net\/excel\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/officetuts.net\/excel\/#\/schema\/person\/41b0b6996aaa4c4127f86f3d24452d42\",\"name\":\"Tomasz Decker\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/officetuts.net\/excel\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/18cbe22837193574870ae40ba56bf712?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/18cbe22837193574870ae40ba56bf712?s=96&d=mm&r=g\",\"caption\":\"Tomasz Decker\"},\"logo\":{\"@id\":\"https:\/\/officetuts.net\/excel\/#\/schema\/person\/image\/\"},\"description\":\"Spreadsheet and Python enthusiast.\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Compare Two Columns in Excel","description":"Learn how to compare two columns in Excel using different methods. Use the formula presented in this tutorial to easily match rows in columns.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/officetuts.net\/excel\/examples\/compare-columns-in-excel\/","og_locale":"en_US","og_type":"article","og_title":"Compare Two Columns in Excel","og_description":"Learn how to compare two columns in Excel using different methods. Use the formula presented in this tutorial to easily match rows in columns.","og_url":"https:\/\/officetuts.net\/excel\/examples\/compare-columns-in-excel\/","article_published_time":"2019-04-03T16:56:25+00:00","article_modified_time":"2024-03-26T10:56:27+00:00","og_image":[{"url":"http:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2019\/04\/matching-columns.png"}],"author":"Tomasz Decker","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Tomasz Decker","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/officetuts.net\/excel\/examples\/compare-columns-in-excel\/#article","isPartOf":{"@id":"https:\/\/officetuts.net\/excel\/examples\/compare-columns-in-excel\/"},"author":{"name":"Tomasz Decker","@id":"https:\/\/officetuts.net\/excel\/#\/schema\/person\/41b0b6996aaa4c4127f86f3d24452d42"},"headline":"Compare Two Columns in Excel","datePublished":"2019-04-03T16:56:25+00:00","dateModified":"2024-03-26T10:56:27+00:00","mainEntityOfPage":{"@id":"https:\/\/officetuts.net\/excel\/examples\/compare-columns-in-excel\/"},"wordCount":460,"publisher":{"@id":"https:\/\/officetuts.net\/excel\/#\/schema\/person\/41b0b6996aaa4c4127f86f3d24452d42"},"image":{"@id":"https:\/\/officetuts.net\/excel\/examples\/compare-columns-in-excel\/#primaryimage"},"thumbnailUrl":"http:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2019\/04\/matching-columns.png","keywords":["file","pinterest"],"articleSection":["examples"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/officetuts.net\/excel\/examples\/compare-columns-in-excel\/","url":"https:\/\/officetuts.net\/excel\/examples\/compare-columns-in-excel\/","name":"Compare Two Columns in Excel","isPartOf":{"@id":"https:\/\/officetuts.net\/excel\/#website"},"primaryImageOfPage":{"@id":"https:\/\/officetuts.net\/excel\/examples\/compare-columns-in-excel\/#primaryimage"},"image":{"@id":"https:\/\/officetuts.net\/excel\/examples\/compare-columns-in-excel\/#primaryimage"},"thumbnailUrl":"http:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2019\/04\/matching-columns.png","datePublished":"2019-04-03T16:56:25+00:00","dateModified":"2024-03-26T10:56:27+00:00","description":"Learn how to compare two columns in Excel using different methods. Use the formula presented in this tutorial to easily match rows in columns.","breadcrumb":{"@id":"https:\/\/officetuts.net\/excel\/examples\/compare-columns-in-excel\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/officetuts.net\/excel\/examples\/compare-columns-in-excel\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/officetuts.net\/excel\/examples\/compare-columns-in-excel\/#primaryimage","url":"http:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2019\/04\/matching-columns.png","contentUrl":"http:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2019\/04\/matching-columns.png"},{"@type":"BreadcrumbList","@id":"https:\/\/officetuts.net\/excel\/examples\/compare-columns-in-excel\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/officetuts.net\/excel\/"},{"@type":"ListItem","position":2,"name":"Compare Two Columns in Excel"}]},{"@type":"WebSite","@id":"https:\/\/officetuts.net\/excel\/#website","url":"https:\/\/officetuts.net\/excel\/","name":"","description":"","publisher":{"@id":"https:\/\/officetuts.net\/excel\/#\/schema\/person\/41b0b6996aaa4c4127f86f3d24452d42"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/officetuts.net\/excel\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/officetuts.net\/excel\/#\/schema\/person\/41b0b6996aaa4c4127f86f3d24452d42","name":"Tomasz Decker","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/officetuts.net\/excel\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/18cbe22837193574870ae40ba56bf712?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/18cbe22837193574870ae40ba56bf712?s=96&d=mm&r=g","caption":"Tomasz Decker"},"logo":{"@id":"https:\/\/officetuts.net\/excel\/#\/schema\/person\/image\/"},"description":"Spreadsheet and Python enthusiast."}]}},"_links":{"self":[{"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/posts\/4172"}],"collection":[{"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/comments?post=4172"}],"version-history":[{"count":6,"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/posts\/4172\/revisions"}],"predecessor-version":[{"id":13923,"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/posts\/4172\/revisions\/13923"}],"wp:attachment":[{"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/media?parent=4172"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/categories?post=4172"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/tags?post=4172"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}