保存几个隐藏的工作表

时间:2017-10-11 14:03:51

标签: excel vba excel-vba

我找到并调整了将所有工作表(在1个Excel文件中)保存到特定文件夹中的工作簿中的代码,然后从现有工作簿中删除工作表,最后在1个工作簿中代替12个工作表我有12个工作簿和1个工作簿工作表在1个文件夹中。

我想保存4个隐藏的工作表和宏,所以最后我希望有12个工作簿,其中包含1个可见工作表(称为Sheet1),4个隐藏工作表(称为:查找,复制数据,导出数据,预览)和宏,所以最后我将有12个工作簿(.xlsm)与5个工作表和启用宏。可见工作表的数量各不相同,但隐藏的工作表总是相同的。

我的代码:

Sub sheets_to_files(Optional byDummy As Byte)

Dim xPath As String
Dim ws As Worksheet
xPath = Application.ActiveWorkbook.path
Application.ScreenUpdating = False
Application.DisplayAlerts = False
For Each xWs In ThisWorkbook.Sheets
  xWs.Copy
  Application.ActiveWorkbook.SaveAs FileName:=xPath & "\" & xWs.Name & 
  ".xlsm", FileFormat:=52
Application.ActiveWorkbook.Close False
Next

For Each ws In Worksheets
    Select Case ws.Name
        Case "Sheet1"
        Case Else
            ws.Delete
    End Select
Next ws
Application.DisplayAlerts = True
End Sub

我相信我需要创建一个数组并更改这些行

For Each xWs In ThisWorkbook.Sheets
    xWs.Copy

1 个答案:

答案 0 :(得分:0)

通常,您可以使用简单的表格(" NAME")。移动以将表格放入新工作簿。

Dim i As Long, j As String, k As Workbook, xPath As String
xPath = Application.ActiveWorkbook.path
Set k = ThisWorkbook
If k.Sheets.Count > 1 Then 'Check
    For i = k.Sheets.Count To 2 Step -1
        j = k.Sheets(i).Name
        k.Sheets(i).Move
        'ActiveWorkbook.Sheets.Add.Name = "Test" 'Lookups, Copy Data, Export Data, Preview
        'ActiveWorkbook.Sheets("Test").Visible = xlSheetHidden
        ActiveWorkbook.SaveAs FileName:=xPath & "\" & j & ".xlsm", FileFormat:=52
        ActiveWorkbook.Close
    Next i
End If