{"id":66863,"date":"2006-07-20T15:04:00","date_gmt":"2006-07-20T15:04:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/07\/20\/how-can-i-tell-if-a-specified-worksheet-exists-in-an-excel-workbook\/"},"modified":"2006-07-20T15:04:00","modified_gmt":"2006-07-20T15:04:00","slug":"how-can-i-tell-if-a-specified-worksheet-exists-in-an-excel-workbook","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-tell-if-a-specified-worksheet-exists-in-an-excel-workbook\/","title":{"rendered":"How Can I Tell If a Specified Worksheet Exists in an Excel Workbook?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> \n<P>Hey, Scripting Guy! How can I tell if a specified worksheet exists in an Excel workbook?<BR><BR>&#8212; MVK<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>Hey, MVK. Back when he was in high school (that was about 3 or 4 years ago, of course), the Scripting Guy who writes this column was kind of half-watching a variety show on TV. The show featured a man and his dogs: \u201cThe Smartest Dogs in the World!\u201d The man would show the dogs a cue card that had a multiple choice question on it. The dogs would &#8211; allegedly &#8211; read the cue card and then bark out the answer: one bark for A, two barks for B, etc.<\/P>\n<TABLE id=\"EYC\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. Sure, that <I>sounds<\/I> impressive. But, to tell you the truth, the questions were pretty easy; they were all questions like \u201cWho was the first President of the United States?\u201d Come on; even a <I>cat<\/I> could get that one right.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>At any rate, as this future Scripting Guy watched the show he suddenly realized how the trick worked: he figured out how the guy was telling his dogs which answer to bark out. Smelling an opportunity, the Scripting Guy recruited his brother and put together a similar act, one in which the two would lay an entire deck of cards down on a table. They would ask someone to select a card, and then &#8211; using his telekinetic powers &#8211; the Scripting Guy would beam the answer into his brother\u2019s mind. <I>One hundred percent of the time<\/I> the Scripting Brother would come up with the correct answer! The Scripting Guy and his brother spent many happy hours enthralling friends and relatives with their act. And no one ever came close to figuring out how they did it. <\/P>\n<P>The point: after mulling this over a bit, we\u2019ve decided that psychic powers are the best way to determine whether a specified worksheet exists in an Excel spreadsheet.<\/P>\n<P>What\u2019s that? No, we aren\u2019t going to tell you how the trick worked, even if you <I>do<\/I> promise not to tell anyone else. (What, you don\u2019t think we\u2019ve heard <I>that<\/I> before?) In fact, maybe it would be best to forego telekinesis altogether; now that we think about it, it might be better to use a script like this one instead:<\/P><PRE class=\"codeSample\">Set objExcel = CreateObject(&#8220;Excel.Application&#8221;)\nSet objWorkbook = objExcel.Workbooks.Open(&#8220;C:\\Scripts\\Test.xls&#8221;)<\/p>\n<p>x = 0<\/p>\n<p>For Each objWorksheet in objWorkbook.Worksheets\n    If objWorksheet.Name = &#8220;Budget&#8221; Then\n        x = 1\n        Exit For\n    End If\nNext<\/p>\n<p>objExcel.Quit<\/p>\n<p>If x = 1 Then\n    Wscript.Echo &#8220;The specified worksheet was found.&#8221;\nElse\n    Wscript.Echo &#8220;The specified worksheet was not found.&#8221;\nEnd If\n<\/PRE>\n<P>Granted, this script probably won\u2019t get you on any variety shows, but &#8211; never mind, forget we said that. After all, if the guy with The World\u2019s Smartest Dogs can get on a variety show, well who knows what you could do with this?<\/P>\n<P>As you probably already guessed, there isn\u2019t much magic to the script. We start out by creating an instance of the <B>Excel.Application<\/B> object, then use the <B>Open<\/B> method to open the file C:\\Scripts\\Test.xls.<BR><\/P>\n<TABLE id=\"ECE\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. Isn\u2019t this about the time we usually set the <B>Visible<\/B> property of Excel to True? Yes, it is. This time around, however, we decided to leave Excel running in an invisible window: that enables us to quickly open the workbook, look for the specified worksheet (a worksheet named <I>Budget<\/I>), and then close Excel, all without anyone knowing what we were doing or how we were doing it. (No, that\u2019s not magic: after all, when you\u2019re a Scripting Guy no one <I>ever<\/I> knows what you\u2019re doing, let alone how &#8211; or why &#8211; you\u2019re doing it.)<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>We then have this seemingly-random little line of code:<\/P><PRE class=\"codeSample\">x = 0\n<\/PRE>\n<P>Why are we, totally out of the blue, assigning the value 0 to a variable named x? We don\u2019t want to spoil the suspense, so hang tight; we\u2019ll explain why in a minute.<\/P>\n<P>Promise.<\/P>\n<P>Our next step is to set up a For Each loop that loops through the collection of all the worksheets found in Test.xls; that\u2019s what this line of code is for:<\/P><PRE class=\"codeSample\">For Each objWorksheet in objWorkbook.Worksheets\n<\/PRE>\n<P>Inside that For Each loop we simply check each worksheet to see if the sheet has a <B>Name<\/B> equal to <I>Budget<\/I>. If it doesn\u2019t, we skip past the worksheet and check the next item in the collection. If the Name <I>is<\/I> equal to Budget, we then assign x the value 1:<\/P><PRE class=\"codeSample\">x = 1\n<\/PRE>\n<P>Now you can probably see what we\u2019re doing here: we\u2019re just using x as a simple way to track whether or not we\u2019ve found a worksheet named Budget. This variable begins life with a value of 0; that value will never change \u2026 unless, of course, we find a worksheet named Budget. In turn, that means that we can determine whether or not the target worksheet was found simply by examining the value of x. If x is 0 then the worksheet wasn\u2019t found. But if x is 1 \u2026.<\/P>\n<TABLE id=\"EJF\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. Yes, by making a change here or there we could have reduced the size of this script by 3 or 4 lines. We chose not to do that simply because variables like our friend x can be very useful in scripting; instead, we decided to take the opportunity to show you a simple example of using a variable to keep track of some condition within the script. It might not seem like much, but it\u2019s a good little trick to have in your repertoire.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>And one other thing: if a worksheet named Budget is found we also execute the <B>Exit For<\/B> statement in order to exit our For Each loop. After all, once we find a worksheet named Budget we don\u2019t need to look through all the other worksheets in the collection; we\u2019ve already found what we were looking for. So why not exit the loop and get on with things?<\/P>\n<P>Good question: what <I>does<\/I> it mean to \u201cget on with things,\u201d at least in this script? Well, to begin with, we use this line of code to close Excel:<\/P><PRE class=\"codeSample\">objExcel.Quit\n<\/PRE>\n<P>And then we use this block of code to examine the value if x and report back whether or not the worksheet was found:<\/P><PRE class=\"codeSample\">If x = 1 Then\n    Wscript.Echo &#8220;The specified worksheet was found.&#8221;\nElse\n    Wscript.Echo &#8220;The specified worksheet was not found.&#8221;\nEnd If\n<\/PRE>\n<P>Yes, it\u2019s like magic. But it\u2019s not really magic.<\/P>\n<P>And don\u2019t bother asking <I>that<\/I> question: we aren\u2019t telling you how the mind-reading trick worked. (How did we know you were going to ask that question? Sorry; we can\u2019t tell you that, either.)<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I tell if a specified worksheet exists in an Excel workbook?&#8212; MVK Hey, MVK. Back when he was in high school (that was about 3 or 4 years ago, of course), the Scripting Guy who writes this column was kind of half-watching a variety show on TV. The show featured [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[710,48,49,3,5],"class_list":["post-66863","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-excel-spreadsheet","tag-microsoft-excel","tag-office","tag-scripting-guy","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I tell if a specified worksheet exists in an Excel workbook?&#8212; MVK Hey, MVK. Back when he was in high school (that was about 3 or 4 years ago, of course), the Scripting Guy who writes this column was kind of half-watching a variety show on TV. The show featured [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66863","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=66863"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66863\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=66863"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=66863"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=66863"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}