Running one macro in several sheets by a code

Reza1001

New Member
Joined
Sep 9, 2024
Messages
6
Office Version
  1. 2021
  2. 2019
Platform
  1. Windows
Hi dears

i have a file including 30 sheets . in some sheets i have one rectangle ( named "100" ) which must be hidden/unhidden and a rounded rectangle ( named "200" ) which must be colorized . i have written two macros for this matter . one of them is this :

Sub ProtBUDCOK()
'
' ProtBUDCOK Macro
'
'
ActiveSheet.Shapes.Range(Array("Rectangle: Rounded Corners 200")).Select
With Selection.ShapeRange.Fill
.Visible = msoTrue
.ForeColor.RGB = RGB(187, 170, 43)
.Transparency = 0
.Solid

End With

Range("s1").Select
ActiveSheet.Shapes.Range(Array("Rectangle 100")).Select
ActiveSheet.Shapes.Range(Array("Rectangle 100")).Visible = msoFalse
End Sub


i want to run this macro and other one by bottoms in sheet1 for sheet2,sheet5,sheet6 and sheet9 ( with codenames ) . below code doesn't work maybe because of ActiveSheet part of above macro or wrong addressing in sheet names . please help me.

Sub RunOnAllSheets()

Application.ScreenUpdating = False

For Each Worksheet In ThisWorkbook.Worksheets
Debug.Print wrkSht.Name
Sheets(Array("Sheet2", "Sheet5", "Sheet6", "Sheet9")).Select

Call ProtBUDBOK
Next
Application.ScreenUpdating = True

End Sub
 
Try...

VBA Code:
Sub ProtBUDCOK()

    Dim i As Integer, myArr As Variant, ws As Worksheet
    myArr = Array("Sheet2", "Sheet5", "Sheet6", "Sheet9")


    For Each ws In ActiveWorkbook.Worksheets
       
        For i = LBound(myArr) To UBound(myArr)
           
            If ws.CodeName = myArr(i) Then
               
               
                    With ws.Shapes("200").Fill
                        .Visible = msoTrue
                        .ForeColor.RGB = RGB(187, 170, 43)
                        .Transparency = 0
                        .Solid
                    End With
             

                ws.Shapes("100").Visible = msoTrue

            End If
       
        Next i
   
    Next ws

End Sub

Edit: done away with an unnecessary "With" statement
really appreciated . this code works perfectly . 👏
 
Upvote 0

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Hi
untested but try this update to your code & see if does what you want

VBA Code:
Sub ProtBUDCOK()
   
    Dim sh          As Variant
   
    For Each sh In Array(Sheet2, Sheet5, Sheet6, Sheet9)
       
        With sh.Shapes("Rectangle: Rounded Corners 200").Fill
           
            .Visible = msoTrue
            .ForeColor.RGB = RGB(187, 170, 43)
            .Transparency = 0
            .Solid
           
        End With
       
        sh.Shapes("Rectangle 100").Visible = msoFalse
       
    Next sh
   
End Sub

Dave
thanks a lot . works fine 🙏
 
Upvote 0

Forum statistics

Threads
1,223,883
Messages
6,175,168
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