我很难过。一直在努力这个。需要例程来计算值。在ColA范围内,有一个" v"或者" h" ...只是试图获得" v"逻辑首先工作。
非常简单。无法使这个功能工作一点点。非常感谢任何建议。谢谢!
这是我的代码:
Function CountFunction() As Integer
Dim ColA As Range, ColB As Range, ColJ As Range
Dim vResult As Integer
Dim Visitor As String
Set ColA = Range("A5:A20")
Set ColB = Range("B5:B20")
Set ColJ = Range("J5:J20")
Visitor = "v"
vResult = 0
For Each ColA In ColA.Range("A5:A20")
For Each ColB In ColB.Range("B5:B20")
For Each ColJ In ColJ.Range("J5:J20")
If ColA.Cells.Value = Visitor Then
If ColB.Cells.Value = ColJ.Cells.Value Then
vResult = vResult + 1
Else
vResult = vResult + 0
End If
Else
If ColB.Cells.Value = ColJ.Cells.Value Then
vResult = vResult + 0
Else
vResult = vResult + 1
End If
End If
Next
Next
Next
CountFunction = vResult
End Function
答案 0 :(得分:1)
“非常简单。” 不,不是:]
您可以替换
Set ColA = Range("A5:A20")
Set ColB = Range("B5:B20")
Set ColJ = Range("J5:J20")
带
Set ColA = Range("A1")
Set ColB = Range("A1")
Set ColJ = Range("A1")
或更好,替换
For Each ColA In ColA.Range("A5:A20")
For Each ColB In ColB.Range("B5:B20")
For Each ColJ In ColJ.Range("J5:J20")
与
For Each ColA In Range("A5:A20")
For Each ColB In Range("B5:B20")
For Each ColJ In Range("J5:J20")
答案 1 :(得分:1)
有可能(但我真的在猜测)你想要达到的目标是:
Function CountFunction() As Integer
Dim ColA As Range
Dim vResult As Integer
Dim Visitor As String
Visitor = "v"
vResult = 0
For Each ColA In Range("A5:A20")
If ColA.Value = Visitor Then
If ColA.Offset(0, 1).Value = ColA.Offset(0, 9).Value Then
vResult = vResult + 1
End If
Else
If ColA.Offset(0, 1).Value <> ColA.Offset(0, 9).Value Then
vResult = vResult + 1
End If
End If
Next
CountFunction = vResult
End Function
更新以下评论:
如果您希望将其用作UDF,我建议传递您正在使用的三个不同范围(在您的示例中&#34; A5:A20&#34;,&#34; B5:B20&#34; ,和&#34; J5:J20&#34;)加上Visitor
的值(因为我怀疑你将根据你是否正在计算&#34的值进行修改代码; v& #34;或&#34; h&#34;)。
因此将函数修改为:
Function CountFunction(rngA As Range, rngB As Range, rngX As Range, Visitor As String) As Variant
Dim vResult As Integer
Dim r As Long
vResult = 0
If rngA.Rows.Count <> rngB.Rows.Count Or _
rngA.Rows.Count <> rngX.Rows.Count Then
CountFunction = CVErr(xlErrRef)
Exit Function
End If
For r = 1 To rngA.Rows.Count
If rngA.Cells(r, 1).Value = Visitor Then
If rngB.Cells(r, 1).Value = rngX.Cells(r, 1).Value Then
vResult = vResult + 1
End If
Else
If rngB.Cells(r, 1).Value <> rngX.Cells(r, 1).Value Then
vResult = vResult + 1
End If
End If
Next
CountFunction = vResult
End Function
并将其命名为
=CountFunction($A5:$A20,$B5:$B20,J5:J20,"v")
(显然,我希望,可以调整范围以适应任何适当的范围。)
该函数执行非常有限的错误检查 - 它只检查三个范围中的每一个都具有相同的行数。但它不会检查列数或值的有效性等。