VBA从另一个工作表中提取数据时出错

时间:2019-03-20 11:52:32

标签: excel vba

我已经在多个子程序上运行了相同的代码,但是只有一个完全相同的子程序给了我这个错误,但我不知道为什么: VBA-运行时错误1004“应用程序定义或对象定义的错误”

这是代码,我在错误涉及的地方发表了评论:

Sub Copy()

Dim customerBook As Workbook
Dim filter As String
Dim caption As String
Dim customerFilename As String
Dim customerWorkbook As Workbook
Dim targetWorkbook As Workbook

' make weak assumption that active workbook is the target
Set targetWorkbook = Application.ActiveWorkbook

' get the customer workbook
filter = "Text files (*.xlsx),*.xlsx"
caption = "Please select an input file "
customerFilename = Application.GetOpenFilename(filter, , caption)

Set customerWorkbook = Application.Workbooks.Open(customerFilename)
' copy data from customer to target workbook
Dim targetSheet As Worksheet
Set targetSheet = targetWorkbook.Worksheets(14)
Dim sourceSheet As Worksheet
Set sourceSheet = customerWorkbook.Worksheets(1)

'the line below is where the error refers to
targetSheet.Range("A1", "CA50000").Value = sourceSheet.Range("A1", "CA50000").Value

' Close customer workbook
customerWorkbook.Close

End Sub

0 个答案:

没有答案