简体   繁体   English

如何在所有工作表中运行VBA宏

[英]How to run VBA macros in all sheets

I am new to VBA, and I have a sheet which has VBA macros assigned to shapes. 我是VBA的新手,我有一张工作表,其中有分配给形状的VBA宏。 It works fine on the same sheet, but when I copy and paste the shapes in the next sheet or create a new shape and assign the same macro it does not work. 它可以在同一张纸上正常工作,但是当我将形状复制并粘贴到下一张纸上或创建新形状并分配相同的宏时,它将不起作用。 Kindly help me. 请帮助我。 Below is the code. 下面是代码。

ActiveSheet.Shapes("CTSnext").Fill.ForeColor.SchemeColor = Black

Thanks for your response !! 感谢您的答复 !!

However, Let me explain the issue. 但是,让我解释一下这个问题。

In Sheet 1 I have 4 shapes Named 1) Cancelling the service 2) Next 3) Not interested 4) Reset 在工作表1中,我有4个形状,分别为1)取消服务2)接下来的3)不感兴趣的4)重置

And all 3 shapes (2,3,4) will be in forecolor as white so it will be invisible. 并且所有3个形状(2,3,4)的原色都为白色,因此将不可见。 When user clicks on "Cancelling the service" "Next" will appear (Meaning the fore color will change to black). 当用户单击“取消服务”时,将出现“下一步”(意思是前颜色将变为黑色)。 And when "Next" is clicked "Not interested" will appear (Meaning the fore color will change to black). 当单击“下一步”时,将出现“不感兴趣”(意思是前颜色将变为黑色)。 And when user clicks on "Reset" both "Next" & "Not interested" will turn into invisible (Meaning the fore color will change to white). 当用户单击“重置”时,“下一步”和“不感兴趣”都将变为不可见(意味着前景色将变为白色)。 Until now everything works fine. 到目前为止,一切正常。

Now i want to have a shape in Sheet 2 which says "Reset". 现在我想在工作表2中有一个形状,上面写着“重置”。 So when user clicks on it the same function (Meaning the VBA of the "Reset" button in Sheet 1) should happen. 因此,当用户单击它时,应发生相同的功能(意味着工作表1中“重置”按钮的VBA)。

Below are the code. 下面是代码。

For Cancel ActiveSheet.Shapes("Next").Fill.ForeColor.SchemeColor = Black 取消ActiveSheet.Shapes(“ Next”)。Fill.ForeColor.SchemeColor =黑色

For Next ActiveSheet.Shapes("Notinterested").Fill.ForeColor.SchemeColor = Black 对于下一个ActiveSheet.Shapes(“ Notinterested”)。Fill.ForeColor.SchemeColor =黑色

For Reset ActiveSheet.Shapes("Next").Fill.ForeColor.SchemeColor = 1 ActiveSheet.Shapes("Notinterested").Fill.ForeColor.SchemeColor = 1 用于重置ActiveSheet.Shapes(“ Next”)。Fill.ForeColor.SchemeColor = 1 ActiveSheet.Shapes(“ Notinterested”)。Fill.ForeColor.SchemeColor = 1

I hope i made myself clear, also i don't know to how to attach the excel file to this page so it will be helpful for you all to understand my need. 我希望我已经说清楚了,我也不知道如何将excel文件附加到此页面,因此它将对您所有人了解我的需求有所帮助。

Thanks is advance. 感谢是前进。

Your line of code refers to one specific named shape (CTSnext). 您的代码行引用一种特定的命名形状(CTSnext)。 You replace that by shape number if you have only one in the sheet Shapes(0) . 如果表单Shapes(0)只有一个,则用形状号替换。
If you have several shapes on the current sheet and you want the macro to run on all shape, you can use a loop like 如果当前工作表上有多个形状,并且希望宏在所有形状上运行,则可以使用如下循环

for each shp in ActiveSheet.Shapes
   shp.Fill.ForeColor.SchemeColor = Black
Next shp

Following on from iDevlop's answer to loop through each Worksheet as well as each shape. 按照iDevlop的回答,循环遍历每个工作表以及每个形状。

Dim wks as Worksheet
For Each wks in Me.Worksheets
    For Each shp in wks.Shapes
       shp.Fill.ForeColor.SchemeColor = Black
    Next shp
Next wks

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM