如何在不更改原始文件的情况下保存经过编辑的新 excel 文件?

How to save a new excel file with edits without changing original?

在 excel 的某些早期版本中,我曾经能够在工作簿(称为工作簿 A)中进行一些编辑,选择“另存为”,重命名文件(称为工作簿 B),以及何时完成后,我可以打开我的原始工作簿 A,发现它处于预编辑状态,而工作簿 B 已保存编辑。作为参考,我对 Microsoft 365 使用 Excel。

无论如何,这就是我试图用下面的代码做的事情,我 运行 遇到了一个问题。它使用与工作簿 B 中完全相同的编辑保存工作簿 A,而我想保持工作簿 A 不变。

Sub NoticeGenerator()
    Dim wxhS As Worksheet, wbkT As Workbook
    Const TABCOLOR As Long = 192 'Standard Tab color Dark Red
    
 
 'Hides any tabs that are Dark Red
   For Each wxhS In Application.ActiveWorkbook.Worksheets
        If wxhS.Tab.Color <> TABCOLOR Then
            wxhS.Visible = xlSheetHidden
            
        End If
        If wxhS.Tab.Color = TABCOLOR Then
            wxhS.Cells.Font.Color = RGB(0, 0, 0)
            
        End If
    
    Next      
  
'Displaying the saveas dialog box
FName = Application.GetSaveAsFilename("Notice Generator v", _
    "Excel files,*.xlsm", 1, "Select your folder and filename")

'Saves file if filename is entered, otherwise it won't save
If FName <> False Then
ActiveWorkbook.SaveAs Filename:=FName, FileFormat:=xlOpenXMLWorkbookMacroEnabled
     End If


 For Each wxhS In Application.ActiveWorkbook.Worksheets
        If wxhS.Tab.Color = TABCOLOR Then
            wxhS.EnableSelection = xlUnlockedCells
            wxhS.Protect
        End If
 Next



End Sub

关闭自动保存 - 这就是答案。它使“另存为”既可以手动工作也可以在代码中工作。它实际上改变了您可以保存的选项。启用自动保存后,您只能获得“保存副本”,但关闭它后,您可以选择“保存”和“另存为”选项。

我在下面使用的最终代码在继续之前关闭了自动保存:

Sub NoticeGenerator()
    Dim wxhS As Worksheet, wbkT As Workbook
    Const TABCOLOR As Long = 192 'Standard Tab color Dark Red

'Turns off AutoSave because with it on it would compromise the original file while saving
    If Val(Application.Version) > 15 Then
        ThisWorkbook.AutoSaveOn = False
    End If   
 
 'Hides any tabs that are Dark Red
   For Each wxhS In Application.ActiveWorkbook.Worksheets
        If wxhS.Tab.Color <> TABCOLOR Then
            wxhS.Visible = xlSheetHidden
            
        End If
        If wxhS.Tab.Color = TABCOLOR Then
            wxhS.Cells.Font.Color = RGB(0, 0, 0)
            
        End If
    
    Next
          
'Displaying the SaveAs dialog box
FName = Application.GetSaveAsFilename("Notice Generator v", _
    "Excel files,*.xlsm", 1, "Select your folder and filename")

'Saves file if filename is entered, otherwise it won't save - only works if AutoSave is turned off first in code above
If FName <> False Then
ActiveWorkbook.SaveAs Filename:=FName, FileFormat:=xlOpenXMLWorkbookMacroEnabled
     End If

'Soft Protects without password red tabbed sheets to avoid compromising the file accidentally
 For Each wxhS In Application.ActiveWorkbook.Worksheets
        If wxhS.Tab.Color = TABCOLOR Then
            wxhS.EnableSelection = xlUnlockedCells
            wxhS.Protect
        End If
 Next


End Sub