将"一个工作表"中的单元格保存到不同工作簿中的工作表中--在没有提示的情况下保存



快速问题。

我这里有这个宏,它具有我需要的功能(将工作簿中一个工作表中的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

相关内容

最新更新