简体   繁体   English

需要运行时错误“424”对象:Excel VBA

[英]Run-time error '424' Object Required : Excel VBA

I am very new to coding and I am trying to copy data's from one worksheet to another worksheet on a particular rows.我对编码非常陌生,我试图将数据从一个工作表复制到特定行上的另一个工作表。

Sub copycolumns()
    Dim lastrow As Long, erow As Long
    lastrow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row '*/error shows here/*
    For i = 2 To lastrow
        Sheet1.Cells(i, 1).Copy
        erow = Sheet2.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
        Sheet1.Paste Destination:=Worksheets("Sheet2").Cells(erow, 1)
        Sheet1.Cells(i, 3).Copy
        Sheet1.Paste Destination:=Worksheets("Sheet2").Cells(erow, 2)
        Sheet1.Cells(i, 6).Copy
        Sheet1.Paste Destination:=Worksheets("Sheet2").Cells(erow, 3)
    Next i
    Application.CutCopyMode = False
    Sheet2.Columns().AutoFit
    Range("A1").Select
End Sub

What I am missing in here?我在这里缺少什么?

Error 424 "object required" has only one reason to pop up on that line of code.错误 424“需要对象”只有一个原因会在该行代码上弹出。

Option Explicit isn't specified, and Sheet1 isn't declared anywhere.未指定Option Explicit ,并且未在任何地方声明Sheet1

Look at the properties of your "Sheet1" module (F4), then note the value of the (Name) property:查看“Sheet1”模块 (F4) 的属性,然后记下(Name)属性的值:

模块“Sheet1”的属性

The Name property is what you're seeing in Excel. Name属性是您在 Excel 中看到的内容。 The (Name) property isn't visible anywhere in Excel, and that value determines the name of a global-scope identifier that VBA declares automagically for you. (Name)属性在 Excel 中的任何位置都不可见,该值决定了 VBA 自动为您声明的全局范围标识符的名称。

You can't do Sheet1.Cells(...) if no worksheet has a (Name) property value of Sheet1 .如果没有工作表的(Name)属性值为Sheet1则不能执行Sheet1.Cells(...)

But specify Option Explicit anyway.但无论如何都要指定Option Explicit The reason for error 424 is that Sheet1 being undeclared, at run-time it's an implicit Variant with the default value of Empty , which isn't an object, thus against which a member call is illegal - only an object could ever possibly have a Cells member.错误 424 的原因是Sheet1未声明,在运行时它是一个默认值Empty的隐式Variant ,它不是一个对象,因此成员调用是非法的 - 只有一个对象可能有Cells成员。

Protip: give the (Name) property a meaningful name, eg SummarySheet .小贴士:给(Name)属性起一个有意义的名字,例如SummarySheet Then you can do SummarySheet.Cells(...) .然后你可以做SummarySheet.Cells(...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM