请参阅下面的代码,但正在发生的事情令人困惑。我已经看了这里的其他问题,并尝试了不同的东西,看看我是否可以让它工作,但我不能。我得到了要创建的新文件,但它只是空的Sheet1(2)。(我不想要,我想它粘贴到新文件的Sheet1。)但目前的格式是,我想复制Sheet1从旧文件,只是粘贴到新文件的Sheet1工作表的所有内容。如果你看到代码中的任何错误,请告诉我。谢谢
Sub saveResult()
Dim mywb As Workbook
Dim Save_Path As String
Dim sht As Worksheet
Set sht = Worksheets("Macro")
Save_Path = sht.Cells(5, 7).Value
Application.ScreenUpdating = False
Application.DisplayStatusBar = False
Application.EnableEvents = False
ActiveSheet.DisplayPageBreaks = False
todayyear = Format(Date, "YYYY")
filemonth = Format(Date, "mm")
fileyear = Right(todayyear, 2)
Set mywb = Workbooks.Add
Sheets("Sheet1").Copy Before:=mywb.Sheets("Sheet1")
mywb.SaveAs Save_Path & "\FinalProduct " & filemonth & fileyear & ".xlsx"
mywb.Close SaveChanges:=False
Application.ScreenUpdating = True
Application.DisplayStatusBar = True
Application.EnableEvents = True
ActiveSheet.DisplayPageBreaks = False
MsgBox ("Updated File Generated! Ready for review and upload.")
End Sub
2条答案
按热度按时间fxnxkyjh1#
最佳实践是为任何
Worksheet
指定父Workbook
。被隐式
由于
Workbooks.Add
激活了新工作簿,因此这相当于它复制一张白纸。
更好的是,不带任何参数的
Worksheet.Copy
会创建一个新的工作簿,因此将与
fwzugrvs2#
我已经修改了你的代码来解决这个问题。我还重命名了一些对象,使其更容易理解: