{"id":64153,"date":"2007-08-25T01:42:00","date_gmt":"2007-08-25T01:42:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/08\/25\/how-can-i-replace-numeric-values-in-an-excel-spreadsheet\/"},"modified":"2007-08-25T01:42:00","modified_gmt":"2007-08-25T01:42:00","slug":"how-can-i-replace-numeric-values-in-an-excel-spreadsheet","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-replace-numeric-values-in-an-excel-spreadsheet\/","title":{"rendered":"How Can I Replace Numeric Values in an Excel Spreadsheet?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"> \n<P>Hey, Scripting Guy! We have a spreadsheet where people enter a bunch of data. In order for this data to be used in another application, none of the values can exceed 999; if we have a value like 1234, that\u2019s <I>supposed<\/I> to be entered as 999, the maximum-allowed value. However, people have a tendency to enter the value as 1234; that means that we can\u2019t import this spreadsheet into our other application until we find this illegal value and change it to 999. How can I write a spreadsheet that will do this fix-up work for me?<BR><BR>&#8212; LK <\/P><IMG height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" height=\"288\" alt=\"Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\"><\/A> \n<P>Hey, LK. You know, in addition to taking a vow of poverty, the Scripting Guy who writes this column has also vowed to help anyone who asks him for help. (OK, so, technically, the Scripting Guy who writes this column didn\u2019t actually take a <I>vow<\/I> of poverty; instead, he sort of had poverty forced upon him. That\u2019s what happens when you have a 17-year-old son, especially one who plays baseball.) Because of his latter vow, the Scripting Guy who writes this column will try to help you; to be honest, however, he\u2019s a little concerned about anyone who comes to him requesting help with \u201cfix-up work.\u201d Fix-up work?!? You obviously haven\u2019t seen the Scripting House lately, have you?<\/P>\n<TABLE class=\"dataTable\" id=\"ECD\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P><B>Note<\/B>. OK, so, in all fairness, <I>no one<\/I> has seen the Scripting House lately; with the Scripting Family having been out of town for a couple weeks, the Scripting House is now hidden behind a jungle of uncut grass and overgrown weeds.<\/P>\n<P>Or at least we <I>assume<\/I> the Scripting House is still back there behind the jungle of uncut grass and overgrown weeds. It\u2019s kind of hard to tell.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>At any rate, fix-up work is definitely not what the Scripting Guy who writes this column does best. But maybe that isn\u2019t his fault. Instead, maybe that\u2019s because there isn\u2019t much need around the Scripting House for a script that goes through an Excel spreadsheet and changes any cell values greater than 999 to the maximum-allowed value of, well, 999:<\/P><PRE class=\"codeSample\">Set objExcel = CreateObject(&#8220;Excel.Application&#8221;)\nobjExcel.Visible = True<\/p>\n<p>Set objWorkbook = objExcel.Workbooks.Open(&#8220;C:\\Scripts\\Test.xls&#8221;)\nSet objWorksheet = objWorkbook.Worksheets(1)<\/p>\n<p>Set objRange = objWorksheet.UsedRange<\/p>\n<p>For Each objCell in objRange\n    If IsNumeric(objCell.Value) Then\n        If objCell.Value &gt; 999 Then\n            objCell.Value = 999\n        End If\n    End If\nNext\n<\/PRE>\n<P>As you can see, there really isn\u2019t much <I>to<\/I> this script; as it turns out, it\u2019s surprisingly easy to zip through a spreadsheet, find any values that are greater than 999, and then change those values to 999. For starters, we create an instance of the <B>Excel.Application<\/B> object and then set the <B>Visible<\/B> property to True; that gives us a running instance of Excel that we can see on screen. We then use these two lines of code to open the file C:\\Scripts\\Test.xls, and to bind to the first worksheet in that file:<\/P><PRE class=\"codeSample\">Set objWorkbook = objExcel.Workbooks.Open(&#8220;C:\\Scripts\\Test.xls&#8221;)\nSet objWorksheet = objWorkbook.Worksheets(1)\n<\/PRE>\n<P>Needless to say, there\u2019s nothing too terribly hard about any of that.<\/P>\n<P>Of course, we know what you\u2019re thinking: \u201cSure, there\u2019s nothing terribly hard about any of <I>that<\/I>. But now we have to figure out how to do a search-and-replace operation that can find \u2013 and then replace \u2013 any values greater than 999. How hard is <I>that<\/I> going to be?\u201d<\/P>\n<P>Well, to tell you the truth, that might be <I>very<\/I> hard; we don\u2019t know for sure. That\u2019s because we aren\u2019t going to do a true search-and-replace operation (that is, we aren\u2019t going to use Excel\u2019s <B>Replace<\/B> method). Although we might (or might not) be able to use the Replace method to find and replace these values, we opted for a much easier approach: we\u2019re simply going to grab a collection of all the cells in the worksheet, check the value of each cell individually, and then, if needed, replace that value with 999. As you\u2019re about to see, there\u2019s nothing too terribly hard about any of that, either.<\/P>\n<P>What\u2019s that? You don\u2019t believe us? You still think that this search and replace thing is going to be hard? Let\u2019s see if we can put your mind at ease by explaining exactly how this all works. <\/P>\n<P>To begin with, we use this line of code and the <B>UsedRange<\/B> property to grab a collection of all the cells in the worksheet. Nothing too terribly hard there:<\/P><PRE class=\"codeSample\">Set objRange = objWorksheet.UsedRange\n<\/PRE>\n<TABLE class=\"dataTable\" id=\"E2E\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P><B>Note<\/B>. OK, we need to clarify this a little. The UsedRange property doesn\u2019t necessarily return <I>all<\/I> the cells in the spreadsheet; instead, it returns the range of cells that actually contain data. For example, suppose you have data in cell A1 and another piece of data in cell D5. The UsedRange property returns cells A1 <I>through<\/I> D5; that is, the first cell that has data in it, the last cell that has data in it, and any cells in between.<\/P>\n<P>It goes without saying that the UsedRange property is a very handy little property to know about.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>When you create an instance of Excel\u2019s <B>Range<\/B> object (which is what happens when we reference the UsedRange property) we get back a collection of all the cells in that range. In turn, that means we can access each of the individual cells in the range simply by setting up a For Each loop to walk through all the items (cells) in the collection. And \u2013 surprise, surprise \u2013 that\u2019s exactly what we do next:<\/P><PRE class=\"codeSample\">For Each objCell in objRange\n<\/PRE>\n<P>Inside this For Each loop we do a couple of things. First, we need to determine whether the value in a given cell is a numeric value; if it\u2019s not, we\u2019re going to run into problems (i.e., script-crashing problems) when we try to figure out if that value is greater than 999. Therefore, to avoid problems, we use VBScript\u2019s <B>IsNumeric<\/B> function to determine whether or not the value in the first cell happens to be a number:<\/P><PRE class=\"codeSample\">If IsNumeric(objCell.Value) Then\n<\/PRE>\n<P>If IsNumeric returns False that means we\u2019re dealing with a string, a date, or some other non-numeric piece of information; in that case, we return to the top of the loop and repeat the process with the next cell in the collection. Let\u2019s assume, however, that IsNumeric returns True; that means that we <I>are<\/I> dealing with a numeric value. In that case, we then use this line of code to determine if that value is greater than 999:<\/P><PRE class=\"codeSample\">If objCell.Value &gt; 999 Then\n<\/PRE>\n<P>And what if the value <I>is<\/I> greater than 999? No problem; we simply assign a new value (999) to the cell:<\/P><PRE class=\"codeSample\">objCell.Value = 999\n<\/PRE>\n<P>From there we go back to the top of the loop and try again with the next cell in the collection. By the time we\u2019ve looped through the entire collection of cells any values greater than 999 will have been replaced with 999.<\/P>\n<P>Which, fortuitously enough, is just what we wanted them to be replaced with.<\/P>\n<P>That should do the trick, LK. If for some reason it <I>doesn\u2019t<\/I> do the trick, just let us know and we\u2019ll write you a new script. In fact, we might write you a new script anyway. In fact, we might write <I>everyone<\/I> a new script, as long as that means we can stay away from the Scripting Jungle.<\/P>\n<TABLE class=\"dataTable\" id=\"EPG\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P><B>Note<\/B>. How bad could the Scripting Jungle really be? Let\u2019s put it this way: eyewitnesses claim that there are soldiers living in that jungle who <A href=\"http:\/\/www.wanpela.com\/holdouts\/registry.html\" target=\"_blank\">still don\u2019t know that World War II is over<\/A>. To be honest, that\u2019s a little hard to believe.<\/P>\n<P>Although, come to think of it, we haven\u2019t seen the Scripting Son since we returned home from Italy. Hmmmm, you don\u2019t suppose \u2026.<\/P><\/TD><\/TR><\/TBODY><\/TABLE><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! We have a spreadsheet where people enter a bunch of data. In order for this data to be used in another application, none of the values can exceed 999; if we have a value like 1234, that\u2019s supposed to be entered as 999, the maximum-allowed value. However, people have a tendency to [&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-64153","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! We have a spreadsheet where people enter a bunch of data. In order for this data to be used in another application, none of the values can exceed 999; if we have a value like 1234, that\u2019s supposed to be entered as 999, the maximum-allowed value. However, people have a tendency to [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/64153","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=64153"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/64153\/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=64153"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=64153"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=64153"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}