我是VBA的新手,所以请原谅我。我在一段代码中遇到问题,根据工作表“1b.SearchClient”中单元格C14中的条件搜索数据库表“4c.CustomerP数据库”,并将这些匹配的条目复制到工作表“1b.Search Client”中。但是,当我尝试执行代码时,收到错误:
运行时错误'1004': 对象'_Worksheet'的方法'Range'失败
调试行带我去购买.Range(Cells(i,1),Cells(i,10))。复制
希望善良的灵魂可以帮助我!Sub findrecord()
Dim custid As String
Dim finalrow As Integer
Dim i As Integer
Dim targetWorkbook As Workbook
Dim activeWs As Worksheet
Dim purchases As Worksheet
Set targetWorkbook = Application.ActiveWorkbook
Set activeWs = targetWorkbook.Sheets("1b.SearchClient")
Set purchases = targetWorkbook.Sheets("4c.CustomerP Database")
activeWs.Range("A26:J200").ClearContents
custid = activeWs.Range("C14").Value
For i = 7 To 10000
If purchases.Cells(i, 1) = custid Then
purchases.Range(Cells(i, 1), Cells(i, 10)).Copy
activeWs.Range("A1000").End(x1Up).Offset(1, 0).PasteSpecial x1PasteFormulasAndNumberFormats
End If
Next i
End Sub
答案 0 :(得分:1)
范围内的细胞也需要参考购买:
purchases.Range(purchases.Cells(i, 1), purchases.Cells(i, 10)).Copy
或使用With:
With purchases
If .Cells(I, 1) = custid Then
.Range(.Cells(I, 1), .Cells(I, 10)).Copy
activeWs.Range("A1000").End(x1Up).Offset(1, 0).PasteSpecial x1PasteFormulasAndNumberFormats
End If
End With