excel - 如何将除工作表 1 之外的每个工作表另存为单独的工作簿?

标签 excel vba

我有一个包含 5 个工作表的 Excel 工作簿,但我只想将工作表 2 保存到工作表 - 5,但我不想保存工作表 1。我想将其排除在保存之外。我该怎么做?

我尝试了一些代码,但遇到了困难。

Sub SaveShtsAsBook()

    Dim xcsvFile As String
    Dim datestring As String
    Dim Count As Integer


    datestring = DateValue(Now) & Time
    datestring = Replace(datestring, "/", "_")
    datestring = Replace(datestring, ":", "_")
    datestring = Replace(datestring, " ", "_")

'    Application.WindowState = xlMinimized
'    Application.Visible = False

    Application.EnableEvents = True
   ' Application.Calculation = xlCalculationManual
  '  Application.Wait (Now + TimeValue("0:00:10"))

    For Count = 1 To 3000
    DoEvents
    Next Count


    'For Each Sheet In Worksheets
    For Each Sheet In ThisWorkbook.Worksheets ' Safer way to qualify the worksheets with the workbook where this code lies

       Select Case Sheet.Name
        Case "Sheet1"
        ' do nothing

        Case Else
           xcsvFile = "E:\" & xWs.Name & "_" & datestring & ".csv"
       ' xcsvFile = "E:\" & "\" & xWs.Name & ".csv" 'compare mine to yours to see issues

        xWs.Copy

        Dim newSheet As Workbook 'setting copied sheet to workbook variable for easier coding
        Set newSheet = ActiveSheet.Parent 'parent of worksheet is workbook

        newSheet.SaveAs Filename:=xcsvFile, FileFormat:=xlCSV, CreateBackup:=False
        newSheet.Close False
       End Select
    Next
End Sub

最佳答案

问题是您引用的 xWs 变量在您的代码中不存在。如果您使用 Sheet 更改它,它会完美地工作,正如我在 Excel 上测试的那样:

Sub SaveShtsAsBook()
    Dim xcsvFile As String
    Dim datestring As String
    Dim Count As Integer

    datestring = DateValue(Now) & Time
    datestring = Replace(datestring, "/", "_")
    datestring = Replace(datestring, ":", "_")
    datestring = Replace(datestring, " ", "_")

'    Application.WindowState = xlMinimized
'    Application.Visible = False

    Application.EnableEvents = True
   ' Application.Calculation = xlCalculationManual
  '  Application.Wait (Now + TimeValue("0:00:10"))

    For Count = 1 To 3000
        DoEvents
    Next Count


    'For Each Sheet In Worksheets
    For Each Sheet In ThisWorkbook.Worksheets ' Safer way to qualify the worksheets with the workbook where this code lies

        Select Case Sheet.Name
            Case "Sheet1"
            ' do nothing

            Case Else
            xcsvFile = "E:\" & Sheet.Name & "_" & datestring & ".csv"
            ' xcsvFile = "E:\" & "\" & xWs.Name & ".csv" 'compare mine to yours to see issues

            Sheet.Copy

            Dim newSheet As Workbook 'setting copied sheet to workbook variable for easier coding
            Set newSheet = ActiveSheet.Parent 'parent of worksheet is workbook

            newSheet.SaveAs Filename:=xcsvFile, FileFormat:=xlCSV, CreateBackup:=False
            newSheet.Close False
        End Select
    Next
End Sub

希望这有帮助。

关于excel - 如何将除工作表 1 之外的每个工作表另存为单独的工作簿?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57791285/

相关文章:

Excel VLOOKUP(两个值之和,数组(值 1 + 2 之和)

excel - 在 Excel 中复制 Google 电子表格数组公式

sql-server - 在 ssis 脚本任务中格式化 excel 目标列

xml - 如何将多个 XML 中的值提取到 Excel 中?

excel - 根据表格标题自动在单元格中输入日期/时间戳的代码

excel - for next 循环无法为变量分配值 - VBA

Excel PageSetup 多个工作表

VBA Kerberos 身份验证

vba - 将 Excel UDF 放入工作表对象中(而不是放在模块中)

excel - 从 Access 将 Excel 窗口置于前台