?
Solved

Clear Data Worksheets - Tidy Up Code

Posted on 2011-04-27
3
Medium Priority
?
274 Views
Last Modified: 2012-05-11
Hello Experts,

I have the following code and am always seeking to refine it? Any suggestions on how I cna tidy this up?

Regards

LK
Sub Clear_Contents()

'---> Clears contents from each of the selected sheets
    
If MsgBox("Are you sure you want to import data into the workbook? This will clear within the sheets!!", vbYesNoCancel, "Clear Worksheets!!") = vbYes Then
    Sheets("AREA_1").Select
    Cells.Select
    Selection.ClearContents
    Sheets("AREA_2").Select
    Cells.Select
    Selection.ClearContents
    Sheets("COUNTRY_1").Select
    Cells.Select
    Selection.ClearContents
    Sheets("COUNTRY_2").Select
    Cells.Select
    Selection.ClearContents
    Sheets("COUNTRY_3").Select
    Cells.Select
    Selection.ClearContents
    Sheets("CITY_1").Select
    Cells.Select
    Selection.ClearContents
    Sheets("CITY_2").Select
    Cells.Select
    Selection.ClearContents
    Sheets("CITY_3").Select
    Cells.Select
    Selection.ClearContents
    
    End If

End Sub

Open in new window

0
Comment
Question by:lkirke
  • 2
3 Comments
 
LVL 81

Accepted Solution

by:
zorvek (Kevin Jones) earned 2000 total points
ID: 35480819
Sub Clear_Contents()
'---> Clears contents from each of the selected sheets
    If MsgBox("Are you sure you want to import data into the workbook? This will clear within the sheets!!", vbYesNoCancel, "Clear Worksheets!!") = vbYes Then
        Sheets("AREA_1").Cells.ClearContents
        Sheets("AREA_2").Cells.ClearContents
        Sheets("COUNTRY_1").Cells.ClearContents
        Sheets("COUNTRY_2").Cells.ClearContents
        Sheets("COUNTRY_3").Cells.ClearContents
        Sheets("CITY_1").Cells.ClearContents
        Sheets("CITY_2").Cells.ClearContents
        Sheets("CITY_3").Cells.ClearContents
    End If
End Sub

Kevin
0
 
LVL 81

Assisted Solution

by:zorvek (Kevin Jones)
zorvek (Kevin Jones) earned 2000 total points
ID: 35480825
Better:

Sub Clear_Contents()
'---> Clears contents from each of the selected sheets
    Dim SheetName As Variant
    If MsgBox("Are you sure you want to import data into the workbook? This will clear within the sheets!!", vbYesNoCancel, "Clear Worksheets!!") = vbYes Then
        For Each SheetName In Array("AREA_1", "AREA_2", "COUNTRY_1", "COUNTRY_2", "COUNTRY_3", "CITY_1", "CITY_2", "CITY_3")
            Sheets(SheetName).Cells.ClearContents
        Next SheetName
    End If
End Sub

Kevin
0
 

Author Closing Comment

by:lkirke
ID: 35480888
Thank you Kevin. Both great solutions.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

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