【问题标题】:Naming the destination workbooks same as a specific cell in the destination workbook将目标工作簿命名为与目标工作簿中的特定单元格相同
【发布时间】:2020-06-23 16:36:43
【问题描述】:

我从一个网站获得了这个代码,它工作正常并且完成了我需要的工作,即从一个活动工作簿中提取每个工作表到一个文件夹,并将工作簿命名为与其来自的工作表相同的名称。

我只需要稍作调整即可将目标工作簿命名为与它来自的工作表中的特定单元格相同。

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

            'Change all cells in the worksheet to values if you want
            If Destwb.Sheets(1).ProtectContents = False Then
                With Destwb.Sheets(1).UsedRange
                    .Cells.Copy
                    .Cells.PasteSpecial xlPasteValues
                    .Cells(1).Select
                End With
                Application.CutCopyMode = False
            End If


            '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

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    您只需更改保存工作簿的方式。您可以创建一个变量,并在保存之前将名称存储在其中。即我在下面的代码中使用sFileName,并假设名称存储在 Range("A1") 中,您可以根据需要对其进行更改。

    ub 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
        Dim sFileName 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
    
                'Change all cells in the worksheet to values if you want
                If Destwb.Sheets(1).ProtectContents = False Then
                    With Destwb.Sheets(1).UsedRange
                        .Cells.Copy
                        .Cells.PasteSpecial xlPasteValues
                        .Cells(1).Select
                    End With
                    Application.CutCopyMode = False
                End If
    
    
                'Save the new workbook and close it
                'get name of workbook
                sFileName = sh.Range("A1").Value
    
                With Destwb
                    .SaveAs FolderName _
                          & "\" & sFileName & 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
    

    【讨论】:

    • 啊,这行得通!正是我想要的。非常感谢@Dean。
    【解决方案2】:

    您可以修改并使用以下代码:

    Sub test()
    
        Dim wbName As String, wbPath As String, wbType As String, TestStr As String
        Dim wb As Workbook
        
        'The name is in cell A1, Sheet1 of this workbook
        wbName = ThisWorkbook.Worksheets("Sheet1").Range("A1").Value
        'The path is a Test folder on the desktop
        wbPath = "C:\Users\mario\Desktop\Test"
        'wbType is .xlsx
        wbType = ".xlsx"
        
        'Before create a workbook check if already exist
        TestStr = Dir(wbPath & "\" & wbName & wbType)
        
        If TestStr = "" Then
            'Create and save the folder
            Set wb = Workbooks.Add
        
            wb.SaveAs Filename:=wbPath & "\" & wbName & wbType
            'Close the new workbook
            wb.Close
        Else
            MsgBox "Workbook already exist!"
        End If
        
    End Sub
    

    【讨论】:

    • 感谢@Error1004 的帮助。但我不想使用 ThisWorkbook.Worksheets("Sheet1").Range 因为工作簿可能不包含 Sheet1。我应该使用 ThisWorkbook.Sheets(1).Range 吗?
    • 另外,我可以在现有代码中使用您的代码行吗?
    • no 只是一个例子。你可以将任何你想要的值传递给wbName
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-29
    • 2013-01-16
    相关资源
    最近更新 更多