从当前工作簿中存在的模板创建新工作簿

时间:2014-03-11 19:55:07

标签: vba excel-vba combobox userform excel

这就是我所拥有的,需要能够将ComboBox中调用的任何模板保存为单独的工作簿而不使用新的宏,但是使用内部引用保留公式时,应将其余值转换为值。

听到我尝试这样做,显然无效,因为它正在保存活动工作表,而不是选择的模板。也是因为验证,这是必要的,我被迫两次使用部分代码,有没有办法以任何不同的方式做到这一点。

我想我还需要提一下ComboBox在UserForm上。

请帮帮我

'Continue to create your invoice and check for the archive folder existance
Private Sub ContinueButton_Click()
    If cmbSheet.Value = "" Then
    MsgBox "Please select the Invoice Template from the list to continue."
    ElseIf cmbSheet.Value <> 0 Then
    Dim response
    Application.ScreenUpdating = 0
    Sheets(cmbSheet.Value).Visible = True
'Creating the directory only if it doesn't exist
    directoryPath = getDirectoryPath
    If Dir(directoryPath, vbDirectory) = "" Then
        response = MsgBox("The directory " & Settings.Range("_archiveDir").Value & " does not exist. Would you like to create it?", vbYesNo)
        If response = vbYes Then
            createDirectory directoryPath
            MsgBox "The folder has been created. " & directoryPath




            'Application.Goto Sheets(cmbSheet.Value).[a22], True
            Application.ScreenUpdating = False
        Else
            MsgBox "You need to create new folder " & Settings.Range("_archiveDir").Value & " to archive your invoices prior to creating them."
            Unload Me
        End If
        Unload Me
    ElseIf Dir(directoryPath, vbDirectory) <> directoryPath Then

        'Working in Excel 97-2007
    Dim FileExtStr As String
    Dim FileFormatNum As Long
    Dim Sourcewb As Workbook
    Dim Destwb As Workbook
    Dim TempFilePath As String
    Dim TempFileName As String
    Dim newFile As String, fName As String
    Dim sep As String
    sep = Application.PathSeparator

    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With

    Set Sourcewb = ActiveWorkbook

    'Copy the sheet to a new workbook
    ActiveSheet.Copy
    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
            'We exit the sub when your answer is NO in the security dialog that you
            'only see when you copy a sheet from a xlsm file with macro's disabled.
            If Sourcewb.Name = .Name Then
                With Application
                    .ScreenUpdating = True
                    .EnableEvents = True
                End With
                MsgBox "Your answer is NO in the security dialog"
                Exit Sub
            Else
                Select Case Sourcewb.FileFormat
                Case 51: FileExtStr = ".xlsx": FileFormatNum = 56
                Case 52:
                    If .HasVBProject Then
                        FileExtStr = ".xlsm": FileFormatNum = 56
                    Else
                        FileExtStr = ".xlsx": FileFormatNum = 56
                    End If
                Case 56: FileExtStr = ".xls": FileFormatNum = 56
                Case Else: FileExtStr = ".xlsb": FileFormatNum = 50
                End Select
            End If
        End If
    End With

    '    'If you want to change all cells in the worksheet to values, uncomment these lines.
    '    With Destwb.Sheets(1).UsedRange
    '        .Cells.Copy
    '        .Cells.PasteSpecial xlPasteValues
    '        .Cells(1).Select
    '    End With
    '    Application.CutCopyMode = False

    'Save the new workbook and close it
    fName = Range("I11").Value
     'Change the date format to whatever you'd like, but make sure it's in quotes
    newFile = fName & " " & Format$(Date, "mm-dd-yyyy") & Columns("M:N").Select
    Selection.Delete Shift:=xlToLeft
    TempFilePath = directoryPath & sep
    TempFileName = "New File"

    With Destwb
        .SaveAs TempFilePath & TempFileName, FileFormat:=FileFormatNum
        .Close SaveChanges:=False
    End With

    MsgBox "You can find the new file in " & TempFilePath

    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With


        'Application.Goto Sheets(cmbSheet.Value).[a22], True
        Application.ScreenUpdating = False
        Unload Me
    End If
    End If
End Sub

1 个答案:

答案 0 :(得分:1)

由于它是您要复制的模板表,您可能希望Sourcewb.Sheets(cmbSheet.Value).Copy而不是ActiveSheet.Copy

为避免两次运行代码,请更改此代码:

    Else
        MsgBox "You need to create new folder " & Settings.Range("_archiveDir").Value & " to archive your invoices prior to creating them."
        Unload Me
    End If
    Unload Me
ElseIf Dir(directoryPath, vbDirectory) <> directoryPath Then

改为:

    Else
        MsgBox "You need to create new folder " & Settings.Range("_archiveDir").Value & " to archive your invoices prior to creating them."
        Unload Me
    End If
End If
If Dir(directoryPath, vbDirectory) <> directoryPath Then

以下是我的更改后的整个代码

Option Explicit

'Continue to create your invoice and check for the archive folder existance
Private Sub ContinueButton_Click()

    If cmbsheet.Value = "" Then
        MsgBox "Please select the Invoice Template from the list to continue."
    ElseIf cmbsheet.Value <> 0 Then
        Dim response
        Application.ScreenUpdating = 0
        'Creating the directory only if it doesn't exist
        directoryPath = getDirectoryPath
        If Dir(directoryPath, vbDirectory) = "" Then
            response = MsgBox("The directory " & Settings.Range("_archiveDir").Value & " does not exist. Would you like to create it?", vbYesNo)
            If response = vbYes Then
                createDirectory directoryPath
                MsgBox "The folder has been created. " & directoryPath

                'Application.Goto Sheets(cmbSheet.Value).[a22], True
                Application.ScreenUpdating = False
            Else
                MsgBox "You need to create new folder " & Settings.Range("_archiveDir").Value & " to archive your invoices prior to creating them."
                'Unload Me
                GoTo THE_END
            End If
        End If
        If Dir(directoryPath, vbDirectory) <> directoryPath Then
            Sheets(cmbsheet.Value).Visible = True

                'Working in Excel 97-2007
            Dim FileExtStr As String
            Dim FileFormatNum As Long
            Dim Sourcewb As Workbook
            Dim Destwb As Workbook
            Dim TempFilePath As String
            Dim TempFileName As String
            Dim newFile As String, fName As String
            Dim sep As String
            sep = Application.PathSeparator

            With Application
                .ScreenUpdating = False
                .EnableEvents = False
            End With

            Set Sourcewb = ActiveWorkbook

            'Copy the sheet to a new workbook
            Sourcewb.Sheets(cmbsheet.Value).Copy
            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
                    'We exit the sub when your answer is NO in the security dialog that you
                    'only see when you copy a sheet from a xlsm file with macro's disabled.
                    If Sourcewb.Name = .Name Then
                        'With Application
                        '    .ScreenUpdating = True
                        '    .EnableEvents = True
                        'End With
                        MsgBox "Your answer is NO in the security dialog"
                        'Exit Sub
                        GoTo THE_END
                    Else
                        Select Case Sourcewb.FileFormat
                        Case 51: FileExtStr = ".xlsx": FileFormatNum = 56
                        Case 52:
                            If .HasVBProject Then
                                FileExtStr = ".xlsm": FileFormatNum = 56
                            Else
                                FileExtStr = ".xlsx": FileFormatNum = 56
                            End If
                        Case 56: FileExtStr = ".xls": FileFormatNum = 56
                        Case Else: FileExtStr = ".xlsb": FileFormatNum = 50
                        End Select
                    End If
                End If
            End With

            'If you want to change all cells in the worksheet to values, uncomment these lines.
            'With Destwb.Sheets(1).UsedRange
            With Sourcewb.Sheets(cmbsheet.Value).UsedRange
                .Cells.Copy
                .Cells.PasteSpecial xlPasteValues
                .Cells(1).Select
            End With
            Application.CutCopyMode = False

            'Save the new workbook and close it
            fName = Range("I11").Value
             'Change the date format to whatever you'd like, but make sure it's in quotes
            newFile = fName & " " & Format$(Date, "mm-dd-yyyy") & Columns("M:N").Select
            Selection.Delete Shift:=xlToLeft
            TempFilePath = directoryPath & sep
            TempFileName = "New File"

            With Destwb
                .SaveAs TempFilePath & TempFileName, FileFormat:=FileFormatNum
                .Close SaveChanges:=False
            End With

            MsgBox "You can find the new file in " & TempFilePath

            'With Application
            '    .ScreenUpdating = True
            '    .EnableEvents = True
            'End With


            'Application.Goto Sheets(cmbSheet.Value).[a22], True
            'Application.ScreenUpdating = False
            'Unload Me
        End If
    End If

THE_END:

    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With
    Unload Me


End Sub