Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 如何备份当前工作表或工作簿_Excel_Vba - Fatal编程技术网

Excel 如何备份当前工作表或工作簿

Excel 如何备份当前工作表或工作簿,excel,vba,Excel,Vba,我有一个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, ".") 'Cre

我有一个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-如果可能,我需要一个宏,它可以帮助我备份活动工作表,而不会丢失工作表上的任何数据或信息

请指引我哪里做错了。谢谢每一位会员

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