对象变量或未设置块变量 - 错误

时间:2015-10-27 08:44:36

标签: excel vba excel-vba

我正在运行一个简单的VBA代码,如下所示:

Sub TransferData()

'transfer stuff from workbook 1 to workbook 2

    Dim strPath1 As String

    Dim strPath2 As String

    Dim wbkWorkbook1 As Workbook

    Dim wbkWorkbook2 As Workbook

    'define paths and filenames

    strPath1 = "C:\blp\data\grid1.xls"

    strPath2 = "Z:\24AM\Risk Managemen\Risk Management Processes.xlsm"

    'copy the values across

    wbkWorkbook2.Worksheets("FXDUMP").Range("A1:Z2000").Value = wbkWorkbook1.Worksheets("Book1").Range("A1:Z2000").Value

    'close the two workbooks

    wbkWorkbook1.Close (False)

    wbkWorkbook2.Close (True)
End Sub

但是当我尝试运行宏时,我收到了以下错误消息:

  

对象变量或未设置块变量

你能不能给我一个输入来解决这个问题?

提前感谢。

1 个答案:

答案 0 :(得分:1)

在您的代码中包含以下语句,它将打开Excel工作表并将变量分配给Excel工作表

Set wbkWorkbook1 = Workbooks.Open(Filename:=strPath1)
Set wbkWorkbook2 = Workbooks.Open(Filename:=strPath2)