在Excel中将工作表添加到工作簿的末尾(普通方法不工作?) [英] Adding sheets to end of workbook in Excel (normal method not working?)
问题描述
这是VBA代码,用于尝试将新的工作表添加到工作簿中的最后一个位置。
This is the VBA code im using to try add a new sheet to the last place in the workbook
mainWB.Sheets.Add(After:=Sheets(Sheets.Count)).Name = new_sheet_name
这个网站上有一个类似的问题。它不工作。
I saw this in a similar question on this site. Its not working.
我在一个循环中做到这一点,每张表都添加到工作表中的第二个位置。有永久的2张(信息和摘要),然后我再添加5个更多的测试1到5.我总是以这个顺序结束表:
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:
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.
我做错了什么?
推荐答案
p>
Try this
mainWB.Sheets.Add(After:=mainWB.Sheets(mainWB.Sheets.Count)).Name = new_sheet_name
这篇关于在Excel中将工作表添加到工作簿的末尾(普通方法不工作?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!