Excel VBA-我收到运行时错误424:使用workbooks.open打开另一个Excel文件时需要对象

时间:2019-02-23 02:55:27

标签: excel vba

我正在尝试将几列数据从一个excel文件复制到另一个。我尝试调试,但在打开工作簿的行上收到运行时错误424:对象-其他excel文件确实使用此代码打开,然后弹出错误。

<script src="https://unpkg.com/vue@2.6.7"></script>

<div id="app">
  <input
  type="text"
  :id="['tv_code_' + listItem.asset_id]"
  @input="getInputValue('tv_code', listItem.asset_id)"
>
  <pre>{{form.obj}}</pre>
</div>

1 个答案:

答案 0 :(得分:1)

创建一个最小,完整和可验证的示例(请参阅https://stackoverflow.com/help/mcve)会增加您获得快速响应并获得良好答案的机会。

解决方案
-将数据类型从“变体”更改为“范围”
-调整范围大小

有用:
通过添加类似Sub SampleData()的内容,您可以使我们的生活更加轻松,并且无需花费大量时间来重新创建问题,我们就可以看到您的问题。

在我对您的代码进行测试期间,确实发生了运行时错误424,然后才使用源数据创建对工作表的明确引用。
通过将actWs添加到Set SAPNum = actWs.Range(Cells(3, 2), Cells(NRows, 2)),即使在开始时未选择源数据工作表的情况下,该错误也消失了  的CreateMatDump

Option Explicit

Sub SampleData()
    Dim actCell As Range
    For Each actCell In Sheets(1).Range("A1:R15")
        actCell.Value = actCell.Address
    Next actCell
    On Error Resume Next

    Workbooks.Add
    If Sheets("Dump").Name <> "Dump" Then
        Worksheets.Add After:=Sheets(1)
        Sheets(2).Name = "Dump"
    End If
    On Error GoTo 0

    With Sheets("Dump")
        .Range("A1").Value = "SAPNum"
        .Range("B1").Value = "MatType"
        .Range("C1").Value = "MatGroup"
        .Range("D1").Value = "UOM.Value"
        .Range("E1").Value = "MPN.Value"
        .Range("F1").Value = "MatDesc"
        .Range("A:F").ColumnWidth = 14
    End With
    ActiveWorkbook.SaveAs "C:\temp\dumpfile.xlsx"
End Sub

Sub CreateMatDump()
    Dim DumpFile As Workbook 'SAP Material Dump File
    Dim actWb As Workbook
    Dim actWs As Worksheet
    Dim NRows As Long
    Dim SAPNum As Range, MatType As Range, MatGroup As Range, UOM As Range, MPN As Range, MatDesc As Range

    Set actWb = ThisWorkbook
    Set actWs = ThisWorkbook.Sheets(1)

    actWb.Activate
    actWs.Select

    'Count rows
    NRows = actWs.Cells(Rows.Count, 14).End(xlUp).Row

    'Copy values to arrays
    Set SAPNum = actWs.Range(Cells(3, 2), Cells(NRows, 2))
    Set MatType = actWs.Range(Cells(3, 6), Cells(NRows, 6))
    Set MatGroup = actWs.Range(Cells(3, 11), Cells(NRows, 11))
    Set UOM = actWs.Range(Cells(3, 10), Cells(NRows, 10))
    Set MPN = actWs.Range(Cells(3, 14), Cells(NRows, 14))
    Set MatDesc = actWs.Range(Cells(3, 9), Cells(NRows, 9))

    Debug.Print "SAPNum   : "; SAPNum.Address
    Debug.Print "MatType  : "; MatType.Address
    Debug.Print "MatGroup : "; MatGroup.Address
    Debug.Print "UOM      : "; UOM.Address
    Debug.Print "MPN      : "; MPN.Address
    Debug.Print "MatDesc  : "; MatDesc.Address

    'Open SAP Material Dump File
    'Set DumpFile = Workbooks.Open(Filename:="R:\BURNABY\SAP Templates (Parts Upload & Batch PR Entry)\SAP Material Dump - Test.xlsx")
    Set DumpFile = Workbooks.Open(Filename:="c:\temp\dumpfile.xlsx")
    'Set DumpFile = ActiveWorkbook

    'Print arrays to SAP Material Dump File
    With DumpFile.Sheets(2)
        SAPNum.Copy    'to:
                            .Range("A2").PasteSpecial Paste:=xlPasteValues
        MatType.Copy   'to:
                            .Range("B2").PasteSpecial Paste:=xlPasteValues
        MatGroup.Copy  'to:
                            .Range("C2").PasteSpecial Paste:=xlPasteValues
        UOM.Copy       'to:
                            .Range("D2").PasteSpecial Paste:=xlPasteValues
        MPN.Copy       'to:
                            .Range("E2").PasteSpecial Paste:=xlPasteValues
        MatDesc.Copy   'to:
                            .Range("F2").PasteSpecial Paste:=xlPasteValues
    End With
End Sub