Solved

VBA to clear data from worksheets

Posted on 2014-10-10
6
176 Views
Last Modified: 2014-10-10
I have a file that has over 30 different worksheets. Some of these have data I need to keep but the majority have data that needs to be cleared so that we can start collecting the current data and post on the sheets.

Could an expert provide me with VBA code that will clear data from all the sheets in the workbook except for those that I need to keep. There are currently 6 sheets that I need to keep so we can call them Sheet1, Sheet2 etc etc up to Sheet6

Many thanks
0
Comment
Question by:Jagwarman
6 Comments
 
LVL 22

Expert Comment

by:Ivo Stoykov
ID: 40372412
Hi

You could use this macro and call it for each sheet you want

Sub DeleteSheetContetn()
    Cells.Select
    Selection.ClearContents
    Range("A1").Select
End Sub

Open in new window

HTH
Ivo Stoykov
0
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40372415
Define "clear"? Do you want to keep headers (and if so, which rows), page setup? Are there formulas already set up?
0
 
LVL 85

Accepted Solution

by:
Rory Archibald earned 500 total points
ID: 40372423
Here's a simple example of completely clearing the sheets:

Sub ClearSheets()
Dim wks as Worksheet
for each wks in activeworkbook.worksheets
select case LCase$(wks.name)
' note the sheet names should be listed in lower case!
Case "sheet1", "sheet2", "sheet3", "sheet4","sheet5","sheet6"
' do nothing
Case Else
wks.Usedrange.Clear
End Select
Next
End Sub

Open in new window

0
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 

Author Comment

by:Jagwarman
ID: 40372431
Hi Phillip

in this case I need to clear the sheets completely headers and all

Regards
0
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40372438
Then both Rory's and Ivo's solutions both work.
0
 

Author Comment

by:Jagwarman
ID: 40372450
As I need to clear so many sheets Rory's is the better solution for me, but thank you all for your input
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

820 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