Tidying up code

Hi,

I have some code below, i must repeat the action for 4 other spreadsheets

Europe.xlms
LATAM.xlms etc etc

Instead of having the code repeated an additional 4 times, can i create some form of loop that is tidier and more efficient?

Many thanks

Sub ClearSheets()

Windows("Asia.xlsm").Activate

With ActiveWorkbook

Sheets("PB Review").Select
ActiveSheet.UsedRange.Clear

Sheets("PM Review").Select
ActiveSheet.UsedRange.Clear

End With

End Sub

Open in new window

Seamus2626Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SteveCommented:
A couple of suggestions here...

Cycle through sheets or call each individually.
Up to you which works...

Sub MainSheets()

Windows("Asia.xlsm").Activate

Call ClearSheets(Sheets("PB Review"))
'Call ClearSheets(Sheets("PB Review"))  'change this line for each seet

End Sub


Sub AllSheets()

Dim ws As Worksheet

Windows("Asia.xlsm").Activate

For Each ws In ThisWorkbook.Sheets

    Call ClearSheets(ws)

Next ws

End Sub


Sub ClearSheets(ws As Worksheet)

With ws

    .UsedRange.Clear

End With

End Sub

Open in new window

U--Example.xlsm
0
Rgonzo1971Commented:
Hi,

pls try

Sub ClearSheets()
aWbkList = Array("Asia.xlsm", "Europe.xlsm", "LATAM.xlsm")
For Each Item In aWbkList
    Windows(Item).Activate
    With ActiveWorkbook
        Sheets("PB Review").UsedRange.Clear
        Sheets("PM Review").UsedRange.Clear
    End With
Next
End Sub

Regards
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SteveCommented:
Oops.. I read worksheet, not workbook...

Workbook example below:

Sub MainSheets()

Call ClearSheets(Workbooks("Example.xlsm"))
Call ClearSheets(Workbooks("Europe.xlsm"))
Call ClearSheets(Workbooks("LATAM.xlsm"))

End Sub



Sub ClearSheets(wb As Workbook)

With wb

    .Sheets("Sheet1").UsedRange.Clear
    .Sheets("Sheet2").UsedRange.Clear

End With

End Sub

Open in new window

U--Example.xlsm
0
Seamus2626Author Commented:
Thanks guys!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.

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.