{"id":63673,"date":"2007-11-02T00:12:00","date_gmt":"2007-11-02T00:12:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/11\/02\/hey-scripting-guy-how-can-i-determine-the-value-of-the-last-cell-in-an-office-excel-range\/"},"modified":"2007-11-02T00:12:00","modified_gmt":"2007-11-02T00:12:00","slug":"hey-scripting-guy-how-can-i-determine-the-value-of-the-last-cell-in-an-office-excel-range","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-determine-the-value-of-the-last-cell-in-an-office-excel-range\/","title":{"rendered":"Hey, Scripting Guy! How Can I Determine the Value of the Last Cell in an Office Excel Range?"},"content":{"rendered":"<p><H2><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\"> <\/H2>\n<P>Hey, Scripting Guy! How can I determine the value of the last cell in a range like C1:C10?<BR><BR>&#8212; MJdR<\/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, MJdR. You know, just a few days ago word leaked that a Federal Emergency Management Agency (FEMA) press conference held to discuss the US government\u2019s response to the recent spate of wildfires in California was a fake. Instead of having real reporters asking real questions the press conference consisted of FEMA employees asking phony, pre-screened questions. That led to some major concerns and worries here in the US: after all, if FEMA press conferences are phony, does that mean that Scripting Guys press conferences are phony as well? Because so many people are losing sleep over this matter, we\u2019d like to take a moment to put your fears to rest. Have no fear, America: the Scripting Guys press conferences are as real as they\u2019ve ever been. <\/P>\n<P>Now, let\u2019s take a moment to answer a few questions from the audience:<\/P>\n<TABLE id=\"E3C\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P>Hey, Scripting Guy! I don\u2019t really have a question, I just wanted to let you know that I think your daily column is not only the best daily scripting column I\u2019ve ever read, but also the best column of <I>any<\/I> kind I\u2019ve ever read. I just wanted to thank you for all the hard work you\u2019ve done. I know the word \u201cgenius\u201d is grossly overused these days, but if the shoe fits \u2026.<\/P>\n<P>&#8212; PC<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Thank you, PC; we appreciate that. How about the gentleman over there:<\/P>\n<TABLE id=\"EKD\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P>Hey, Scripting Guy! As I understand it, you\u2019ve been writing this daily column for over three years now. After all this time, do you find it difficult to continue turning out columns that are so incredibly well-written, informative, and entertaining?<\/P>\n<P>&#8212; DT<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Again, thank you for those kind words, DT. In answer to your question, no, we don\u2019t find it difficult in the least. After all, we\u2019re doing what we love, and we couldn\u2019t ask for anything more than that. How about the lady with the angry snarl on her face:<\/P>\n<TABLE id=\"EUD\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P>Hey, Scripting Guy! Don\u2019t you find it a little suspicious that PC has the same initials as Scripting Guy Peter Costantini, and that DT has the same initials as Scripting Guy Dean Tsaltas?<\/P>\n<P>&#8212; The Scripting Editor<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>That\u2019s a very good observation, Scripting Editor. However, we believe that MJdR asked a question before you did, something about determining the value of the last cell in a range. In all fairness, we should address his question first, then get back to you if time allows. Here you go, MJdR:<\/P><PRE class=\"codeSample\">Const xlCellTypeLastCell = 11<\/p>\n<p>Set objExcel = CreateObject(&#8220;Excel.Application&#8221;)\nobjExcel.Visible = True<\/p>\n<p>Set objWorkbook = objExcel.Workbooks.Add()\nSet objWorksheet = objWorkbook.Worksheets(1)<\/p>\n<p>objWorksheet.Cells(1, 1) = 1\nobjWorksheet.Cells(2, 1) = 2\nobjWorksheet.Cells(3, 1) = &#8220;&#8221;\nobjWorksheet.Cells(4, 1) = 4\nobjWorksheet.Cells(5, 1) = &#8220;&#8221;\nobjWorksheet.Cells(6, 1) = 6\nobjWorksheet.Cells(7, 1) = 7<\/p>\n<p>Set objRange = objExcel.Range(&#8220;A1:A7&#8221;)\nobjRange.Cells.SpecialCells(xlCellTypeLastCell).Activate<\/p>\n<p>Wscript.Echo objExcel.ActiveCell.Value\n<\/PRE>\n<P>Any further questions? <\/P>\n<P>Um, we mean besides yours, Scripting Editor. <\/P>\n<P>Ah, right: how <I>does<\/I> this script work? Unlike some of the other questions we\u2019ve been asked recently, that\u2019s a very <I>good<\/I> question.<\/P>\n<P>To begin with, we define a constant named xlCellTypeLastCell and set the value to 11; we\u2019ll use this constant to tell the script which cell in the range we want to maneuver to. (Needless to say, that would be the last cell in the range.) After defining the constant 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 onscreen. And once we <I>have<\/I> a running instance of Excel that we can see onscreen, that frees us to use these two lines of code to create a new workbook and then bind ourselves to the first worksheet in that workbook:<\/P><PRE class=\"codeSample\">Set objWorkbook = objExcel.Workbooks.Add()\nSet objWorksheet = objWorkbook.Worksheets(1)\n<\/PRE>\n<P>What follows next are seven lines of code that simply assign values to cells A1 through A7; that means you don\u2019t have to worry too much about any of those lines. You might have noticed, however, that we actually assigned cells A3 and A5 empty strings. Why? Well, as you probably already know, Excel can sometimes be \u201ctricked\u201d into thinking that it\u2019s encountered the end of a range simply because it encounters a blank cell somewhere in that range. We tossed the blank cells into <I>our<\/I> range just to demonstrate that the script will work even if a range has a blank cell or two.<\/P>\n<P>Thank you, but please hold your applause until the end. Thanks again.<\/P>\n<P>That brings us to these two lines of code:<\/P><PRE class=\"codeSample\">Set objRange = objExcel.Range(&#8220;A1:A7&#8221;)\nobjRange.Cells.SpecialCells(xlCellTypeLastCell).Activate\n<\/PRE>\n<P>In the first line, we\u2019re creating a <B>Range<\/B> object that encompasses cells A1 through A7; that should be fairly straightforward. So then what are we doing in line 2? Well, in line 2 we\u2019re doing a couple of things. First, we\u2019re calling the <B>SpecialCells<\/B> method, a method that belongs to the Range object\u2019s <B>Cells<\/B> collection. The SpecialCells method is deigned to return a collection of, well, special cells; this could be all the blank cells in a collection; all the cells that contain the same formatting; all the cells that contain a formula; and so on. (For more information, see the <A href=\"http:\/\/msdn2.microsoft.com\/en-us\/library\/aa213567(office.11).aspx\" target=\"_blank\"><B>Excel Language Reference<\/B><\/A> on MSDN.) In our case we\u2019re passing SpecialCells the constant xlCellTypeLastCell; this tells the method to return a collection containing a single cell: the very last cell in the range.<\/P>\n<P>OK, that\u2019s one thing that we do with that line of code: we get back a collection containing the very last cell in the range. The other thing we do is call the <B>Activate<\/B> method, something that \u2013 once we\u2019ve identified the last cell in the range \u2013 makes that cell the active cell.<\/P>\n<P>Why do we bother activating the last cell in the range? That\u2019s easy; that way we can echo back the value of the cell by using this line of code:<\/P><PRE class=\"codeSample\">Wscript.Echo objExcel.ActiveCell.Value\n<\/PRE>\n<P>By using the <B>ActiveCell<\/B> property we can retrieve the value even if we don\u2019t know the address of the last cell in the range. (Which, in a real script, is likely.)<\/P>\n<P>Now, what do you suppose we\u2019re going to get when we echo back the value of the active cell? You got it; we\u2019re going to get back the value of cell A7, the last cell in the range:<\/P><PRE class=\"codeSample\">7\n<\/PRE>\n<P>That\u2019s all we have to do. <I>Now<\/I> you can applaud.<\/P>\n<P>Oh, wait: we still need to answer the Scripting Editor\u2019s question, don\u2019t we? As it turns out, there\u2019s nothing suspicious here at all. To begin with, Peter Costantini\u2019s initials are <I>not<\/I> PC; for reasons known only to himself, Peter actually uses Y6 as his initials. Likewise, Dean Tsaltas\u2019 initials are not DT, either; being a Canadian citizen, Dean does not have any initials. We hope that clears up any misconceptions.<\/P>\n<P>That concludes today\u2019s press conference. Thank you all for coming, and please drive home safely. We\u2019ll see everyone tomorrow, same time, same place.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I determine the value of the last cell in a range like C1:C10?&#8212; MJdR Hey, MJdR. You know, just a few days ago word leaked that a Federal Emergency Management Agency (FEMA) press conference held to discuss the US government\u2019s response to the recent spate of wildfires in California was [&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":[711,48,49,3,5],"class_list":["post-63673","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-excel-application","tag-microsoft-excel","tag-office","tag-scripting-guy","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I determine the value of the last cell in a range like C1:C10?&#8212; MJdR Hey, MJdR. You know, just a few days ago word leaked that a Federal Emergency Management Agency (FEMA) press conference held to discuss the US government\u2019s response to the recent spate of wildfires in California was [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/63673","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=63673"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/63673\/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=63673"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=63673"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=63673"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}