Solved

VBA to clear data from worksheets

Posted on 2014-10-10
6
169 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

863 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

27 Experts available now in Live!

Get 1:1 Help Now