简体   繁体   English

EXCEL复制到新工作簿时设置工作表顺序

[英]EXCEL Set sheet order when copying to a new workbook

Is it possible to control the order of worksheets when copying to another workbook? 复制到另一个工作簿时是否可以控制工作表的顺序? My code lists the order in which I'd like the sheets to appear in the new workbook, but the "Fail" sheet keeps ending up last in the tab order. 我的代码列出了我希望工作表出现在新工作簿中的顺序,但“失败”工作表在选项卡顺序中始终排在最后。 Any help would be greatly appreciated. 任何帮助将不胜感激。 Thank you. 谢谢。

Set wb = ThisWorkbook
wb.Save
wb.Sheets(Array("Fail", "Fail Screenshot", "Fail Screenshot2", "Fail Screenshot3", "Fail Screenshot4")).Copy
Set tempWB = ActiveWorkbook
fileName = "C:\Users\ME\Desktop\TEST\" & Sheets("Fail").Range("M7").Value & ".xlsx"
ActiveWorkbook.SaveAs fileName:=fileName, FileFormat:=xlOpenXMLWorkbook, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False, CreateBackup:=False
If Len(Dir(tempFile)) <> 0 Then
Kill tempFile

Try to sort your sheets after copying to new workbook: 尝试在复制到新工作簿后对工作表进行排序:

Sub sheetorders()
 Dim m, arr
 Set wb = ThisWorkbook
 arr = Array("Fail", "Fail Screenshot", "Fail Screenshot2", "Fail Screenshot3", "Fail Screenshot4")
 wb.Save
 wb.Sheets(arr).Copy
 For Each m In arr
    Sheets(m).Move After:=Sheets(Sheets.Count)
 Next m
 Set tempWB = ActiveWorkbook
 fileName = "C:\Users\ME\Desktop\TEST\" & Sheets("Fail").Range("M7").Value & ".xlsx"
 ActiveWorkbook.SaveAs fileName:=fileName, FileFormat:=xlOpenXMLWorkbook, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False, CreateBackup:=False
 If Len(Dir(tempFile)) <> 0 Then
 Kill tempFile
End Sub

If your list fails only to keep "Fail" worksheet as the first one then just move it: 如果您的列表失败只是将“失败”工作表保留为第一个,那么只需移动它:

wb.Sheets(Array("Fail", "Fail Screenshot", "Fail Screenshot2", "Fail Screenshot3", "Fail Screenshot4")).Copy
Sheets("Fail").Move Before:=Sheets(1)

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

相关问题 将工作表复制到Excel中的新工作簿时,再次打开新文件时会出错 - Copying a sheet to a new workbook in Excel create a error when opening the new file again Excel VBA 将指定的一组工作表复制到新工作簿/从副本中排除工作表 - Excel VBA copying specified set of worksheets to new workbook/excluding sheet from copy 有没有办法导出和Excel工作表而不复制到工作簿? - Is there a way to export and Excel sheet without copying to a workbook? 向 excel 工作簿添加新工作表 - Add a new sheet to excel workbook 将动态单元格/行复制到新工作表或工作簿中 - Copying Dynamic Cells/Rows Into New Sheet or Workbook 将列复制到VBA中的新工作簿表 - Copying a column to a new workbook sheet in VBA 使用工作表范围将范围从Excel工作簿复制到另一个工作簿 - copying a range from an Excel workbook to another workbook using sheet range 将Nth个Excel工作簿中的Xth工作表复制到一个新的工作簿中,使用哪种语言? - Copying Xth sheet from N Excel workbooks into a new workbook-which language to use? 在不复制工作表的情况下从另一个工作簿引用 Excel 工作表 - Reference an excel sheet from another workbook without copying the sheet Excel VBA将工作表复制到新工作簿并在Outlook中通过电子邮件发送新工作簿 - Excel VBA Copying Sheets to a New Workbook and emailing the New Workbook in Outlook
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM