使用xlsm文件

时间:2015-09-30 08:13:44

标签: excel vba excel-vba mkdir

我有这个代码,创建一个文件夹并将实际文件保存在其中,但我希望它只保存一个只包含一个工作表的副本。这样带有代码的文件就像一个模板...

你编写你的东西并按下按钮,它会在新创建的文件夹中保存带有一张(带有表格的表格)的.xlsx文件...所以你可以用数百个文件作为文件夹。

所以最后它应该像这样工作:

  1. 您打开以下代码所在的.xlsm文件。
  2. 你需要填写一张FORM(以后应该“导出”)和 您在表单中复制内容的列表。
  3. 当您填写表格并按下按钮并保存表格时 将新文件夹中的工作表设为.xlsx,然后您可以继续使用.xlsm 文件。
  4. 如果您不清楚,请询问。

    我现在的代码

    Sub Macro1()
    Dim strFilename, strDirname, strPathname, strDefpath As String
    On Error Resume Next ' If directory exist goto next line
    strDirname = Range("D81").Value ' New directory name
    
    strFilename = Range("D8").Value 'New file name
    strDefpath = Application.ActiveWorkbook.Path 'Default path name
    If IsEmpty(strDirname) Then Exit Sub
    If IsEmpty(strFilename) Then Exit Sub
    
    MkDir strDefpath & "\" & strDirname
    strPathname = strDefpath & "\" & strDirname & "\" & strFilename 'create total string
    
    ActiveWorkbook.SaveAs Filename:=strPathname & ".xlsm", _
    FileFormat:=xlOpenXMLWorkbookMacroEnabled, Password:="", WriteResPassword:="", _
    ReadOnlyRecommended:=False, CreateBackup:=False
    End Sub
    

    这里的问题是我有像1102,1103这样的表格的名称。下一步是有名称为1102_1和1102_2的文件,它们都应该进入文件夹1102 ......

    这有点出于我的知识,请帮助我们:)欢迎光临

    现在我在下面使用此代码 问题是它总是关闭xlsm文件真正令人烦恼的事情,当我重新打开它时,它想要更新我需要删除的文件,但我不知道如何:/ ...它只应该导出/保存一个特殊表

    Private Sub CommandButton1_Click()
    Dim strFilename As String, _
    strDirname As String, _
    strPathname As String, _
    strDefpath As String, _
    SheetToExport As String, _
    WbMaster As Workbook, _
    WbCopy As Workbook
    
    
    On Error Resume Next ' If directory exist goto next line
    strDirname = Range("W12").Value ' New directory name
    strFilename = Range("D8").Value 'New file name
    
    Set WbMaster = Application.ActiveWorkbook
    SheetToExport = Range("A1").Value 'Or specify UserForm output
    
    strDefpath = WbMaster.Path 'Default path name
    
    If IsEmpty(strDirname) Then Exit Sub
    If IsEmpty(strFilename) Then Exit Sub
    
    MkDir strDefpath & "\" & strDirname
    strPathname = strDefpath & "\" & strDirname & "\" & strFilename 'create total string
    
    WbMaster.Sheets(SheetToExport).Copy
    Set WbCopy = Application.ActiveWorkbook
    
    WbCopy.SaveAs Filename:=strPathname & ".xlsx", _
                    FileFormat:=xlOpenXMLWorkbook, Password:="", WriteResPassword:="", _
                    ReadOnlyRecommended:=False, CreateBackup:=False
    
    WbCopy.Close
    End Sub
    

1 个答案:

答案 0 :(得分:2)

小心你的变量声明!

你在OP(原帖)中的表现方式:

strFilenamestrDirnamestrPathname被声明为Variant,而不是String

您仍然可以使用它们但是它会占用更多内存,如果您将它们用作参数,则会出现问题。

参见代码:

Dim strFilename As String, _
    strDirname As String, _
    strPathname As String, _
    strDefpath As String, _
    SheetToExport As String, _
    WbMaster As Workbook, _
    WbCopy As Workbook


On Error Resume Next ' If directory exist goto next line
strDirname = Range("D81").Value ' New directory name
strFilename = Range("D8").Value 'New file name

Set WbMaster = Application.ActiveWorkbook
SheetToExport = Range("A1").Value 'Or specify UserForm output

strDefpath = WbMaster.Path 'Default path name

If IsEmpty(strDirname) Then Exit Sub
If IsEmpty(strFilename) Then Exit Sub

MkDir strDefpath & "\" & strDirname
strPathname = strDefpath & "\" & strDirname & "\" & strFilename 'create total string

WbMaster.Sheets(SheetToExport).Copy
Set WbCopy = Application.ActiveWorkbook

WbCopy.SaveAs Filename:=strPathname & ".xlsx", _
                FileFormat:=xlOpenXMLWorkbook, Password:="", WriteResPassword:="", _
                ReadOnlyRecommended:=False, CreateBackup:=False

ClosingWb = MsgBox("Do you wish to close the exported file?",vbYesNo,"Close exported file")
If ClosingWb <> vbNo Then WbCopy.Close