Clear an Excel sheet using VB.Net

Posted on 2006-04-06
Last Modified: 2008-02-01
I have added a "clear" button to an excel sheet and I need the VB.Net code to clear the contents of the entire sheet, minus the column headings (1 line).  The ending cell will constantly be changing each time the user uses the "clear" button.
Question by:RFGNaper
    LVL 4

    Accepted Solution

    i'm going to suppose you already figured out how to select the worksheet you want to do this on.

    with .worksheet
        .Range(Selection, Selection.End(xlDown)).Select
    end with

    this clears the complete worksheet except for the first row.

    LVL 44

    Expert Comment

    Hello RFGNaper,

    if there is a continuous column like A or B you can get do it like

    assuming you already have your variables declared

    'this will give you the last row
    intRowCount = mySheet.[A1].End(xlDown).Row
    'this will clear all cells in the sheet except the first
    mySheet.Range("A2:Z" & intRowCount).Clear ' Z is you last column

    hope this helps a bit

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    Suggested Solutions

    Introduction This article makes the case for using two modules in your VBA/VB6 applications to provide both case-sensitive and case-insensitive text comparison operations.  Recently, I solved an EE question using the LIKE function.  In order for th…
    I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now