{"id":3270,"date":"2021-04-24T10:01:41","date_gmt":"2021-04-24T17:01:41","guid":{"rendered":"https:\/\/SUMMALAI.COM\/?p=3270"},"modified":"2021-04-24T10:01:42","modified_gmt":"2021-04-24T17:01:42","slug":"how-to-check-if-a-worksheet-exists-in-excel-vba","status":"publish","type":"post","link":"https:\/\/SUMMALAI.COM\/?p=3270","title":{"rendered":"How to Check if a Worksheet Exists in Excel VBA"},"content":{"rendered":"\n<p>There is a couple of ways to do this.<\/p>\n\n\n\n<p>Solutions 1,  use a for &#8211; next loop to check worksheet one by one.<\/p>\n\n\n\n<p><em>Function WorksheetExists(ByVal WorksheetName As String) As Boolean<br>Dim Sht As Worksheet<br>For Each Sht In ThisWorkbook.Worksheets<br>If Application.Proper(Sht.Name) = Application.Proper(WorksheetName) Then<br>WorksheetExists = True<br>Exit Function<br>End If<br>Next Sht<br>WorksheetExists = False<br>End Function<\/em><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img fetchpriority=\"high\" decoding=\"async\" width=\"396\" height=\"364\" src=\"https:\/\/SUMMALAI.COM\/wp-content\/uploads\/2021\/04\/image-5.png\" alt=\"\" class=\"wp-image-3271\" srcset=\"https:\/\/SUMMALAI.COM\/wp-content\/uploads\/2021\/04\/image-5.png 396w, https:\/\/SUMMALAI.COM\/wp-content\/uploads\/2021\/04\/image-5-300x276.png 300w\" sizes=\"(max-width: 396px) 100vw, 396px\" \/><figcaption>This is a sample for deleting a worksheet if exists<\/figcaption><\/figure>\n\n\n\n<p>Solution 2, <\/p>\n\n\n\n<p>Setting the non-existent sheet as an object and then blowing through the errors is clever indeed. Also learned from the use of wildcard * in the second suggestion.<\/p>\n\n\n\n<p><em>Public Function WorksheetExists(ByVal WorksheetName As String) As Boolean<br>On Error Resume Next<br>WorksheetExists = (Sheets(WorksheetName).Name &lt;&gt; &#8220;&#8221;)<br>On Error GoTo 0<br>End Function<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>There is a couple of ways to do this. Solutions 1, use a for &#8211; next loop to check worksheet one by one. Function WorksheetExists(ByVal WorksheetName As String) As BooleanDim Sht As WorksheetFor Each Sht In ThisWorkbook.WorksheetsIf Application.Proper(Sht.Name) = Application.Proper(WorksheetName) ThenWorksheetExists = TrueExit FunctionEnd IfNext ShtWorksheetExists = FalseEnd Function Solution 2, Setting the non-existent sheet <a class=\"read-more\" href=\"https:\/\/SUMMALAI.COM\/?p=3270\">Read More<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[10,18],"tags":[700],"class_list":["post-3270","post","type-post","status-publish","format-standard","hentry","category-microsoft","category-microsoft-office","tag-check-if-a-worksheet-exists"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=\/wp\/v2\/posts\/3270","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3270"}],"version-history":[{"count":2,"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=\/wp\/v2\/posts\/3270\/revisions"}],"predecessor-version":[{"id":3273,"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=\/wp\/v2\/posts\/3270\/revisions\/3273"}],"wp:attachment":[{"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3270"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3270"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3270"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}