我需要帮助将数据从已关闭的工作簿(不打开它们)复制到使用VBA的主工作簿中的列中。我一直收到错误:
运行时错误424:需要对象
这是我的代码:
Set x = Workbooks.Open("C:\Users\DD\Desktop\EMS")
x.Sheets("PO Report").Range("Y3:Y500").Copy
y.Activate
Sheets("Sheet1").Range("Q2").PasteSpecial
Application.CutCopyMode = False
x.Close
提前感谢您的帮助!
答案 0 :(得分:0)
这是问题 - 您没有指定excel文件的文件名
Set x = Workbooks.Open("C:\Users\DD\Desktop\EMS")
您无法从已关闭的文件中读取数据......必须打开
你还需要暗淡你的x
对象
Dim x as object
答案 1 :(得分:0)
我修改了发布的代码here。在“Sheet1”表单模块中插入以下代码:
Option Explicit
Sub GetDataDemo()
Dim FilePath$
Dim i As Long
Const FileName$ = "EMS.xlsx"
Const SheetName$ = "PO Report"
FilePath = "C:\Users\DD\Desktop\"
DoEvents
Application.ScreenUpdating = False
If Dir(FilePath & FileName) = Empty Then
MsgBox "The file " & FileName & " was not found", , "File Doesn't Exist"
Exit Sub
End If
For i = 3 To 500
Range("Q" & i - 1) = GetData(FilePath, FileName, SheetName, Range("Y" & i))
Next i
ActiveWindow.DisplayZeros = False
End Sub
Private Function GetData(Path, File, Sheet, Rng)
Dim Data$
Data = "'" & Path & "[" & File & "]" & Sheet & "'!" & Rng.Address(, , xlR1C1)
GetData = ExecuteExcel4Macro(Data)
End Function