Looping macro issue

davez

Board Regular
Joined
Feb 12, 2003
Messages
144
Office Version
  1. 365
Platform
  1. Windows
Hi all, hoping someone can let me know what I am doing wrong here - I need to run a macro on all sheets within a workbook but the code below is only running it on the first sheet, any help?

VBA Code:
Sub WorksheetLoop_RunMacroAllSheets()
 
Application.ScreenUpdating = False
Application.Calculation = xlManual

 
Dim i As Long
Dim shtCount As Long

shtCount = Sheets.Count

For i = 1 To shtCount

Call COPY_D1_O1_DOWN

Next i
         
Application.ScreenUpdating = True
Application.Calculation = xlAutomatic

End Sub
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Expanding on @johnnyL's comments ...

You haven't provided your code for COPY_D1_O1_DOWN. But how will that Sub know which sheet it's working on?

VBA Code:
Sub Test()

    Dim i As Long
        
    For i = 1 To Sheets.Count
        Call COPY_D1_O1_DOWN
    Next i

End Sub
Sub COPY_D1_O1_DOWN()

    'Gets called multiple times.  But always operates on ActiveSheet
    Range("D1:O1").Copy Range("D2")

End Sub

Here's an alternative:

Code:
Sub Test2()
        
    Dim ws As Worksheet
    
    For Each ws In ThisWorkbook.Worksheets
        Call COPY_D1_O1_DOWN2(ws)
    Next ws

End Sub
Sub COPY_D1_O1_DOWN2(ws As Worksheet)

    With ws
        .Range("D1:O1").Copy .Range("D2")
    End With
    
End Sub
 
Upvote 0
Thanks guys, sorry if I was a bit vague. Anyway got this to work

VBA Code:
Sub WorksheetLoop_RunMacroAllSheets()

    Dim xSh As Worksheet
   
    Application.ScreenUpdating = False
    Application.Calculation = xlManual
   
    For Each xSh In Worksheets
        xSh.Select
        Call COPY_D1_O1_DOWN
       
    Next
   
    Application.ScreenUpdating = True
    Application.Calculation = xlAutomatic
   
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,215,404
Messages
6,124,715
Members
449,184
Latest member
COrmerod

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