方法'范围'失败。 VBA

Sub tester() Dim total As Integer Dim z As Integer For z = 4 To 90 If Not IsNull(Range("c" & z)) Then total = total + Application.WorksheetFunction.Index(Range("h2:h69171"), Application.WorksheetFunction.Match(Cells("c" & z) & Cells("A98").Value, Range("l2:l69171"), 1)) End If Next z Range("b98").Value = total End Sub 

这段代码给了我一个object'_Global失败的方法范围(在If …行上)。 有人可以帮我找出原因吗? 提前致谢

 Sub tester() Dim total As Integer Dim z As Integer Dim wf As WorksheetFunction Set wf = Application.WorksheetFunction With Sheet1 For z = 4 To 90 If Not IsEmpty(.Cells(z, 3).Value) Then total = total + wf.Index(.Range("h2:h69171"), wf.Match(.Cells(z, 3).Value & .Cells(98, 1).Value, .Range("L2:L69171"), 1), 1) End If Next z .Range("b98").Value = total End With End Sub 

单元格需要一个行和列参数。 而且,将所有范围参考前缀添加到表格中是很好的select – 我使用With Block并使用一段时间来启动范围参考。

此外,当您不向INDEX提供列参数时,它将返回整行。

我创build了一个wfvariables,只是为了保持代码的简洁性和可读性。