我在VBA和编码方面是一个全新的人,如果满足条件粘贴到另一本书中,我试图从同一工作簿的单元格中获取数据。
尝试获取在excel单元格中输入的值时出现此错误:
Run Time Error '424' object required
当我按下调试按钮时,它会转到第一行并突出显示它。我不知道为什么会这样?同样在监视窗口中时,它也会显示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
我已经忽略了这一部分,因为它含糊您的意图。您要粘贴在哪张纸上?应该粘贴什么范围?由于未定义此“ 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
首先,似乎您需要声明缺少的变量:
Sub copycells()
Dim a as Integer
Dim b as Integer
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
此外,您可以尝试使用Set
运算符。
无论如何,不清楚哪个属性或方法会产生错误。因此,我建议打破属性和方法调用的链条:
a = Workbooks("Purchase.xlsx").Worksheets("Sale").Cells(Rows.Count, 2).End(xlUp).Row
只需声明单独的代码行,即可使用单个属性或方法代替。例如:
Dim workbk as Excel.Workbook
Dim worksht as Excel.Worksheet
Set workbk = Workbooks("Purchase.xlsx")
Set worksht = workbk.Worksheets("Sale")
按照这种方式,您将能够找到有问题的电话。
这应该教您始终使用Option Explicit
。如果您一直在使用它,则可能发生以下情况[Compile error: Variable not defined
):
确定后:
您发现Row
出了点问题。
[另一个Rows
更改为Compile error: Variable not defined
后。确定后:
您发现a =
出了点问题。
[添加Dim a As Long
后再添加一个Compile error: Variable not defined
。确定后:
您发现i
出了点问题。
[添加Dim i As Long
后再添加一个Compile error: Variable not defined
。确定后:
您再次看到Row
出了点问题。
[另一个Rows
更改为Compile error: Variable not defined
后。确定后:
您发现b =
出了点问题。
[添加Dim b As Long
后再添加一个Compile error: Variable not defined
。确定后:
您发现ActivateSheet
出了点问题。
[更改为ActiveSheet
后最后是Run-time error
:
[Debug
之后:
[Row
再次看起来可疑。
[更改为Rows
后另一个Run-time error
:
[Debug
之后:
您发现ActiveSheet.Paste
出了点问题,尤其是Paste
。
[更改为ActiveSheet.PasteSpecial
后另一个Run-time error
:
[Debug
之后:
您看到Worksheets("Sale").Activate
出了点问题。
由于Source.xlsx处于活动状态,因此您考虑更改为Workbooks("Purchase.xlsx").Worksheets("Sale").Activate
,一切都终于确定了。还是它?
The代码
Option Explicit
Sub copycells()
Dim a As Long
Dim b As Long
Dim i As Long
With ThisWorkbook.Worksheets("Sale")
a = .Cells(.Rows.Count, 2).End(xlUp).Row
For i = 2 To a
If .Cells(i, 6).Value = "Parmesh" Then
.Rows(i).Copy
With Workbooks("Source.xlsx").Worksheets("Billdetails")
b = .Cells(.Rows.Count, 2).End(xlUp).Row
.Cells(b + 1, 1).PasteSpecial
End With
End If
Next
' If Purchase.xlsx and ThisWorkbook are the same then use the following:
'.Cells(1).Select
End With
Application.CutCopyMode = False
' If Purchase.xlsx and ThisWorkbook are not the same then use the following:
'Workbooks("Purchase.xlsx").Worksheets("Sale").Cells(1, 1).Select
End Sub
' Assuming that you need only values and that "Thisworkbook" is "Purchase.xlsx"
Sub copyCellsValues()
Const SourceBook As String = "Source.xlsx"
Const SourceSheet As String = "Billdetails"
Const MainSheet As String = "Sale"
Const MainColumn As Long = 6
Const Criteria As String = "Parmesh"
Dim Sale As Worksheet
Dim Bill As Worksheet
Dim a As Long
Dim b As Long
Dim i As Long
Set Sale = ThisWorkbook.Worksheets(MainSheet)
Set Bill = Workbooks(SourceBook).Worksheets(SourceSheet)
a = Sale.Cells(Sale.Rows.Count, 2).End(xlUp).Row
b = Bill.Cells(Bill.Rows.Count, 2).End(xlUp).Row + 1
For i = 2 To a
If Sale.Cells(i, MainColumn).Value = Criteria Then
Bill.Rows(b).Value = Sale.Rows(i).Value
b = b + 1
End If
Next
End Sub