{"id":67593,"date":"2006-04-06T10:15:00","date_gmt":"2006-04-06T10:15:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/04\/06\/how-can-i-locate-and-replace-information-for-a-specific-item-in-a-spreadsheet\/"},"modified":"2006-04-06T10:15:00","modified_gmt":"2006-04-06T10:15:00","slug":"how-can-i-locate-and-replace-information-for-a-specific-item-in-a-spreadsheet","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-locate-and-replace-information-for-a-specific-item-in-a-spreadsheet\/","title":{"rendered":"How Can I Locate and Replace Information for a Specific Item in a Spreadsheet?"},"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! In an Excel spreadsheet, how can I locate and replace information for a specific computer?<BR><BR>&#8212; BB<\/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, BB. You know, there\u2019s an old saying that goes, \u201cIf all you have is a hammer then everything looks like a nail.\u201d Well, all we Scripting Guys have at the moment is some code for modifying a record in a database. So guess what: to us everything looks like a database, including your Excel spreadsheet.<\/P>\n<P>Fortunately, though, you actually <I>can<\/I> use database techniques to work with an Excel spreadsheet. And while we don\u2019t <I>have<\/I> to use this approach to answer your question, using some basic database commands seemed a lot easier than writing a bunch of code to open the spreadsheet, search for the computer in question, figure out what row and column we\u2019re in, change the data, save the spreadsheet, etc. etc. How much easier did it seem? This much easier:<\/P><PRE class=\"codeSample\">On Error Resume Next<\/p>\n<p>Const adOpenStatic = 3\nConst adLockOptimistic = 3\nConst adCmdText = &amp;H0001<\/p>\n<p>Set objConnection = CreateObject(&#8220;ADODB.Connection&#8221;)\nSet objRecordSet = CreateObject(&#8220;ADODB.Recordset&#8221;)<\/p>\n<p>objConnection.Open &#8220;Provider=Microsoft.Jet.OLEDB.4.0;&#8221; &amp; _\n    &#8220;Data Source=C:\\Scripts\\Inventory.xls;&#8221; &amp; _\n        &#8220;Extended Properties=&#8221;&#8221;Excel 8.0;HDR=Yes;&#8221;&#8221;;&#8221; <\/p>\n<p>objRecordset.Open &#8220;Select * FROM [Sheet1$]&#8221;, _\n    objConnection, adOpenStatic, adLockOptimistic, adCmdText<\/p>\n<p>strSearchCriteria = &#8220;Name = &#8216;atl-fs-01&#8242;&#8221;\nobjRecordSet.Find strSearchCriteria<\/p>\n<p>objRecordset(&#8220;IPAddress&#8221;) = &#8220;192.168.1.100&#8221;\nobjRecordset.Update<\/p>\n<p>objRecordset.Close\nobjConnection.Close\n<\/PRE>\n<P>Before we talk about the script itself we should mention that, for this sample script, we\u2019re assuming you have a very simple spreadsheet, one that looks something like this:<\/P><IMG border=\"0\" alt=\"Hey, Scripting Guy!\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/excelado1.jpg\" width=\"356\" height=\"295\"> \n<P><BR>As you can see, we have only two fields here: <B>Name<\/B> and <B>IPAddress<\/B>. Needless to say you aren\u2019t limited to just two fields; we kept our spreadsheet small in order to keep the script as simple as possible (and to enable us to keep the screenshot as small as possible). But you can have as many fields as you want (or at least as many fields as there are columns in Excel).<\/P>\n<P>We should also note that we won\u2019t explain today\u2019s script in minute detail; that\u2019s because we have an <A href=\"http:\/\/null\/technet\/scriptcenter\/resources\/officetips\/jun05\/tips0607.mspx\"><B>Office Space<\/B><\/A> article that already does that for us. We\u2019ll focus primarily on the code that updates the spreadsheet, and simply refer you to the Office Space article for more information on the ADO (ActiveX Data Objects) code that lets you treat Excel as a database.<\/P>\n<P>The script begins by defining three constants we need to use in order to make the ADO connection to the spreadsheet. After creating instances of the <B>ADODB.Connection<\/B> and <B>ADODB.Recordset<\/B> objects we then use this clunky-looking line of code to actually make the connection to the Excel spreadsheet:<\/P><PRE class=\"codeSample\">objConnection.Open &#8220;Provider=Microsoft.Jet.OLEDB.4.0;&#8221; &amp; _\n    &#8220;Data Source=C:\\Scripts\\Inventory.xls;&#8221; &amp; _\n        &#8220;Extended Properties=&#8221;&#8221;Excel 8.0;HDR=Yes;&#8221;&#8221;;&#8221;\n<\/PRE>\n<P>Like we said, there\u2019s no need to worry about things like Provider and Extended Properties, at least for now; instead, just note that you set the <B>Data Source<\/B> to the path to your Excel spreadsheet and let it go at that.<\/P>\n<P>After connecting to the spreadsheet we then use this line of code to select all the records (that is, all the rows in the spreadsheet):<\/P><PRE class=\"codeSample\">objRecordset.Open &#8220;Select * FROM [Sheet1$]&#8221;, _\n    objConnection, adOpenStatic, adLockOptimistic, adCmdText\n<\/PRE>\n<P>Notice that, in our Select statement, we select the worksheet where the data is stored; in this case, that\u2019s Sheet1, so we specify <B>[Sheet1$]<\/B>. What if we needed to get information from a worksheet we renamed Computers? No problem; just change the query accordingly:<\/P><PRE class=\"codeSample\">objRecordset.Open &#8220;Select * FROM [Computers$]&#8221;, _\n    objConnection, adOpenStatic, adLockOptimistic, adCmdText\n<\/PRE>\n<P>This gives us a recordset containing information about all the computers listed in the spreadsheet. That\u2019s fine, except that we\u2019re interested in only <I>one<\/I> computer: atl-fs-01. Therefore, we use these two lines of code to define a search criteria and to locate the record for atl-fs-01:<\/P><PRE class=\"codeSample\">strSearchCriteria = &#8220;Name = &#8216;atl-fs-01&#8242;&#8221;\nobjRecordSet.Find strSearchCriteria\n<\/PRE>\n<P>When we execute the <B>Find<\/B> method the cursor gets positioned at the record for atl-fs-01. (Incidentally, an error will be generated if no computer named atl-fs-01 can be found in the database. Because of that you might want to implement error-handling of some kind somewhere in the script.) What do we do now that we\u2019ve found the record we were looking for? Why, we do this, of course:<\/P><PRE class=\"codeSample\">objRecordset(&#8220;IPAddress&#8221;) = &#8220;192.168.1.100&#8221;\nobjRecordset.Update\n<\/PRE>\n<P>All we\u2019re doing here is assigning a new value to the IPAddress property; note that we don\u2019t need to worry about specifying which computer we\u2019re giving the new value. Why not? That\u2019s easy: the Find method has already located the computer for us. After assigning the new IP address we then call <B>Update<\/B> to write the changes to the database. Which, in this case, means changing the appropriate cell in the spreadsheet.<\/P>\n<P>Will this really work? Judge for yourself:<\/P><IMG border=\"0\" alt=\"Hey, Scripting Guy!\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/excelado2.jpg\" width=\"356\" height=\"295\"> \n<P><BR>Yes, we know: we thought we were crazy, too when we first suggested using ADO to update an Excel spreadsheet. But as you can see, ADO works pretty well, and with minimal effort on our part. In fact, if we could just use ADO to pound in all these nails we\u2019d be home free. Anyone want to trade a database for a hammer?<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! In an Excel spreadsheet, how can I locate and replace information for a specific computer?&#8212; BB Hey, BB. You know, there\u2019s an old saying that goes, \u201cIf all you have is a hammer then everything looks like a nail.\u201d Well, all we Scripting Guys have at the moment is some code for [&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":[19,146,710,48,49,3,5],"class_list":["post-67593","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-activex-data-objects-ado","tag-databases","tag-excel-spreadsheet","tag-microsoft-excel","tag-office","tag-scripting-guy","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! In an Excel spreadsheet, how can I locate and replace information for a specific computer?&#8212; BB Hey, BB. You know, there\u2019s an old saying that goes, \u201cIf all you have is a hammer then everything looks like a nail.\u201d Well, all we Scripting Guys have at the moment is some code for [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/67593","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=67593"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/67593\/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=67593"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=67593"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=67593"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}