Clear Data Worksheets - Tidy Up Code

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

lkirkeAsked:
Who is Participating?
 
zorvek (Kevin Jones)Connect With a Mentor ConsultantCommented:
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
 
zorvek (Kevin Jones)Connect With a Mentor ConsultantCommented:
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
 
lkirkeAuthor Commented:
Thank you Kevin. Both great solutions.
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.