Need help with For Each WS in Worksheet method

Akbarov

Active Member
Joined
Jun 30, 2018
Messages
347
Office Version
  1. 365
Platform
  1. Windows
Hello dear community,

I have VBA code which works perfectly fine for one sheet, I want to modify it to work for each sheet.
If WS range (E69) = "Run VBA" to run VBA + loop , else ignore..

VBA Code:
 For Each WS In Worksheets
    If (WS.Name) <> "Run VBA" Then
        ThisWorkbook.Worksheets(WS.Name).Activate
        'Run VBA and loop...'

Code is something like this...
Instead of sheet ("DATA") I want to run code for each sheet which in cell E69 is = "Run VBA"
Is it simple or need complex change?


Code:
Sub XL2()
Dim wkname, pptname, currentPath As String

Application.DisplayAlerts = False


'Other Values

pptname = "Temp.pptx"
currentPath = ThisWorkbook.Path


'Ópening Powerpoint
Set ppApp = CreateObject("Powerpoint.Application")
Set pPres = ppApp.Presentations.Open(currentPath & Application.PathSeparator & pptname, msoFalse, msoCTrue, msoCTrue)
'Opening Excel
Set wk = ThisWorkbook



sldtoDuplicate = 1

Dim myvalues
myvalues = Array("DATA")
For my = LBound(myvalues) To UBound(myvalues)

        Set Sh = wk.Worksheets((myvalues(my)))
    
        
        i = 0
        For j = 0 To Sh.ChartObjects.Count
        
            For Each obj In Sh.ChartObjects
            '    Debug.Print
            
                
                If obj.TopLeftCell.Column = Sh.Range("F80").Offset(0, i).Column Then
                    'Duplicate First Slide
                    Set pattrenSlide = pPres.Slides(sldtoDuplicate)
                    Set sldrange = pattrenSlide.Duplicate
                    sldrange.MoveTo (pPres.Slides.Count)
                    Set sld = pPres.Slides(pPres.Slides.Count)
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Hi Akbarov, try this:
VBA Code:
Dim WS As Worksheet

For Each WS In ThisWorkbook.Sheets
    If WS.Range("E69").Value = "Run VBA" Then
        'your code here
        End If
     
      Next WS
 
Upvote 0
Thank you very much for this.
But can you please tell me what to do with this part of code?
VBA Code:
Dim myvalues
myvalues = Array("DATA")
For my = LBound(myvalues) To UBound(myvalues)

        Set Sh = wk.Worksheets((myvalues(my)))
 
Upvote 0
Thank you very much for this.
But can you please tell me what to do with this part of code?
VBA Code:
Dim myvalues
myvalues = Array("DATA")
For my = LBound(myvalues) To UBound(myvalues)

        Set Sh = wk.Worksheets((myvalues(my)))
Can anyone tell me please what to do with this part of code?
 
Upvote 0
I tried like this, but loop wont stop... I have 3 sheets, it copy sheet1 data, sheet2 data, sheet3 data then again sheet1,sheet2,sheet3 and so on for ever...

VBA Code:
  Dim WS As Worksheet

For Each WS In ThisWorkbook.Sheets
    If WS.Range("E66").Value = "Run VBA" Then
        'your code here
        Set sh = wk.ActiveSheet
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top