使用工作簿时出错

时间:2013-10-29 10:14:56

标签: excel vba

我想开发一种方法来确保从1个工作簿(ThisWorkbook)复制信息,并将其粘贴到空行的另一个工作簿(All data.xlsm)中。如果2张是在同一个工作簿中,我能够开发代码,但现在我需要引用另一个工作簿它不起作用(错误9)。困难在于两个工作簿都在团队网站上,但两个文件都是开放的。 请在下面找到我的代码(我最好的尝试)。

Dim actionlogRow
Dim actionlogRowSet As Boolean
Dim RecordSave As String
Dim recordActionLog As String

actionlogRow = 1
actionlogRowSet = False

Application.ScreenUpdating = False

RecordSave = Range("new_actions").Cells(1, 1).Value

Workbooks("All data.xlsm").Activate

Do

    recordActionLog = Worksheets("Action Log").Range("C8:AE8").Offset(actionlogRow, 0).Cells(1, 1)

    If recordActionLog = "" Then
        'Location to copy to is current row
        actionlogRowSet = True
    Else
        'Look at next row
        actionlogRow = actionlogRow + 1
    End If

Loop Until actionlogRowSet = True

'Copy the record into the database
Call ThisWorkbook.Sheets("New actions").Range("new_actions_endorsed").Copy
Call Workbooks("All data.xlsm").Worksheets("Action Log").Range("C8:AE8").Offset(actionlogRow, 0).PasteSpecial(xlPasteValuesAndNumberFormats)

Application.ScreenUpdating = True

1 个答案:

答案 0 :(得分:0)

您必须在同一个Excel实例中打开这两个电子表格。如果您先打开Excel,然后从中打开两个电子表格(Ctrl O),您应该没问题。但是,如果您打开两个Excel程序文件,他们无法看到对方,您将收到“运行时错误9:下标超出范围”