我有以下 VBA 代码将数据从一张表(数据基于公式得出)复制到另一张表,但是当我从 informatica 工具调用时它不起作用,因为它无法处理以下警报。
此警报需要手动确认,当我运行并手动确认时,它运行正常。
代码:
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = False
objExcel.DisplayAlerts = False
set objWorkbook = objExcel.Workbooks.Open("<Source path>")
Set objWorksheet = objWorkbook.Worksheets("Sheet1")
Set objRange = objWorksheet.Range("A:L")
objRange.Copy
Set objExcel2 = CreateObject("Excel.Application")
objExcel2.Visible = False
objExcel2.DisplayAlerts = False
Set objWorkbook2 = objExcel2.Workbooks.Open("<Target path>")
Set objWorksheet2 = objWorkbook2.Worksheets("Sheet1")
objWorksheet2.Paste
objWorkbook2.Save
objWorkbook2.Close
objWorkbook.Close
objExcel.Quit
objExcel2.Quit
您能帮我如何抑制此警报并使我的代码正常运行吗?
答案1
错误/警报表明不能将日期粘贴为公式,而只能将其粘贴为值。
为什么不首先将代码更改为粘贴为值?你可以使用 来实现PasteSpecial xlPasteValues
。
或者在您的代码中:
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = False
objExcel.DisplayAlerts = False
set objWorkbook = objExcel.Workbooks.Open("<Source path>")
Set objWorksheet = objWorkbook.Worksheets("Sheet1")
Set objRange = objWorksheet.Range("A:L")
objRange.Copy
Set objExcel2 = CreateObject("Excel.Application")
objExcel2.Visible = False
objExcel2.DisplayAlerts = False
Set objWorkbook2 = objExcel2.Workbooks.Open("<Target path>")
Set objWorksheet2 = objWorkbook2.Worksheets("Sheet1")
objWorksheet2.Range("A:L").PasteSpecial xlPasteValues
objWorkbook2.Save
objWorkbook2.Close
objWorkbook.Close
objExcel.Quit
objExcel2.Quit
答案2
我不确定你为什么要使用两个 Excel 实例。如果这只是纯粹的 Excel(不确定 Informatica 工具是什么),那么此代码应该可以工作:
Sub Test()
Dim TargetBook As Workbook
Set TargetBook = Workbooks.Open("<Target Path>")
Dim SourceBook As Workbook
Set SourceBook = Workbooks.Open("<Source Path>")
SourceBook.Worksheets("Sheet1").Range("A:L").Copy Destination:=TargetBook.Worksheets("Sheet1").Range("B1")
End Sub