nnsea 发表于 2019-10-19 16:27:44

批量将工作表转为工作簿VBA


Sub Newbooks()

    Dim sht As Worksheet, mypath$
    With Application.FileDialog(msoFileDialogFolderPicker)
   '选择保存工作薄的文件路径
      .AllowMultiSelect = False
      '不允许多选
      If .Show Then
            mypath = .SelectedItems(1)
      Else
            Exit Sub
            '如果没有选择保存路径,则退出程序
      End If
    End With
    If Right(mypath, 1) <> "\" Then mypath = mypath & "\"
    Application.DisplayAlerts = False
    '取消显示系统警告消息,避免重名工作簿无法保存。当有重名工作簿时,会直接覆盖保存。
    Application.ScreenUpdating = False
    '取消屏幕刷新
    For Each sht In Worksheets
    '遍历工作表
      sht.Copy
      '复制工作表,工作表单纯复制后,会成为活动工作薄
      With ActiveWorkbook
            .SaveAs mypath & sht.Name, xlWorkbookDefault
            '保存活动工作薄到指定路径下,以默认文件格式
            .Close True '关闭工作薄并保存
      End With
    Next
    MsgBox "处理完成。", , "提醒"
    Application.ScreenUpdating = True '恢复屏幕刷新
    Application.DisplayAlerts = True '恢复显示系统警告消息
End Sub
页: [1]
查看完整版本: 批量将工作表转为工作簿VBA