对象不支持这个属性或方法

我基本上试图匹配2工作表中的一系列单元格,并从相应的列中获取值。

当我运行这个代码时,我得到一个错误:

对象不支持这个属性或方法

我的代码

Sub Macro2() Dim rowcount As Integer Dim target As Variant rowcount = Range("E2", Range("E2").End(xlDown)).Count For i = 1 To rowcount + 1 target = Application.Match(ActiveSheet.Cells(i, 6) & "-" & Cells(i, 5) & "-" & Cells(i, 4) & "-" & Cells(i, 3), Worksheets(14).Range("A6:A3000"), 0) If ActiveSheet.Cells(i, 6) & "-" & Cells(i, 5) & "-" & Cells(i, 4) & "-" & Cells(i, 3) = _ ActiveSheet.Cells(i + 1, 6) & "-" & Cells(i + 1, 5) & "-" & Cells(i + 1, 4) & "-" & Cells(i + 1, 3) Then ActiveSheet.Cells(i, 17) = Worksheets(14).target.Offset(0, 10) End If Next i End Sub 

如果要查找行数,则需要使用以下语法: rowcount = Range(Range("E2"), Range("E2").End(xlDown)).Rows.Count ,最好使用LongInteger

此外,当无法find与Application.Match函数的成功匹配时,您需要捕获可能的错误,使用If Not IsError(target) Then

注意 :尽量避免使用ActiveSheet ,而是使用完全限定的工作表,在代码中使用Worksheets("YourSheetName")

 Sub Macro2() Dim rowcount As Long Dim target As Variant rowcount = Range(Range("E2"), Range("E2").End(xlDown)).Rows.Count For i = 1 To rowcount + 1 target = Application.Match(ActiveSheet.Cells(i, 6) & "-" & Cells(i, 5) & "-" & Cells(i, 4) & "-" & Cells(i, 3), Worksheets(14).Range("A6:A3000"), 0) If Not IsError(target) Then ' successful Match If ActiveSheet.Cells(i, 6) & "-" & Cells(i, 5) & "-" & Cells(i, 4) & "-" & Cells(i, 3) = _ ActiveSheet.Cells(i + 1, 6) & "-" & Cells(i + 1, 5) & "-" & Cells(i + 1, 4) & "-" & Cells(i + 1, 3) Then ActiveSheet.Cells(i, 17) = Worksheets(14).target.Offset(0, 10) End If Else MsgBox "Unable to find a Match !" End If Next i End Sub 

target不是Worksheets对象的属性或方法。

我相信你需要改变Worksheets(14).target.Offset(0, 10)Worksheets(14).Range("A5").Offset(target, 10)


你的编码也应该一致。 在你的代码中,你有像ActiveSheet.Cells(i + 1, 6) & "-" & Cells(i + 1, 5) ... ,在那里你具体限定Cells(i + 1, 6)ActiveSheet但允许Cells(i + 1, 5)默认为在ActiveSheet 。 虽然它起作用,但如果以后需要重新读取代码,它会变得非常混乱。