Excel VBA-SaveCopyAs,然后将活动工作簿更改为新创建的文件

时间:2019-02-28 16:29:14

标签: excel vba

我正在努力对文件SaveCopyAs进行硬编码,然后将活动工作簿更改为新创建的文件,以便稍后可以在代码中将没有任何公式等的文件邮寄出去。 我尝试了Workbook.Activate,但似乎无法从自己的代码中获取正确的文件。

感谢您的帮助!

Sheets("Send").Visible = True
Sheets.Select
Cells.Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
ActiveSheet.Paste
Application.CutCopyMode = False

Dim thisWb As Workbook, d As Integer

Set thisWb = ActiveWorkbook
d = InStrRev(thisWb.FullName, ".")
'ActiveWorkbook.SaveAs Filename:=Left(thisWb.FullName, d - 1) & "-Prelims" & 
Mid(thisWb.FullName, d)
Sheets("Send").Visible = False
ActiveWorkbook.SaveCopyAs Filename:=Left(thisWb.FullName, d - 1) & "- Prelims" & Mid(thisWb.FullName, d)
ActiveWorkbook.Close savechanges:=False

1 个答案:

答案 0 :(得分:0)

保存文件副本后,需要打开它:

在这一行,修改:

ResourceQuota

在这些行之前添加这些语句,让我们在字符串变量中捕获输出文件名:

ActiveWorkbook.SaveCopyAs Filename:=Left(thisWb.FullName, d - 1) & "- Prelims" & Mid(thisWb.FullName, d)
ActiveWorkbook.Close savechanges:=False

然后我们可以做:

Dim newFileName as String
newFileName = Left(thisWb.FullName, d - 1) & "- Prelims" & Mid(thisWb.FullName, d)

最后,打开新的:

thisWb.SaveCopyAs Filename:=newFileName  ' use thisWb instead of ActiveWorkbook

将它们放在一起:

Dim newWorkbook as Workbook
Set newWorkbook = Workbooks.Open(newFileName)