尝试提示用户打开工作簿,然后对其进行更改并在用户打开的工作簿的相同路径下另存为新工作簿。同时,保持用户打开工作簿不保存/不更改并关闭它。
以下是我的代码但失败 - 新工作簿为空,用户打开的工作簿未关闭。
Sub saveas()
R = Application.GetOpenFilename _
(Title:="Please choose file", _
FileFilter:="Excel Files *.xls*; *.csv (*.xls*; *.csv),")
Set extwbk = Workbooks.Open(R)
If R = False Then
MsgBox "No file selected. Please click run again and select file.",
vbExclamation, "Sorry!"
Exit Sub
Else
End If
'a = extwbk.Worksheets.Count
'For i = 1 To a
'will input code there that make change on the open workbook
'Next i
'After change process complete, save the changed workbook as a new workbook in the
same path of the openworkbook and then keep the origin openworkbook
unchange and then close it
Set extwbk = ActiveWorkbook
ActiveWorkbook.Sheets.Copy
ActiveWorkbook.saveas Filename:=extwbk.Path & "\log.xlsx"
ActiveWorkbook.Close savechanges:=True
extwbk.Close savechanges:=False
End Sub
答案 0 :(得分:1)
我认为您需要更改
的最后一行ActiveWorkbook.Close savechanges:=False
到
ActiveWorkbook.Close savechanges:=True
extwbk.Close savechanges:=False
否则我认为当您使用ActiveWorkbook.saveas
保存新工作簿时 - 它会立即成为活动工作簿