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

huangapple go评论64阅读模式
英文:

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:

> VBA
> indentation

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.

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

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:

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

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:

> Data type
> summary

Notice how noe even the red case that failed before works properly:

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

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 &#39; is a number
            If cell.Value &gt; Value Then &#39; is greater than
                Result = Result + 1
            &#39;Else &#39; is not greater than; do nothing
            End If
        &#39;Else &#39; is not a number; do nothing
        End If
    Next cell
    Dontes2 = Result
End Function

huangapple
  • 本文由 发表于 2023年3月9日 18:27:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75683313.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定