首页 文章

Excel VBA-错误'424'对象所需问题

提问于
浏览
1

嗨,我完全是新的VBA . 我正在尝试创建一个按钮,它可以自动将数据从sheet1(“概述”)上的列A-E复制到Sheet2(“示例”) .

下面是我从教程视频中复制的代码,但最终会出现错误424消息 - 需要对象 .

问题似乎与lastrow:lastrow = Sheetoverview.Cells(Rows.Count,1).End(xlUp).Row

有人可以帮我解决这个问题吗?谢谢!

Sub copyconlumns()

Dim lastrow As Long, erow As Long

lastrow = Sheetoverview.Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To lastrow
Sheetoverview.Cells(i, 1).copy
erow = sheetexample.Cells(Tows.Count, 1).End(xlUp).Offset(1, 0).Row

Sheetoverview.Paste Destination = Worksheets("Sheetexample").Cells(erow, 1)

Sheetoverview.Cells(i, 2).copy
Sheetoverview.Paste Destination = Worksheets("Sheetexample").Cells(erow, 2)

Sheetoverview.Cells(i, 3).copy
Sheetoverview.Paste Destination = Worksheets("Sheetexample").Cells(erow, 3)

Sheetoverview.Cells(i, 4).copy
Sheetoverview.Paste Destination = Worksheets("Sheetexample").Cells(erow, 4)

Sheetoverview.Cells(i, 5).copy
Sheetoverview.Paste Destination = Worksheets("Sheetexample").Cells(erow, 5)

Next i

Application.CutCopyMode = False
sheetexample.Columns.AutoFit
Range("A1").Select

End Sub

1 回答

  • 1

    VBA中的点运算符访问对象的属性或方法 . 因此 - 当您收到 object required 错误时

    lastrow = Sheetoverview.Cells(Rows.Count, 1).End(xlUp).Row
    

    在点之前出现问题 . 在这种情况下,它几乎肯定是 Sheetoverview . 除非这是一个全局变量,它已经被赋予了一个超出sub的值(这将是一个有问题的设计),这是一个未声明的变量,它从未被设置为等于一个工作表 . 如果您将 Sheetoverview 替换为 Sheets("Overview") 并将 sheetexample 替换为 Sheets("Example") ,则代码更有可能正常工作 .

    很难过分强调将行 Option Explicit 放在VBA中每个模块顶部的重要性 . 这会强制您声明变量并允许编译器捕获许多错误,就像您在此处获得的错误一样 . 可以将VBA编辑器配置为在所有新模块中自动插入该行( Tools/Options/Require Variable Declatation ) .

相关问题