简体   繁体   English

Excel VBA函数中的变量类型

[英]Variable Types in Excel VBA Function

I want to create a function that calculate cell in a determined range. 我想创建一个函数来计算确定范围内的单元格。

When I return the value of the calc to excel I get an #VALUE! 当我将calc的值返回到excel时,我得到一个#VALUE! error, which means that the variable types are different. 错误,表示变量类型不同。

I tried to use Cint(var) and get the same error. 我尝试使用Cint(var)并得到相同的错误。

Here is the code without Cint(): 这是没有Cint()的代码:

Function CalcTest(Interval As Range) As Integer
    Dim x As Integer
    Dim y As Integer
    x = Interval.Offset(0, 0).Value
    y = Interval.Offset(1, 0).Value
    CalcTest = x + y
End Function

I already tried: 我已经尝试过:

Function CalcTest(Intervalo As Range) As Integer
    CalcTest = Interval.Offset(0, 0).Value + Interval.Offset(1, 0).Value
End Function

And: 和:

Function CalcTest(Interval As Range) As Integer
    Dim x As Integer
    Dim y As Integer
    x = CInt(Interval.Offset(0, 0).Value)
    y = CInt(Interval.Offset(1, 0).Value)
    CalcTest = x + y
End Function

And Without declarating de function type: 并且无需声明de函数类型:

Function CalcTest(Interval As Range)
    ...
    ...
End Function

And in Excel I call the function with some range: 在Excel中,我会在一定范围内调用该函数:

=CalcText(A1:A2)

What Am I doing wrong? 我究竟做错了什么?

#

The big picture: 大图:

在此处输入图片说明

What I need to do is create a cel in any place that counts de total values of the $R col for every occurrency of a key value in $N col. 我需要做的是在$ N col中每次出现键值时,都在$ R col的合计值的任何地方创建一个cel。

For every time I have "TH" in $N Col, I need do accumulate de $R col value of that row in a cel. 对于$ N Col中的每次“ TH”,我都需要在cel中累积该行的$ R col价值。

Same for many others $N Values. 许多其他$ N值相同。

In this sample the value of the accumulated cel for TH in $N is 25. 在此样本中,TH在$ N中的累计cel的值为25。

Tks for the help!!! Tks的帮助!

Is this what you are trying? 这是您要尝试的吗?

Public Function CalcTest(rng1 As Range, rng2 As Range) As Variant
    On Error GoTo Whoa

    CalcTest = rng1.Value + rng2.Value

    Exit Function
Whoa:
    CalcTest = "Please check the Range Values"
End Function

In an Excel cell, put this formula =CalcText(A1,A2) 在Excel单元格中,将此公式=CalcText(A1,A2)

在此处输入图片说明

You are making this way hard on yourself. 您正在对自己进行这种方式。

Put this into a cell in row 2 and drag it down: 将其放入第2行的单元格中并将其向下拖动:

=SUMIF(N$2:N2,"TH",R$2:R2)

You should not simplify your code to something that doesn't fairly represent your question. 您不应将代码简化为不完全代表您的问题的代码。 From your comments and Question. 从您的评论和问题。

Function CalcTest(Interval As Range) As Integer
    Dim x As Range
    CalcTest = 0
    For Each x In Interval
        If IsNumeric(x) Then
            CalcTest = CalcTest + x.Value
        End If
    Next x
End Function

This will make sure what you are adding up is actually a number. 这样可以确保您要累加的实际上是一个数字。

But as is this will not work any different then the worksheet function: 但这与工作表功能没有什么不同:

=Sum(Range)

Simply converting the values to Integer won't work if what you are converting is not converatble. 如果您要转换的内容无法收敛,则仅将值转换为Integer是行不通的。 If you pass a Letter to the CInt() function it will just fail. 如果将Letter传递给CInt()函数,它将只会失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM