嗨,我有一个Excel工作簿,其中包含六个工作表。在工作簿中,一切正常。我编写了一个宏,该宏可以帮助我备份当前工作簿而不是当前工作表。代码如下。
Sub FileSaveAs()
Dim strFolder As String
Dim i As Long
'Find the position of the period in the file name
i = InStr(ActiveWorkbook.Name, ".")
'Create a default file name by concatenating the file name without the extention _
plus the current date and time, and plus the xlsm extention
Filename = Left(ActiveWorkbook.Name, i - 1) & "_" & Format(Now, "yyyy-mm-dd_hh mm") & ".xlsm"
'Open Save As dialog to a default folder with default file name
With Application.FileDialog(msoFileDialogSaveAs)
.AllowMultiSelect = False
.FilterIndex = 2 '2 = xlsm
.InitialFileName = "Report" & Filename
.InitialView = msoFileDialogViewDetails
If .Show = -1 Then strFolder = .SelectedItems(1) Else Exit Sub
.Execute
End With
End Sub
在不丢失任何数据或格式或工作表中的设置的情况下,备份工作簿而不是当前工作表的效果很好。 我有两个问题。 1-当我单击备份按钮时,活动工作簿将关闭,备份工作簿将打开。 2-我尽力备份了当前工作表,而又不丢失任何数据或格式,但是我没有这样做,因为单击当前工作表的备份按钮时,一切都会发生(我写了另一个宏来备份当前的工作表,但是它不起作用,所以我没有在这里写它)。
我想做什么。我想做两件事。 1-当我单击备份按钮时,原始工作表保持打开状态,而备份工作表应保持关闭状态,以便我可以从同一主工作表中进行不同名称的备份。 2-如果可能,我想要一个宏,它可以帮助我备份活动工作表,而又不会丢失工作表上的任何数据或信息。
请指导我在哪里做错了。感谢每个成员。
答案 0 :(得分:1)
Try,
Sub FileSaveAs()
Dim strFolder As String
Dim i As Long
Dim Fn As String
Dim Wb As Workbook
Set Wb = ThisWorkbook
Fn = Wb.FullName
Wb.Save
'Find the position of the period in the file name
i = InStr(ActiveWorkbook.Name, ".")
'Create a default file name by concatenating the file name without the extention _
plus the current date and time, and plus the xlsm extention
Filename = Left(ActiveWorkbook.Name, i - 1) & "_" & Format(Now, "yyyy-mm-dd_hh mm") & ".xlsm"
'Open Save As dialog to a default folder with default file name
With Application.FileDialog(msoFileDialogSaveAs)
.AllowMultiSelect = False
.FilterIndex = 2 '2 = xlsm
.InitialFileName = "Report" & Filename
.InitialView = msoFileDialogViewDetails
If .Show = -1 Then strFolder = .SelectedItems(1) Else Exit Sub
.Execute
End With
Set Wb = ActiveWorkbook
Workbooks.Open (Fn)
Wb.Close (0)
End Sub