{"id":911,"date":"2020-11-27T23:32:09","date_gmt":"2020-11-27T23:32:09","guid":{"rendered":"http:\/\/www.vbastring.com\/blog\/?p=911"},"modified":"2020-11-28T20:36:29","modified_gmt":"2020-11-28T20:36:29","slug":"how-to-have-excel-check-if-range-of-cells-contains-specific-text-with-vba","status":"publish","type":"post","link":"https:\/\/vbastring.com\/blog\/2020\/11\/27\/how-to-have-excel-check-if-range-of-cells-contains-specific-text-with-vba\/","title":{"rendered":"How To Have Excel Check If Range Of Cells Contains Specific Text With VBA"},"content":{"rendered":"<p>Here&#8217;s an example of how to check if a value exists in a range of cells:<\/p>\n<p>It is actually a response from a question I received:<\/p>\n<blockquote><p>&#8220;Can you let me know how to check if a particular value is present in a named list.  Or in a different worksheet&#8221;<\/p><\/blockquote>\n<p>You can use the &#8220;<strong>countif<\/strong>&#8221; function.<\/p>\n<p>ex.  <em>=countif({value to look for}, {range to look in})<\/em><\/p>\n<p><strong>if the result is greater than zero, the value is in the range, if zero then it&#8217;s not there.<\/strong><\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/www.vbastring.com\/blog\/wp-content\/uploads\/2020\/11\/countif.png?resize=688%2C333\" alt=\"\" width=\"688\" height=\"333\" class=\"aligncenter size-full wp-image-914\" \/><\/p>\n<p>Take a look at the above example.  <\/p>\n<p>You&#8217;ll see that the built in countif Excel function returns a value greater than zero if the value be sought after is in the list (range), and zero if it&#8217;s not.<\/p>\n<h2>Now how do you use the countif function with VBA?<\/h2>\n<pre class=\"theme:familiar lang:vb decode:true \" >\r\n\r\n\r\n\r\nFunction GetNextInvoiceID(strYear, strMonth) As String\r\n    Dim intCounter As Integer\r\n    Dim varPos As Variant\r\n    Dim lngLastRow As Long\r\n    Dim strCounter As String\r\n    Dim blnFound As Boolean\r\n    \r\n   \r\n    lngLastRow = FindLastRow(\"A\")\r\n    \r\n    \r\n    For intCounter = 1 To 100\r\n        If Len(intCounter) &lt; 10 Then\r\n            strCounter = \"0\" &amp; intCounter\r\n        Else\r\n            strCounter = intCounter\r\n        End If\r\n        strInvoiceID = strYear &amp; strMonth &amp; strCounter\r\n        \r\n        blnFound = False\r\n        \r\n        intct = Application.WorksheetFunction.CountIf(Range(\"F1:F\" &amp; lngLastRow), strInvoiceID)\r\n        If intct &gt; 0 Then\r\n\r\n        Else\r\n\r\n           GetNextInvoiceID = strInvoiceID\r\n           Exit For\r\n        End If\r\n        \r\n    \r\n    Next\r\n\r\nEnd Function\r\n\r\nFunction FindLastRow(WhichColumn As String) As Long\r\n    \r\n    'FINDS THE LAST ROW BASED ON THE COLUMN LETTER <---\r\n    \r\n    Dim lngLastRow As Long\r\n    \r\n    'move to the last row on the worksheet and find the last used cell.\r\n    \r\n    With ActiveSheet\r\n        lngLastRow = ActiveSheet.Cells(1048576, WhichColumn).End(xlUp).Row\r\n    End With\r\n\r\n    FindLastRow = lngLastRow\r\n\r\nEnd Function\r\n<\/pre>\n<p>* Here I'm using the \"countif\" function, and returning the value to a variable called \"intct\" :<\/p>\n<p><em><strong>intct = Application.WorksheetFunction.CountIf(Range(\"F1:F\" & lngLastRow), strInvoiceID)<\/strong><\/em><\/p>\n<p>Questions?  <a href=\"http:\/\/www.vbastring.com\/blog\/contact-me\/\" rel=\"noopener noreferrer\" target=\"_blank\">Contact Me<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s an example of how to check if a value exists in a range of cells: It is actually a response from a question I received: &#8220;Can you let me know how to check if a particular value is present in a named list. Or in a different worksheet&#8221; You can use the &#8220;countif&#8221; function. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","om_disable_all_campaigns":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[],"tags":[],"class_list":["post-911","post","type-post","status-publish","format-standard","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How To Have Excel Check If Range Of Cells Contains Specific Text With VBA - My Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/vbastring.com\/blog\/2020\/11\/27\/how-to-have-excel-check-if-range-of-cells-contains-specific-text-with-vba\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How To Have Excel Check If Range Of Cells Contains Specific Text With VBA - My Blog\" \/>\n<meta property=\"og:description\" content=\"Here&#8217;s an example of how to check if a value exists in a range of cells: It is actually a response from a question I received: &#8220;Can you let me know how to check if a particular value is present in a named list. Or in a different worksheet&#8221; You can use the &#8220;countif&#8221; function. [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/vbastring.com\/blog\/2020\/11\/27\/how-to-have-excel-check-if-range-of-cells-contains-specific-text-with-vba\/\" \/>\n<meta property=\"og:site_name\" content=\"My Blog\" \/>\n<meta property=\"article:published_time\" content=\"2020-11-27T23:32:09+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-11-28T20:36:29+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/www.vbastring.com\/blog\/wp-content\/uploads\/2020\/11\/countif.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/2020\\\/11\\\/27\\\/how-to-have-excel-check-if-range-of-cells-contains-specific-text-with-vba\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/2020\\\/11\\\/27\\\/how-to-have-excel-check-if-range-of-cells-contains-specific-text-with-vba\\\/\"},\"author\":{\"name\":\"admin\",\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/#\\\/schema\\\/person\\\/e1de0b30e98940381697872449c341f5\"},\"headline\":\"How To Have Excel Check If Range Of Cells Contains Specific Text With VBA\",\"datePublished\":\"2020-11-27T23:32:09+00:00\",\"dateModified\":\"2020-11-28T20:36:29+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/2020\\\/11\\\/27\\\/how-to-have-excel-check-if-range-of-cells-contains-specific-text-with-vba\\\/\"},\"wordCount\":178,\"image\":{\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/2020\\\/11\\\/27\\\/how-to-have-excel-check-if-range-of-cells-contains-specific-text-with-vba\\\/#primaryimage\"},\"thumbnailUrl\":\"http:\\\/\\\/www.vbastring.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/11\\\/countif.png\",\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/2020\\\/11\\\/27\\\/how-to-have-excel-check-if-range-of-cells-contains-specific-text-with-vba\\\/\",\"url\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/2020\\\/11\\\/27\\\/how-to-have-excel-check-if-range-of-cells-contains-specific-text-with-vba\\\/\",\"name\":\"How To Have Excel Check If Range Of Cells Contains Specific Text With VBA - My Blog\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/2020\\\/11\\\/27\\\/how-to-have-excel-check-if-range-of-cells-contains-specific-text-with-vba\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/2020\\\/11\\\/27\\\/how-to-have-excel-check-if-range-of-cells-contains-specific-text-with-vba\\\/#primaryimage\"},\"thumbnailUrl\":\"http:\\\/\\\/www.vbastring.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/11\\\/countif.png\",\"datePublished\":\"2020-11-27T23:32:09+00:00\",\"dateModified\":\"2020-11-28T20:36:29+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/#\\\/schema\\\/person\\\/e1de0b30e98940381697872449c341f5\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/2020\\\/11\\\/27\\\/how-to-have-excel-check-if-range-of-cells-contains-specific-text-with-vba\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/vbastring.com\\\/blog\\\/2020\\\/11\\\/27\\\/how-to-have-excel-check-if-range-of-cells-contains-specific-text-with-vba\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/2020\\\/11\\\/27\\\/how-to-have-excel-check-if-range-of-cells-contains-specific-text-with-vba\\\/#primaryimage\",\"url\":\"http:\\\/\\\/www.vbastring.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/11\\\/countif.png\",\"contentUrl\":\"http:\\\/\\\/www.vbastring.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/11\\\/countif.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/2020\\\/11\\\/27\\\/how-to-have-excel-check-if-range-of-cells-contains-specific-text-with-vba\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How To Have Excel Check If Range Of Cells Contains Specific Text With VBA\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/\",\"name\":\"My Blog\",\"description\":\"My WordPress Blog\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/#\\\/schema\\\/person\\\/e1de0b30e98940381697872449c341f5\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b2feaa698a4bd91b409df1beb5ff6acc2d7842d4f78543d413ebd468bc0171af?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b2feaa698a4bd91b409df1beb5ff6acc2d7842d4f78543d413ebd468bc0171af?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b2feaa698a4bd91b409df1beb5ff6acc2d7842d4f78543d413ebd468bc0171af?s=96&d=mm&r=g\",\"caption\":\"admin\"},\"sameAs\":[\"https:\\\/\\\/vbastring.com\\\/blog\"],\"url\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/author\\\/admin\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How To Have Excel Check If Range Of Cells Contains Specific Text With VBA - My Blog","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:\/\/vbastring.com\/blog\/2020\/11\/27\/how-to-have-excel-check-if-range-of-cells-contains-specific-text-with-vba\/","og_locale":"en_US","og_type":"article","og_title":"How To Have Excel Check If Range Of Cells Contains Specific Text With VBA - My Blog","og_description":"Here&#8217;s an example of how to check if a value exists in a range of cells: It is actually a response from a question I received: &#8220;Can you let me know how to check if a particular value is present in a named list. Or in a different worksheet&#8221; You can use the &#8220;countif&#8221; function. [&hellip;]","og_url":"https:\/\/vbastring.com\/blog\/2020\/11\/27\/how-to-have-excel-check-if-range-of-cells-contains-specific-text-with-vba\/","og_site_name":"My Blog","article_published_time":"2020-11-27T23:32:09+00:00","article_modified_time":"2020-11-28T20:36:29+00:00","og_image":[{"url":"http:\/\/www.vbastring.com\/blog\/wp-content\/uploads\/2020\/11\/countif.png","type":"","width":"","height":""}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"Written by":"admin","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/vbastring.com\/blog\/2020\/11\/27\/how-to-have-excel-check-if-range-of-cells-contains-specific-text-with-vba\/#article","isPartOf":{"@id":"https:\/\/vbastring.com\/blog\/2020\/11\/27\/how-to-have-excel-check-if-range-of-cells-contains-specific-text-with-vba\/"},"author":{"name":"admin","@id":"https:\/\/vbastring.com\/blog\/#\/schema\/person\/e1de0b30e98940381697872449c341f5"},"headline":"How To Have Excel Check If Range Of Cells Contains Specific Text With VBA","datePublished":"2020-11-27T23:32:09+00:00","dateModified":"2020-11-28T20:36:29+00:00","mainEntityOfPage":{"@id":"https:\/\/vbastring.com\/blog\/2020\/11\/27\/how-to-have-excel-check-if-range-of-cells-contains-specific-text-with-vba\/"},"wordCount":178,"image":{"@id":"https:\/\/vbastring.com\/blog\/2020\/11\/27\/how-to-have-excel-check-if-range-of-cells-contains-specific-text-with-vba\/#primaryimage"},"thumbnailUrl":"http:\/\/www.vbastring.com\/blog\/wp-content\/uploads\/2020\/11\/countif.png","inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/vbastring.com\/blog\/2020\/11\/27\/how-to-have-excel-check-if-range-of-cells-contains-specific-text-with-vba\/","url":"https:\/\/vbastring.com\/blog\/2020\/11\/27\/how-to-have-excel-check-if-range-of-cells-contains-specific-text-with-vba\/","name":"How To Have Excel Check If Range Of Cells Contains Specific Text With VBA - My Blog","isPartOf":{"@id":"https:\/\/vbastring.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/vbastring.com\/blog\/2020\/11\/27\/how-to-have-excel-check-if-range-of-cells-contains-specific-text-with-vba\/#primaryimage"},"image":{"@id":"https:\/\/vbastring.com\/blog\/2020\/11\/27\/how-to-have-excel-check-if-range-of-cells-contains-specific-text-with-vba\/#primaryimage"},"thumbnailUrl":"http:\/\/www.vbastring.com\/blog\/wp-content\/uploads\/2020\/11\/countif.png","datePublished":"2020-11-27T23:32:09+00:00","dateModified":"2020-11-28T20:36:29+00:00","author":{"@id":"https:\/\/vbastring.com\/blog\/#\/schema\/person\/e1de0b30e98940381697872449c341f5"},"breadcrumb":{"@id":"https:\/\/vbastring.com\/blog\/2020\/11\/27\/how-to-have-excel-check-if-range-of-cells-contains-specific-text-with-vba\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/vbastring.com\/blog\/2020\/11\/27\/how-to-have-excel-check-if-range-of-cells-contains-specific-text-with-vba\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/vbastring.com\/blog\/2020\/11\/27\/how-to-have-excel-check-if-range-of-cells-contains-specific-text-with-vba\/#primaryimage","url":"http:\/\/www.vbastring.com\/blog\/wp-content\/uploads\/2020\/11\/countif.png","contentUrl":"http:\/\/www.vbastring.com\/blog\/wp-content\/uploads\/2020\/11\/countif.png"},{"@type":"BreadcrumbList","@id":"https:\/\/vbastring.com\/blog\/2020\/11\/27\/how-to-have-excel-check-if-range-of-cells-contains-specific-text-with-vba\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/vbastring.com\/blog\/"},{"@type":"ListItem","position":2,"name":"How To Have Excel Check If Range Of Cells Contains Specific Text With VBA"}]},{"@type":"WebSite","@id":"https:\/\/vbastring.com\/blog\/#website","url":"https:\/\/vbastring.com\/blog\/","name":"My Blog","description":"My WordPress Blog","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/vbastring.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/vbastring.com\/blog\/#\/schema\/person\/e1de0b30e98940381697872449c341f5","name":"admin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/b2feaa698a4bd91b409df1beb5ff6acc2d7842d4f78543d413ebd468bc0171af?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/b2feaa698a4bd91b409df1beb5ff6acc2d7842d4f78543d413ebd468bc0171af?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/b2feaa698a4bd91b409df1beb5ff6acc2d7842d4f78543d413ebd468bc0171af?s=96&d=mm&r=g","caption":"admin"},"sameAs":["https:\/\/vbastring.com\/blog"],"url":"https:\/\/vbastring.com\/blog\/author\/admin\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/vbastring.com\/blog\/wp-json\/wp\/v2\/posts\/911","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/vbastring.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vbastring.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vbastring.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/vbastring.com\/blog\/wp-json\/wp\/v2\/comments?post=911"}],"version-history":[{"count":5,"href":"https:\/\/vbastring.com\/blog\/wp-json\/wp\/v2\/posts\/911\/revisions"}],"predecessor-version":[{"id":926,"href":"https:\/\/vbastring.com\/blog\/wp-json\/wp\/v2\/posts\/911\/revisions\/926"}],"wp:attachment":[{"href":"https:\/\/vbastring.com\/blog\/wp-json\/wp\/v2\/media?parent=911"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vbastring.com\/blog\/wp-json\/wp\/v2\/categories?post=911"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vbastring.com\/blog\/wp-json\/wp\/v2\/tags?post=911"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}