英文:
I'm an absolute noob at Excel VBA and trying to create a function that checks how many values are bigger then a given value in a column
问题
Function Dontes2(a As Range, b)
Dim cell As Range
result = 0
For Each cell In a
If cell > b Then
result = result + 1
End If
Next cell
Dontes2 = result
End Function
这是你的代码,但当我在Excel中运行它时,它一直返回0作为结果。我给函数传递了一个包含从1到35的35个数字的范围,然后检查其中有多少个数字大于10。我期望结果是25,但函数一直返回0。我刚开始学习VBA,解决方案可能非常明显。提前感谢任何答案。
英文:
Function Dontes2(a As Range, b)
Dim cell As Range
result = 0
For Each cell In a
If cell > b Then
result = result + 1
Next cell
Dontes2 = result
End Function
here is the code but it keeps giving back 0 as a result when I try to run it in excel
I give the function a range of 35 numbers from 1 to 35 and check how many of them are bigger then 10. I would expect it to be 25 but the function keeps giving back 0.
I'm just starting to learn VBA and the solution might be really obvious. Thanks for any answers in advance.
答案1
得分: 1
@VBasic2008提出的代码很好,但由于你正在学习,我将使用你的原始代码:
首先,确保正确缩进:
> VBA缩进
其次,你的代码应该至少像这样:
Function Dontes2(a As Range, b)
Dim cell As Range
result = 0
For Each cell In a
If cell > b Then result = result + 1
Next cell
Dontes2 = result
End Function
当使用单元格或数字作为参数调用时,代码可以完美运行。
请注意,红色示例返回0。这是因为我将第二个参数传递为字符串,所以因为它不是数字,它返回0:请检查公式:
这是我能够获得0作为结果的唯一方法,因为你的代码看起来不错。确保使用正确的输入。
为了避免这种情况,尽量声明你将使用的所有变量,甚至是你函数的参数。所以你的代码的一个好版本应该像这样:
Function Dontes2(ByVal a As Range, ByVal b As Long) As Long
Dim cell As Range
Dim Result As Long '不需要result = 0,因为所有数值变量都会被初始化为0
For Each cell In a
If cell > b Then Result = Result + 1
Next cell
Dontes2 = Result
End Function
请注意,即使之前失败的红色示例现在也能正常工作:
这是因为VBA强制将"10"
解释为数字,所以在cell > b
的比较中可以完美运行。
之前输出为0,是因为你的第二个参数b
没有声明,所以默认情况下,所有未声明的变量都被视为Variant
类型,它可以保存几乎任何数据类型。因此,"10"
被读取为文本,而不是数字,然后cell > b
将始终为False
。
希望这些解释对你有所帮助。
英文:
The code proposed by @VBasic2008 is great but because you are learning I'm going to use your original code:
Firt of all, make sure you indent properly:
Second, your code should be like this at least:
Function Dontes2(a As Range, b)
Dim cell As Range
result = 0
For Each cell In a
If cell > b Then result = result + 1
Next cell
Dontes2 = result
End Function
The code works perfectly when invoked using cells or even numbers as arguments.
Note the red case returns 0. It's because I'm passing the second argument as a string so because it's not numeric, it returns 0: Check the formulas please:
This is the only way I can obtain 0 as result because your code looks good. Make sure you use the right inputs.
To avoid this, try always to declare all the variables you are going to use, even the arguments of yur Function. So a nice version of your code should look like this:
Function Dontes2(ByVal a As Range, ByVal b As Long) As Long
Dim cell As Range
Dim Result As Long 'no need of result = 0 because all numeric variables are initialized as 0
For Each cell In a
If cell > b Then Result = Result + 1
Next cell
Dontes2 = Result
End Function
Check this:
Notice how noe even the red case that failed before works properly:
This is because VBA is forcing "10"
to be read as a number so the comparison in cell > b Then result = result + 1
works perfectly.
Your output was 0 before because your second argument b
was not declared so by default all non declared variables are treated as a Variant
type, which can hold almost anything. So "10"
was being read as a text, not a number, and then cell > b
will always be False
Hope this throws some light
答案2
得分: 0
练习循环和条件语句:计算大于指定值的数目(`COUNTIF`)
- 在模块顶部始终使用 `Option Explicit`。它要求你声明所有变量,但它会帮助你在编译时(运行之前)检测到拼写错误和其他错误。
**一个快速修复**
<!-- language: lang-vb -->
Option Explicit
Function Dontes2(ByVal SourceRange As Range, ByVal Value As Double) As Long
Dim cell As Range, Result As Long
For Each cell In SourceRange.Cells
If VarType(cell.Value) = vbDouble Then ' 是一个数字
If cell.Value > Value Then ' 大于指定值
Result = Result + 1
'Else ' 不大于指定值;什么也不做
End If
'Else ' 不是数字;什么也不做
End If
Next cell
Dontes2 = Result
End Function
英文:
Practicing Loops and If-s: Count Values Greater Than (COUNTIF
)
- Always use
Option Explicit
at the top of the module. It will require you to declare all variables, but it will help you to detect typos and other mistakes at compile time (before run-time).
A Quick Fix
<!-- language: lang-vb -->
Option Explicit
Function Dontes2(ByVal SourceRange As Range, ByVal Value As Double) As Long
Dim cell As Range, Result As Long
For Each cell In SourceRange.Cells
If VarType(cell.Value) = vbDouble Then ' is a number
If cell.Value > Value Then ' is greater than
Result = Result + 1
'Else ' is not greater than; do nothing
End If
'Else ' is not a number; do nothing
End If
Next cell
Dontes2 = Result
End Function
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论