VBA Code on multipe sheets

teranprasanna
Lounger
Posts: 27
Joined: 24 Jun 2020, 01:39

VBA Code on multipe sheets

Post by teranprasanna »

Hi All,

I need to run a specific code on multiple worksheets (Only for selected worksheets) within same workbook.

Any suggestions?

Appreciate your support.

Thanks!

User avatar
HansV
Administrator
Posts: 78474
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: VBA Code on multipe sheets

Post by HansV »

For example:

Code: Select all

Sub Example()
    Dim w As Worksheet
    For Each w In Worksheets(Array("ThisSheet", "ThatSheet", "OtherSheet"))
        ' Do something with w
    Next w
End Sub
You should not assume that w is the active sheet in the part between For Each w and Next w unless you use w/Select, but that is usually less efficient.
Best wishes,
Hans

teranprasanna
Lounger
Posts: 27
Joined: 24 Jun 2020, 01:39

Re: VBA Code on multipe sheets

Post by teranprasanna »

Thanks, it works!