[英]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.