提示用户打开工作簿并另存为新工作簿

时间:2018-04-25 03:22:31

标签: vba save

尝试提示用户打开工作簿,然后对其进行更改并在用户打开的工作簿的相同路径下另存为新工作簿。同时,保持用户打开工作簿不保存/不更改并关闭它。

以下是我的代码但失败 - 新工作簿为空,用户打开的工作簿未关闭。

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

1 个答案:

答案 0 :(得分:1)

我认为您需要更改

的最后一行
ActiveWorkbook.Close savechanges:=False

ActiveWorkbook.Close savechanges:=True
extwbk.Close savechanges:=False

否则我认为当您使用ActiveWorkbook.saveas保存新工作簿时 - 它会立即成为活动工作簿