vba 如何快速确定 Excel 工作簿中哪些工作表具有宏?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10140030/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 12:56:05  来源:igfitidea点击:

How to determine quickly which sheets have macros in Excel Workbook?

excelexcel-vbavba

提问by Alekzander

If an Excel Workbook has many sheets and some of them have macros inside, the only way I have found to determine which sheets have macro is by clicking each of them in Project Explorer (Ctrl+Rin VBA).

如果 Excel 工作簿有很多工作表,其中一些工作表内部有宏,我发现确定哪些工作表有宏的唯一方法是在项目资源管理器中单击每个工作表(VBA 中的Ctrl+R)。

Is there any other way to do this?

有没有其他方法可以做到这一点?

回答by Siddharth Rout

I answered a question recently in MSDN and ultimately wrote a blog post on it.

我最近在 MSDN 上回答了一个问题,并最终写了一篇关于它的博客文章。

Topic: Check if an Excel File has a Macro

主题:检查 Excel 文件是否有宏

Link: http://www.siddharthrout.com/2012/04/12/check-if-an-excel-file-has-a-macro/

链接http: //www.siddharthrout.com/2012/04/12/check-if-an-excel-file-has-a-macro/

You can then use .VBComponents.Item(i).Namewith .VBComponents.Item(i).Typeto check which "Sheets" have "macros".

然后您可以使用.VBComponents.Item(i).Namewith.VBComponents.Item(i).Type检查哪些“ Sheets”具有“ macros”。

EDIT

编辑

Technically speaking every macrois a piece of codebut it's not necessary that every piece of codeis a macro. So if you are just checking for macros then see the 1st part of the blog post and if you are checking for any code then look at the 2nd part of the blog post.

从技术上讲,每个都是一段代码,但没有必要每段代码都是宏。因此,如果您只是检查宏,请查看博文的第一部分,如果您正在检查任何代码,请查看博文的第二部分。

回答by assylias

You can loop over the sheets and use the following syntax:

您可以遍历工作表并使用以下语法:

If ActiveWorkbook.VBProject.VBComponents(sheetName).CodeModule.CountOfLines <> 0 Then

sheetNamebeing the name of the sheet.

sheetName是工作表的名称。

For more advanced use, you should check this pagewhich gives more advanced examples.

对于更高级的使用,您应该查看此页面,其中提供了更高级的示例。