使用VBA打开工作簿时出错

时间:2014-12-10 23:08:34

标签: excel vba excel-vba

我已尝试过多种方法在一个工作簿中打开一个单独的工作簿,但总是收到错误消息应用程序定义或对象定义错误,以下是我的代码:

Sub PrepaidImport()
Dim x As Workbook, y As Workbook, vals As Variant, MyFile1 As String, MyFile2 As String
MyFile1 = Application.GetOpenFilename()
Set x = Workbooks.Open(MyFile1)
End Sub

以下代码也会出现同样的错误:

Sub PrepaidImport()
Dim x As Workbook, y As Workbook, vals As Variant, MyFile1 As String, MyFile2 As String
Set x = Workbooks.Open("M:\Company\2014\YTD 2014 Prepaid Assets.xlsx")
End Sub

以下代码出现差异错误,因为我试图打开二进制工作簿:

Sub PrepaidImport()
Dim y as Workbook
Set y = Workbooks.Open("Y:\Branch\Prepaid Assets Amortization Import Template.xlsb")
End Sub

这是我的全部代码:

Sub PrepaidImport()
Dim x As Workbook, y As Workbook, vals As Variant, MyFile1 As String, MyFile2 As String

Set x = Workbooks.Open("M:\Company\2014\YTD 2014 Prepaid Assets.xlsx")
Set y = Workbooks.Open("Y:\Branch\Prepaid Assets Amortization Import Template.xlsb")

vals = x.Worksheets("11.2014").Range("A6", "A" &x.Worksheets("11.2014").Range("A6").End(xlDown)).Value
Set y.Worksheets("Journal_Details").Range("Y1").Value = vals

x.Close

End Sub

文件已打开,但弹出错误消息并使我无法运行后续代码。我在使用Excel 2007。

请帮忙!

1 个答案:

答案 0 :(得分:0)

问题在于你的倒数第二行,你不应该在分配行前面加上“set”语句,即它应该只是

y.Worksheets("Journal_Details").Range("Y1").Value = vals