我一直在尝试许多代码,但似乎都没有用。下面的代码是我找到的最接近我想要实现的代码,但是仍然存在一些问题。
我想将“合并”工作表移动到新工作簿,并将该工作簿另存为预先填充的文件名Consolidated.xlsx。我希望对话框弹出,以便用户只选择他们想要的文件夹。看起来该代码可以按预期工作,但是当您单击“保存”时,它实际上不会产生已保存的文件。
非常感谢您的帮助。
谢谢
Sub Export()
Dim pathh As Variant
ActiveWorkbook.Sheets("consolidated").Copy
pathh = Application.GetSaveAsFilename( _
FileFilter:="xlWorkbookDefault Files (*.xlsx), *.xlsx", _
Title:="Consolidated", _
InitialFileName:=filenamestring)
Application.DisplayAlerts = True
End Sub
另一种保存文件的尝试,但是在保存位置方面不显示对话框:
Application.Goto ActiveWorkbook.Sheets("consolidated").Cells(1, 1)
ActiveSheet.Copy
ActiveWorkbook.SaveAs filename:=("Consolidated"), FileFormat:=xlOpenXMLWorkbook
ActiveWorkbook.Close savechanges:=False
答案 0 :(得分:0)
您可以尝试:
Sub Export()
Dim pathh As Variant
pathh = Application.GetSaveAsFilename( _
FileFilter:="xlWorkbookDefault Files (*.xlsx), *.xlsx", _
Title:="Consolidated", _
InitialFileName:="Consolidated.xlsx")
If pathh <> False then
ActiveWorkbook.Sheets("consolidated").Copy
ActiveWorkbook.Close Filename:=pathh
End If
End Sub
答案 1 :(得分:0)
由于.SaveAs
与当前文件混乱,因此我尝试不使用它。
这或多或少是我用来创建模板文件的内容,但经过修改后可以创建常规文件。
Public Sub CreateTemplate(Sheet As Excel.Worksheet, TemplateFile As String)
Dim SaveFormat As Long, SheetsInNewWorkbook As Long
Dim oBook As Excel.Workbook
Dim FileFormat As Integer
' Delete the old file, if it exists (to avoid the possible overwrite prompt later)
On Error Resume Next
Kill (TemplateFile)
On Error GoTo 0
'Remember the user's setting
SaveFormat = Application.DefaultSaveFormat
SheetsInNewWorkbook = Application.SheetsInNewWorkbook
' Change the DefaultSaveFormat, which controls the format when creating a new workbook.
'Set the file format to the new 2007+ (.xlsx) format (with 1048576 rows), with 1 sheet
Application.DefaultSaveFormat = xlOpenXMLWorkbook '51
Application.SheetsInNewWorkbook = 1
'If you want the old 97-2003 (.xls) format (65536 rows), use
'Application.DefaultSaveFormat = xlWorkbookNormal '-4143
' Create a new Workbook
Set oBook = Application.Workbooks.Add
'Set DefaultSaveFormat & SheetsInNewWorkbook back to the user's settings
Application.DefaultSaveFormat = SaveFormat
Application.SheetsInNewWorkbook = SheetsInNewWorkbook
' Copy the sheet to the new Workbook
Sheet.Copy After:=oBook.Sheets(1)
' Make sure the sheet is Visible (since my templates are hidden sheets)
oBook.Sheets(2).Visible = True
' Supress the prompt to delete the blank Sheet(1)
Application.DisplayAlerts = False
oBook.Sheets(1).Delete
' Set the save format...
FileFormat = xlOpenXMLWorkbook '51
' For templates, use
'FileFormat = xlTemplate '17
' Save the file
oBook.SaveAs Filename:=TemplateFile, FileFormat:=FileFormat, ReadOnlyRecommended:=False, CreateBackup:=False
' Return the prompts to normal
Application.DisplayAlerts = True
' Close the Workbook, and clear the memory
oBook.Close
Set oBook = Nothing
End Sub
您可以这样简单地称呼它:
CreateTemplate ActiveSheet, pathh