我是新来的vba。我试着做一个countif,如果答案超过0,那么我就会对超过500行的问题进行双重检查。
Sub DoubleCheck()
'Variables declaration
Dim row_rounter As Long, rngQ As Range, cel As Range, resDblChq As Variant, rngB As Range, rngC As Range, rngF As Range, rngH As Range, rngI As Range
row_counter = Sheets("GW-DB").Cells(Cells.Rows.Count, "B").End(xlUp).Row 'row counter
'Range Initializatoin
Set rngQ = Sheets("GW-DB").Range("Q2:Q" & row_counter)
Set rngC = Sheets("GW-DB").Range("C2:C" & row_counter)
Set rngF = Sheets("GW-DB").Range("F2:F" & row_counter)
Set rngH = Sheets("GW-DB").Range("H2:H" & row_counter)
Set rngI = Sheets("GW-DB").Range("I2:I" & row_counter)
Set rngB = Sheets("GW-DB").Range("B2:B" & row_counter)
'Loop starts to validate possibility of double cheques
For Each cel In rngQ
' countif same name, same date,same amount, same reason
resDblChq = Application.WorksheetFunction.CountIfs(rngB, c.Offset(0, -16).Value, rngC, c.Offset(0, -15).Value, rngF, c.Offset(0, -12).Value, rngH, c.Offset(0, -9).Value, rngI, c.Offset(0, -8).Value)
' if it counts more than 0 it means there's a possibility of having double cheques issued
If resDblChq > 0 Then
c.Value = "Possible payment made twice"
End If
Next cel
End Sub
谢谢你,我希望保持最简单的方式进行进一步修改
答案 0 :(得分:1)
使用cel而不是c
resDblChq = Application.WorksheetFunction.CountIfs(rngB, cel.Offset(0, -16).Value, rngC, cel.Offset(0, -15).Value, rngF, cel.Offset(0, -12).Value, rngH, cel.Offset(0, -9).Value, rngI, cel.Offset(0, -8).Value)
答案 1 :(得分:0)
在你的循环中,你引用的是Cel
而不是Sub DoubleCheck()
'Variables declaration
Dim row_rounter As Long, rngQ As Range, cel As Range, resDblChq As Variant, rngB As Range, rngC As Range, rngF As Range, rngH As Range, rngI As Range
row_counter = Sheets("GW-DB").Cells(Cells.Rows.Count, "B").End(xlUp).Row 'row counter
'Range Initializatoin
Set rngQ = Sheets("GW-DB").Range("Q2:Q" & row_counter)
Set rngC = Sheets("GW-DB").Range("C2:C" & row_counter)
Set rngF = Sheets("GW-DB").Range("F2:F" & row_counter)
Set rngH = Sheets("GW-DB").Range("H2:H" & row_counter)
Set rngI = Sheets("GW-DB").Range("I2:I" & row_counter)
Set rngB = Sheets("GW-DB").Range("B2:B" & row_counter)
'Loop starts to validate possibility of double cheques
For Each cel In rngQ
' countif same name, same date,same amount, same reason
resDblChq = Application.WorksheetFunction.CountIfs(rngB, cel.Offset(0, -16).Value, rngC, cel.Offset(0, -15).Value, rngF, cel.Offset(0, -12).Value, rngH, cel.Offset(0, -9).Value, rngI, cel.Offset(0, -8).Value)
' if it counts more than 0 it means there's a possibility of having double cheques issued
If resDblChq > 0 Then
cel.Value = "Possible payment made twice"
End If
Next cel
End Sub
。尝试改变这样。这将生成错误424。
def function_name(a):
a = 1
b = a + 1
return a, b
答案 2 :(得分:0)
c
未声明,并且由于未指定Option Explicit
,因此在运行时它是包含Variant
的隐式vbEmpty
:
Debug.Assert Not IsEmpty(c) ' assertion fails here
Debug.Print c.Offset(0, -16).Value '"object required"
当然你可以use Cel
instead of c
,但这不会阻止无数其他恼人的易于避免的运行时错误,只是在顶部指定Option Explicit
每个模块都会。
VBA很乐意编译拼写错误并将其声明为动态隐式Variant
变量而不指定Option Explicit
:这不是您编写可靠代码的方式。使用。选项。明确的。
错误说“需要对象”的原因是,从语法上讲,c.Offset(0, -16).Value
只能对名为Property Get
的对象进行Function
或c
成员调用 - 但IsObject(c)
为False
,因为vbEmpty
不是对象引用。因此,需要对象。