简体   繁体   English

Excel VBA:重置电子表格计数

[英]Excel VBA: Resetting spreadsheet count

I have a excel VBA macro that dynamically generates and deletes spreadsheets based on user input. 我有一个excel VBA宏,可以根据用户输入动态生成和删除电子表格。 However, when I open the VBA IDE, it seems that although I am naming my spreadsheets in the subs that create/delete them, the overall count is still increasing. 但是,当我打开VBA IDE时,尽管我将电子表格命名为创建/删除电子表格的子项,但总体数量仍在增加。

For example, depending on how far into execution my program is, under the "Microsoft Excel Objects" folder in my current project, the spreadsheets in the current workbook could look something like 例如,根据我的程序执行的程度,在当前项目的“ Microsoft Excel Objects”文件夹下,当前工作簿中的电子表格可能看起来像

Sheet101(Sheet3)
Sheet103(Sheet2)
Sheet104(Sheet1)

Or 要么

Sheet81(Inputs)
Sheet83(Date Adjustment Interpolation)
Sheet84(Pricing)
Sheet85(Comparison)

No matter if I delete the rest of them and add one, it still picks up where the last highest one left off. 无论我是否删除其余部分并添加其中一个,它仍然会从最后一个最高的位置继续。

I don't know how many times this macro will be run and I'd feel a lot better about putting it out there if I could reset this annoying tally on the number of spreadsheets that have ever been generated, since I don't know for sure where excel will cut me off. 我不知道该宏将被运行多少次,如果我可以对已经生成的电子表格的数量进行重新设置,我会感觉更好,因为我不知道可以肯定,excel会在哪里切断我。 Plus it's just annoying. 另外,这很烦人。

My Question: 我的问题:

I would like to know how to alter that spreadsheet number, or at least what the relevant object is for doing so. 我想知道如何更改电子表格编号,或者至少要更改相关对象。

Thanks! 谢谢!

Thanks to @dijkay s suggestion on code names, I've found some code to accomplish this. 感谢@dijkay对代码名称的建议,我发现了一些代码可以完成此任务。

ThisWorkbook.VBProject.VBComponents("Sheet1").name = "test"

Will change the code name of Sheet1 to test, so in the Excel Objects folder, it will appear as test(Sheet1) for example. 会将Sheet1的代号更改为test,因此在Excel Objects文件夹中,它将显示为test(Sheet1)。

This option, however, requires messing around with some trust/security settings in each individual excel client running the macro, which is unsuitable for my purposes, unfortunately. 但是,此选项需要在运行宏的每个单独的excel客户端中弄乱一些信任/安全设置,不幸的是,这不适合我的目的。 You can also change the value manually by changing the (Name) property directly in the IDE through the properties window. 您也可以通过在属性窗口中直接在IDE中更改(Name)属性来手动更改值。

here are some ideas you can try... 这是您可以尝试的一些想法...

Sheets(x).Name = "Sheet" & x

or (assuming in this example, 'Sheet3' doesn't already exist: 或(假设在此示例中,“ Sheet3”尚不存在:

Set Sheet3 = sheets.Add
Sheet3.name = "Sheet3"

This is more cleanup than re-setting 这比重新设置更干净

cheers, Micéal 欢呼声,Micéal

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

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