Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Database “粘贴”给出;“所需对象”;错误_Database_Vba - Fatal编程技术网

Database “粘贴”给出;“所需对象”;错误

Database “粘贴”给出;“所需对象”;错误,database,vba,Database,Vba,我得到这个错误: 错误424:需要对象 在这一行: ActiveSheet.Paste Destination:=Worksheets("Sheet1").Range(Cells(erow, 1), Cells(erow, 1)) 在下面的代码中。为什么? Sub LoopThroughDirectory() Dim MyFile As String Dim erow Dim Filepath As String Filepath = "C:\User

我得到这个错误:

错误424:需要对象

在这一行:

ActiveSheet.Paste Destination:=Worksheets("Sheet1").Range(Cells(erow, 1), Cells(erow, 1))
在下面的代码中。为什么?

    Sub LoopThroughDirectory()

    Dim MyFile As String
    Dim erow
    Dim Filepath As String
    Filepath = "C:\Users\Julio Jesus Sanchez\Desktop\MRSK TXT\"
    MyFile = Dir("C:\Users\Julio Jesus Sanchez\Desktop\MRSK TXT\")

    Do While Len(MyFile) > 0
    If MyFile = "MRSK DATABASE.xlsm" Then
    Exit Sub
    End If

    Workbooks.Open (Filepath & MyFile)
    Range("A1:K1").Copy
    ActiveWorkbook.Close

    erow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
    ActiveSheet.Paste Destination:=Worksheets("Sheet1").Range(Cells(erow, 1), Cells(erow, 1))

    MyFile = Dir

    Loop

    End Sub

关闭
ActiveWorkbook
,然后参考
ActiveSheet
,这是什么意思?如果您参考的是母版手册,那么您应该明确地告诉它,通过将其设置为对象,您的VBA是否也在MS Access模块或Excel中?您刚刚将其标记为
数据库
VBA
,我猜是Excel,因为您没有任何对Excel对象的引用

Dim sh as Worksheet

Set sh = Workbooks("NameOfWorkBook.xlsx").WorkSheets("WorksheetName")
然后,每当您参考工作表时:

sh.Paste Destination:=sh.Range(Cells(erow, 1), Cells(erow, 1))

你为什么认为这是个错误?您是否收到错误?是的,它表示这是一个“424”错误,并且需要一个对象。哪一行引发了错误?请帮助我们帮助您。我们怎么可能用这么少的信息来回答这个问题呢?我们在这里帮助(希望如此),而不是解决难题。ActiveSheet.Paste目标:=工作表(“Sheet1”)。范围(单元格(erow,1),单元格(erow,1))