Clear an Excel sheet using VB.Net

Posted on 2006-04-06
Medium Priority
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

Accepted Solution

AndyAelbrecht earned 2000 total points
ID: 16391818
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

ID: 16391838
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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month15 days, 9 hours left to enroll

850 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