2015-09-30 18 views
2

我有這個代碼,創建一個文件夾和一個實際的文件保存在它,但我希望它只保存一份副本只有一張它。這樣代碼文件就像一個模板...創建文件夾和新的* .xlsx文件與模板,如xlsm文件中的宏

你寫你的東西,然後按下按鈕,它會在一個新的創建文件夾中保存一個.xlsx文件(一張表格)。 。所以你可以用數百個文件夾來做到這一點。

那麼到底應該像這樣工作:

  1. 您打開.XLSM文件,其中下面的代碼是
  2. 你得張一種形式(什麼應該被「出口」後。上)和 一個列表,你在表格中複製東西。
  3. 填寫完表格並按下按鈕後,它會將表格 工作表保存爲新文件夾中的.xlsx,您可以繼續使用.xlsm 文件。

如果你不清楚,請詢問。

的代碼,我現在在這裏

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去...

這是一個有點出我knownledge的請幫我傢伙:)迎接

現在我我使用下面的代碼 問題是,它總是關閉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 
+0

呦可以用一個XLSM文件多達XLSX文件創建,如你所願。然而,將宏保存在新的XLSX中是不可能的。 – Balinti

+0

@Balinti:我沒有看到Swi要求解決方案,將宏保存在XLSX文件中。但是我對他/她想要的東西也有點朦朧。標題是'.xls',它表示應該創建舊的Excel文件格式。同時代碼建議將新工作簿保存爲'.xlsm'。那麼,現在是什麼? – Ralph

+0

@Balinti你有什麼想法我可以阻止它從關閉本身? – Swi

回答

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 
+0

這是做什麼用的? 'SheetToExport = Range(「A1」)。Value'或者指定UserForm輸出' – Swi

+0

首先非常感謝你,我很欣賞這一點。 它幾乎完美的工作,我有的問題是,它導出所有工作表,它只應導出一張表,表格/模板..如何:0 – Swi

+0

我不希望它總是關閉文件.. ..我怎麼能阻止呢? 當我重新打開'.xlsm'文件時,如果我想更新,每次都會問我,我該如何阻止它? – Swi

相關問題