Get The Last Used Excel Cell - Visual Basic .NET

Tags: VB.NET, VB 2008, VB 2010, VB 2012, VB 2013

This lesson describes how to get the last used cell in an excel 2007 or above file.

For a lesson on how to connect your project to Excel Sheet please see this tutorial

Add the following function. Change it to Return LastRow or LastColumn according to your need

Public Function LastUsedCell()
        Dim LastColumn As Long
        Dim LastRow As Long
        With worksheet
            LastRow = .Cells(.Rows.Count, 1).End(Excel.XlDirection.xlUp).Row
            LastColumn = .Cells(1, .Columns.Count).End(Excel.XlDirection.xlToLeft).Column
        End With
        Return LastColumn
        'Return LastRow
End Function

Then call the function when a user clicks a button. A TextBox will display the result


 Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) _
 Handles Button1.Click
        TextBox2.Text = LastUsedCell()
    End Sub


Share This

Home | About | Contact | Privacy Policy

Copyright visual-basic-tutorials.com 2017 - All Rights Reserved.