Running Macro on all sheets that meet cell criteria

heyfraank

New Member
Joined
Oct 29, 2021
Messages
3
Office Version
  1. 2019
Platform
  1. MacOS
Hi, I'm trying to make a macro run on every sheet in my workbook, problem is macro doesn't work if cell value is 0:

VBA Code:
Sub Ola()
    Dim Rng As Range
    Dim InputRng As Range, OutRng As Range
    xTitleId = "KutoolsforExcel"
    Set InputRng = Application.Selection
    Set InputRng = Range("A1:B1")
    Set OutRng = Range("B3")
    Set OutRng = OutRng.Range("A1")
    For Each Rng In InputRng.Rows
    xValue = Rng.Range("A1").Value
    xNum = Rng.Range("B1").Value
    OutRng.Resize(xNum, 1).Value = xValue
    Set OutRng = OutRng.Offset(xNum, 0)
Next
End Sub

I tried to loop it using:

VBA Code:
Sub manekankit()
   Dim Ws As Worksheet
   
   For Each Ws In ActiveWorkbook.Worksheets
      If Ws.Range("B1").Value > 0 Then
         Ola
      End If
   Next Ws
End Sub

But it only runs on the active worksheet, not on the whole workbook, so I've got two problems:

1) Every sheet has a Name on B1 and Number of Payments on cell B1, I only need to run the macro on sheets that have a number of payments greater than 0
2) First sheet named "ACUMULADOS" has a summary table of every payment, so I also need to exclude the macro from running on this sheet.

Thanks, any help is appreciated.
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Welcome to the Board!

Try this:
VBA Code:
Sub manekankit()
   Dim Ws As Worksheet
   
   For Each Ws In ActiveWorkbook.Worksheets
      If (Ws.Range("B1").Value > 0) And (Ws.Name <> "ACUMULADOS") Then
         Ws.Activate
         Ola
      End If
   Next Ws
End Sub
 
Upvote 0
Solution
Welcome to the Board!

Try this:
VBA Code:
Sub manekankit()
   Dim Ws As Worksheet
  
   For Each Ws In ActiveWorkbook.Worksheets
      If (Ws.Range("B1").Value > 0) And (Ws.Name <> "ACUMULADOS") Then
         Ws.Activate
         Ola
      End If
   Next Ws
End Sub
Thank you!, works like a charm, very helpful.
 
Upvote 0
You are welcome!

The important thing to note that when looping through worksheets, each worksheet is not actually selected.
So whenever your condition was met, your code would run against the sheet that was activate at the top you called the macro to run (so it literally would be running the code over-and-over on the same sheet).

The way to get around that is to use sheet references in your range references in your OLA code, or simply select the sheet before calling it to make that the active sheet (which is what we did here).
 
Upvote 0

Forum statistics

Threads
1,223,880
Messages
6,175,157
Members
452,615
Latest member
bogeys2birdies

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