简体   繁体   中英

Excel VBA: Resetting spreadsheet count

I have a excel VBA macro that dynamically generates and deletes spreadsheets based on user input. 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.

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

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. 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.

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.

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. You can also change the value manually by changing the (Name) property directly in the IDE through the properties window.

here are some ideas you can try...

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

or (assuming in this example, 'Sheet3' doesn't already exist:

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

This is more cleanup than re-setting

cheers, Micéal

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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