You are currently offline, waiting for your internet to reconnect

Visual Basic Example to Retrieve Value from Last Cell

Support for Office 2003 has ended

Microsoft ended support for Office 2003 on April 8, 2014. This change has affected your software updates and security options. Learn what this means for you and how to stay protected.

This article was previously published under Q141692
SUMMARY
Using Microsoft Visual Basic for Applications code, you can return the value of the last cell on any given sheet.
MORE INFORMATION
Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements. The following example retrieves data from the last cell on the worksheet, assigns it to a variable, and displays the variable in a message box. To use this macro, follow these steps:
  1. Open a new workbook and type the following macro code on a module sheet:
          Sub Get_Last_Cell_Value()          ' Dimension the variable.          Dim LastCellValue As Variant          ' Set the value of the last cell to the variable.          LastCellValue = ActiveCell.SpecialCells(xlLastCell).Value          ' Display the variable in a message box.          MsgBox LastCellValue      End Sub      Sub Get_Last_Cell_Address()          Dim LastCellAddress As String          ' Return the address of the last cell to the variable.          LastCellAddress = ActiveCell.SpecialCells(xlLastCell).Address          ' Display the variable in a message box.          MsgBox LastCellAddress      End Sub					
  2. On a blank worksheet in the workbook, type 1000 in cell Z100.
  3. Select the sheet that has the value in cell Z100.
  4. Run the Get_Last_Cell_Value macro.
When you run the Get_Last_Cell_Value macro and a blank or incorrect value is displayed in the message box, run the Get_Last_Cell_Address macro to find out the address of the last cell. If the cell address is incorrect, see the following article in the Microsoft Knowledge Base to resolve the problem.
244435 How to reset the last cell in Excel
REFERENCES
For more information about the last cell, click Answer Wizard on the Help menu and type:
Tell Me About The Last Cell
In Microsoft Excel version 5.0, click Contents on the Help menu. Select Programming With Visual Basic, click the Search button, and type:
SpecialCells
5.00a 5.00c 8.00 XL97 XL98 XL7 XL5 XL
Properties

Article ID: 141692 - Last Review: 01/19/2007 15:35:10 - Revision: 3.4

  • Microsoft Office Excel 2003
  • Microsoft Excel 2002 Standard Edition
  • Microsoft Excel 2000 Standard Edition
  • Microsoft Excel 97 Standard Edition
  • Microsoft Excel 95 Standard Edition
  • Microsoft Excel 5.0 Standard Edition
  • Microsoft Excel 98 for Macintosh
  • kbdtacode kbhowto kbprogramming KB141692
Feedback