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

问题描述 投票:0回答:1

我在VBA中是一个全新的人,并且一般来说都在编码,如果满足条件以粘贴到另一本书中,我试图从同一工作簿的单元格中获取数据。

尝试获取在excel单元格中输入的值时出现此错误:需要运行时错误“ 424”对象当我按下调试按钮时,它会转到第一行并突出显示它。我无法识别为什么会这样?同样在监视窗口中时,它也会显示a = 0。

代码是

[*Sub copycells()
a = ThisWorkbook.Worksheets("Sale").Cells(Row.Count, 2).End(xlUp).Row
For i = 2 To a
If Worksheets("Sale").Cells(i, 6).Value = "Parmesh" Then
Worksheets("Sale").Row(i).copy
Workbooks("Source.xlsx").Worksheets("Billdetails").Activate
b = Workbooks("Source.xlsx").Worksheets("Billdetails").Cells(Row.Count, 2).End(xlUp).Row
Workbooks("Source.xlsx").Worksheets("Billdetails").Cells(b + 1, 1).Select
ActivateSheet.Paste
Worksheets("Sale").Activate
End If
Next
Application.CutCopyMode = False
Workbooks("Purchase.xlsx").Worksheets("Sale").Cells(1, 1).Select
End Sub*][1]
vba excel-vba
1个回答
0
投票

我已经忽略了这一部分,因为它含糊您的意图。您要粘贴在哪张纸上?应该粘贴什么范围?由于未定义此“ ActivateSheet”,因此如果激活它,将导致您出错。

ActivateSheet.Paste

但是,类似的事情应该可以使您入门:

Option Explicit

Sub copycells()

Dim a As Long
Dim i As Long
Dim b As Long

a = Workbooks("Purchase.xlsx").Worksheets("Sale").Cells(Rows.Count, 2).End(xlUp).Row
For i = 2 To a
If Worksheets("Sale").Cells(i, 6).Value = "Parmesh" Then
    Workbooks("Purchase.xlsx").Worksheets("Sale").Rows(i).Copy
    Workbooks("Source.xlsx").Worksheets("Billdetails").Activate
    b = Workbooks("Source.xlsx").Worksheets("Billdetails").Cells(Rows.Count, 2).End(xlUp).Row
    Workbooks("Source.xlsx").Worksheets("Billdetails").Cells(b + 1, 1).Select

    'ActivateSheet.Paste '## This part is unclear what your itention is. Is to paste something from the Sale Worksheet?

    Workbooks("Purchase.xlsx").Worksheets("Sale").Activate
End If
Next
Application.CutCopyMode = False
Workbooks("Purchase.xlsx").Worksheets("Sale").Cells(1, 1).Select
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.