[英]VBA code for saving worksheet as a prenamed file in the saveas dialogue box
I have been trying a number of codes out there and none seem to work. 我一直在尝试一些代码,但似乎都没有用。 The code below is the closest I have found to what I am trying to achieve, yet something is still amiss.
下面的代码是我找到的最接近我想要实现的代码,但是仍然存在一些问题。
I want to move a sheet "consolidated" to a new workbook and save the workbook as a pre-populated file name Consolidated.xlsx. 我想将工作表“合并”移动到新工作簿,并将该工作簿另存为预填充的文件名Consolidated.xlsx。 I want the dialogue box to pop up so the user just selects the folder they want.
我希望对话框弹出,以便用户只选择他们想要的文件夹。 It appears the code works as expected, however when you click save it doesn't actually produce the saved file.
看起来该代码可以正常工作,但是当您单击“保存”时,它实际上不会产生已保存的文件。
Any help is greatly appreciated. 任何帮助是极大的赞赏。
Thank you 谢谢
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
Another attempt that saves the file, but does not show dialogue box in terms of where to save it: 另一种尝试是保存文件,但在保存位置方面不显示对话框:
Application.Goto ActiveWorkbook.Sheets("consolidated").Cells(1, 1)
ActiveSheet.Copy
ActiveWorkbook.SaveAs filename:=("Consolidated"), FileFormat:=xlOpenXMLWorkbook
ActiveWorkbook.Close savechanges:=False
You can try: 你可以试试:
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
Since the .SaveAs
messes with the current file, I try not to use it. 由于
.SaveAs
与当前文件.SaveAs
,因此我尽量不要使用它。
This is more or less what I use to create template files, but modified to create regular files. 这或多或少是我用来创建模板文件的内容,但经过修改可以创建常规文件。
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
You can just call it easy enough, like this: 您可以这样简单地称呼它:
CreateTemplate ActiveSheet, pathh
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.