【问题标题】:Split worksheets into multiple files and skip hidden files将工作表拆分为多个文件并跳过隐藏文件
【发布时间】:2020-02-14 16:38:20
【问题描述】:

我找到了这段代码。它在 Excel 工作簿中拆分工作表,然后将每个工作表保存为自己的工作簿。

如果工作簿中有隐藏的工作表,则会失败。

代码如下:

Sub SplitWorkbook()
'Updateby20140612
Dim FileExtStr As String
Dim FileFormatNum As Long
Dim xWs As Worksheet
Dim xWb As Workbook
Dim FolderName As String
Application.ScreenUpdating = False
Set xWb = Application.ThisWorkbook
DateString = Format(Now, "yyyy-mm-dd hh-mm-ss")
FolderName = xWb.Path & "\" & xWb.Name & " " & DateString
MkDir FolderName
For Each xWs In xWb.Worksheets
    xWs.Copy
    If Val(Application.Version) < 12 Then
        FileExtStr = ".xls": FileFormatNum = -4143
    Else
        Select Case xWb.FileFormat
            Case 51:
                FileExtStr = ".xlsx": FileFormatNum = 51
            Case 52:
                If Application.ActiveWorkbook.HasVBProject Then
                    FileExtStr = ".xlsm": FileFormatNum = 52
                Else
                    FileExtStr = ".xlsx": FileFormatNum = 51
                End If
            Case 56:
                FileExtStr = ".xls": FileFormatNum = 56
            Case Else:
                FileExtStr = ".xlsb": FileFormatNum = 50
        End Select
    End If
    xFile = FolderName & "\" & Application.ActiveWorkbook.Sheets(1).Name & FileExtStr
    Application.ActiveWorkbook.SaveAs xFile, FileFormat:=FileFormatNum
    Application.ActiveWorkbook.Close False
Next
MsgBox "You can find the files in " & FolderName
Application.ScreenUpdating = True
End Sub

所以如果有人隐藏了工作表,VBA 就会失败。

目前 VBA 会将新工作簿保存为工作表名称,但如果我能获得将旧工作簿“XXX”附加到新名称“XXX_new name”前面的代码,那将是理想的。

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    原文来源:https://www.rondebruin.nl/win/s3/win007.htm

    Sub Copy_Every_Sheet_To_New_Workbook()
    'Working in 97-2013
        Dim FileExtStr As String
        Dim FileFormatNum As Long
        Dim Sourcewb As Workbook
        Dim Destwb As Workbook
        Dim sh As Worksheet
        Dim DateString As String
        Dim FolderName As String
    
        With Application
            .ScreenUpdating = False
            .EnableEvents = False
            .Calculation = xlCalculationManual
        End With
    
        'Copy every sheet from the workbook with this macro
        Set Sourcewb = ThisWorkbook
    
        'Create new folder to save the new files in
        DateString = Format(Now, "yyyy-mm-dd hh-mm-ss")
        FolderName = Sourcewb.Path & "\" & Sourcewb.Name & " " & DateString
        MkDir FolderName
    
        'Copy every visible sheet to a new workbook
        For Each sh In Sourcewb.Worksheets
    
            'If the sheet is visible then copy it to a new workbook
            If sh.Visible = -1 Then
                sh.Copy
    
                'Set Destwb to the new workbook
                Set Destwb = ActiveWorkbook
    
                'Determine the Excel version and file extension/format
                With Destwb
                    If Val(Application.Version) < 12 Then
                        'You use Excel 97-2003
                        FileExtStr = ".xls": FileFormatNum = -4143
                    Else
                        'You use Excel 2007-2013
                        If Sourcewb.Name = .Name Then
                            MsgBox "Your answer is NO in the security dialog"
                            GoTo GoToNextSheet
                        Else
                            Select Case Sourcewb.FileFormat
                            Case 51: FileExtStr = ".xlsx": FileFormatNum = 51
                            Case 52:
                                If .HasVBProject Then
                                    FileExtStr = ".xlsm": FileFormatNum = 52
                                Else
                                    FileExtStr = ".xlsx": FileFormatNum = 51
                                End If
                            Case 56: FileExtStr = ".xls": FileFormatNum = 56
                            Case Else: FileExtStr = ".xlsb": FileFormatNum = 50
                            End Select
                        End If
                    End If
                End With
    
    
                'Save the new workbook and close it
                With Destwb
                    .SaveAs FolderName _
                          & "\" & Destwb.Sheets(1).Name & FileExtStr, _
                            FileFormat:=FileFormatNum
                    .Close False
                End With
    
            End If
    GoToNextSheet:
        Next sh
    
        MsgBox "You can find the files in " & FolderName
    
        With Application
            .ScreenUpdating = True
            .EnableEvents = True
            .Calculation = xlCalculationAutomatic
        End With
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多