vba 在 Excel 中将工作表添加到工作簿的末尾(正常方法不起作用?)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11456157/
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
Adding sheets to end of workbook in Excel (normal method not working?)
提问by Jacxel
This is the VBA code im using to try add a new sheet to the last place in the workbook
这是我用来尝试将新工作表添加到工作簿中的最后一个位置的 VBA 代码
mainWB.Sheets.Add(After:=Sheets(Sheets.Count)).Name = new_sheet_name
I saw this in a similar question on this site. Its not working.
我在这个网站上的一个类似问题中看到了这一点。它不工作。
I do this in a loop and each sheet gets added to the second position in the sheets. There are 2 sheets that are permanently there (info and summary) and I then precede to add 5 more called "test" 1 through 5. I always end up with the sheets in this order:
我循环执行此操作,每张纸都被添加到床单中的第二个位置。有 2 张永久保存在那里(信息和摘要),然后我再添加 5 个称为“测试”1 到 5 的工作表。我总是按以下顺序得到这些工作表:
Info, sheet5, sheet4, sheet3, sheet2, sheet1, Summary
But what I want/was expecting was:
但我想要/期待的是:
Info, Summary, sheet1, sheet2, sheet3, sheet4, sheet5
(the loop does produce them in the expected order so the problem isn't there.)
(循环确实以预期的顺序产生它们,所以问题不存在。)
If I swap the summary and info sheets before I start then they are in the opposite places when I'm done.
如果我在开始之前交换摘要和信息表,那么当我完成时它们会在相反的位置。
What am I doing wrong?
我究竟做错了什么?
回答by Siddharth Rout
Try this
尝试这个
mainWB.Sheets.Add(After:=mainWB.Sheets(mainWB.Sheets.Count)).Name = new_sheet_name
回答by Tim Williams
mainWB.Sheets.Add(After:=Sheets(Sheets.Count)).Name = new_sheet_name
should probably be
应该是
mainWB.Sheets.Add(After:=mainWB.Sheets(mainWB.Sheets.Count)).Name = new_sheet_name
回答by CodeKid
A common mistake is
一个常见的错误是
mainWB.Sheets.Add(After:=Sheets.Count)
which leads to Error 1004. Although it is not clear at all from the official documentation, it turns out that the 'After' parameter cannot be an integer, it must be a reference to a sheet in the same workbook.
这会导致错误 1004。虽然官方文档中根本没有明确说明,但事实证明 'After' 参数不能是整数,它必须是对同一工作簿中工作表的引用。
回答by danielpiestrak
Be sure to fully qualify your sheets with which workbook they are referencing!
请务必使用它们所引用的工作簿完全限定您的工作表!
mainWB.Sheets.Add(After:=mainWB.Sheets(mainWB.Sheets.Count)).Name = new_sheet_name