将一个工作表中的单元格保存到另一个工作簿中的工作表 - 无需提示即可保存

Saving Cells from One Worksheet to a worksheet in a diff workbook -- having it save without prompt

快速提问。

我这里有这个宏,它具有我需要的功能(将工作簿中一个工作表中的 2 个单元格保存到另一个工作簿中的工作表中。

我遇到的唯一问题是它一直提示保存文件,但不让我实际保存它。

关于我需要添加什么代码片段来解决的任何想法?它似乎以只读方式打开文件,也许这就是问题所在?

Sub Test()
Dim x As Workbook
Dim y As Workbook
Dim vals As Variant

'## Open both workbooks first:
Set x = Workbooks.Open("C:/File.xlsx")
Set y = ThisWorkbook

'Store the value in a variable:
vals = y.Sheets("List").Range("B3:B4").Value

'Use the variable to assign a value to the other file/sheet:
x.Sheets("P1").Range("A1:A2").Value = vals

'Close x:
x.Close

End Sub

我已经对此进行了测试,它可以在没有警告消息的情况下运行。只需在打开和保存语句中编辑文件路径

Option Explicit

Sub Test()
Dim x As Workbook
Dim y As Workbook
Dim vals As Variant

'## Open both workbooks first:
Set x = Workbooks.Open("C:\Test.xlsx")
Set y = ThisWorkbook

'Store the value in a variable:
vals = y.Sheets("List").Range("B3:B4").Value

'Use the variable to assign a value to the other file/sheet:
x.Sheets("P1").Range("A1:A2").Value = vals

'Turns off Alerts
Application.DisplayAlerts = False

'Saves file with changes
x.SaveAs Filename:="C:\Test.xlsx", FileFormat:=51, CreateBackup:=False, AccessMode:=xlExclusive, _
ConflictResolution:=Excel.XlSaveConflictResolution.xlLocalSessionChanges

'Close x:
x.Close

'turns alerts back on
Application.DisplayAlerts = True

End Sub