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

时间:2016-11-04 18:15:25

标签: excel vba

复制到另一个工作簿时是否可以控制工作表的顺序?我的代码列出了我希望工作表出现在新工作簿中的顺序,但“失败”工作表在选项卡顺序中始终排在最后。任何帮助将不胜感激。谢谢。

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

2 个答案:

答案 0 :(得分:0)

尝试在复制到新工作簿后对工作表进行排序:

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

答案 1 :(得分:0)

如果您的列表失败只是将“失败”工作表保留为第一个,那么只需移动它:

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