Clear an Excel sheet using VB.Net

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.
RFGNaperAsked:
Who is Participating?
 
AndyAelbrechtCommented:
i'm going to suppose you already figured out how to select the worksheet you want to do this on.

with .worksheet
    .Rows("2:2").Select
    .Range(Selection, Selection.End(xlDown)).Select
    .Selection.ClearContents
end with

this clears the complete worksheet except for the first row.

cheers,
Andy
0
 
bruintjeCommented:
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
bruintje
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.