Excel VBA:从外部工作簿复制范围时出错

Excel VBA:从外部工作簿复制范围时出错,excel,copy,range,vba,Excel,Copy,Range,Vba,我正试图将一系列单元格从关闭的工作簿复制到当前工作簿,但始终出现错误1004。我使用的代码如下所示: Sub Sheet2() Dim Filt As String Dim FilterIndex As Integer Dim Title As String Dim Multi As Boolean Dim DataFile Dim WBdata As Workbook 'I prompt the user to select the file to import Filt = "Excel

我正试图将一系列单元格从关闭的工作簿复制到当前工作簿,但始终出现错误1004。我使用的代码如下所示:

Sub Sheet2()
Dim Filt As String
Dim FilterIndex As Integer
Dim Title As String
Dim Multi As Boolean
Dim DataFile
Dim WBdata As Workbook

'I prompt the user to select the file to import
Filt = "Excel Workbook 2010 (*.xlsx),*.xlsx," & "Excel Workbook (*.xls), *.xls," & "All Files (*.*),*.*"
FilterIndex = 1
Title = "Select file to import"
Multi = False
DataFile = Application.GetOpenFilename(FileFilter:=Filt, FilterIndex:=FilterIndex, Title:=Title, MultiSelect:=Multi)

If DataFile = False Then
    MsgBox "No file was selected"
End If

'Open the file selected by the user
Set WBdata = Workbooks.Open(DataFile)

'Get the data
WBdata.Activate
Sheets("Sheet1").Range(Cells(4, 1), Cells(4, 1).End(xlDown).Offset(-1, 0)).Copy _ ThisWorkbook.Sheets("Sheet2").Columns(1)
ThisWorkbook.Sheets("Sheet2").Activate
ThisWorkbook.Sheets("Sheet2").Columns(1).Select
Selection.EntireColumn.AutoFit

'Close and Select Cell (1,1)
WBdata.Close
ThisWorkbook.Sheets("Manager").Activate
ThisWorkbook.Sheets("Manager").Cells(1, 1).Select
End Sub
调试器在
工作表(“Sheet1”).范围(单元格(4,1)、单元格(4,1).结束(xlDown).偏移量(-1,0)).复制uuThisWorkbook.Sheets(“Sheet2”).列(1)

我在一个测试文件中尝试了相同的语法,结果很顺利,但我无法在实际文件中实现。这是测试文件中的代码:

Sheets("Sheet1").Range(Cells(1, 1), Cells(1, 1).End(xlDown).Offset(-1, 0)).Copy ThisWorkbook.Sheets("Sheet1").Columns(2)
谢谢你的帮助,谢谢

两件事

  • 您可能希望在
    MsgBox“未选择任何文件”
    之后退出子项,以便代码不会在此行中给出错误
    Set WBdata=Workbooks.Open(DataFile)
    如果用户取消了对话框?或者处理
    If
    语句的
    Else
    部分中的代码,如下代码所示

  • 出现该错误是因为您没有完全限定单元格,例如,请参见下面代码中的
    单元格(1,1)
    中的
    (点)

  • 您的代码可以重新编写为(未经测试


    伟大的人!非常感谢,它很有效!我需要深入研究
    .Cells
    语法以进一步理解它。
    Sub Sheet2()
        Dim Filt As String, Title As String
        Dim FilterIndex As Integer
        Dim Multi As Boolean
        Dim DataFile
        Dim WBdata As Workbook, ws As Worksheet
    
        Filt = "Excel Workbook 2010 (*.xlsx),*.xlsx," & _
               "Excel Workbook (*.xls), *.xls," & _
               "All Files (*.*),*.*"
        FilterIndex = 1
        Title = "Select file to import"
        Multi = False
        DataFile = Application.GetOpenFilename(FileFilter:=Filt, _
                                               FilterIndex:=FilterIndex, _
                                               Title:=Title, _
                                               MultiSelect:=Multi)
    
        If DataFile = False Then
            MsgBox "No file was selected"
        Else
            'Open the file selected by the user
            Set WBdata = Workbooks.Open(DataFile)
            Set ws = WBdata.Sheets("Sheet2")
    
            'Get the data
            With ws
                .Range(.Cells(4, 1), .Cells(4, 1).End(xlDown).Offset(-1, 0)).Copy _
                ThisWorkbook.Sheets("Sheet2").Columns(1)
                ThisWorkbook.Sheets("Sheet2").Columns(1).EntireColumn.AutoFit
    
                'Close and Select Cell (1,1)
                WBdata.Close SaveChanges:=False
            End With
        End If
    End Sub