Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel VBA返回带有大量计算的奇怪结果_Excel_Vba - Fatal编程技术网

Excel VBA返回带有大量计算的奇怪结果

Excel VBA返回带有大量计算的奇怪结果,excel,vba,Excel,Vba,我在excel中创建了一个函数,它基本上在For语句的动态范围内搜索字符串,并返回单元格的值。这基本上是一个预算功能,但这不是重点 问题是,所有结果都很小,但当结果太大时(比如32000左右……出于某种原因,这似乎是数字),函数开始返回0 有人遇到过这样的问题吗 下面是有问题的代码: Function Material(item As String, Optional sheetType As String) As Integer Dim wSheet As Worksheetr Dim co

我在excel中创建了一个函数,它基本上在For语句的动态范围内搜索字符串,并返回单元格的值。这基本上是一个预算功能,但这不是重点

问题是,所有结果都很小,但当结果太大时(比如32000左右……出于某种原因,这似乎是数字),函数开始返回0

有人遇到过这样的问题吗

下面是有问题的代码:

Function Material(item As String, Optional sheetType As String) As Integer

Dim wSheet As Worksheetr
Dim count As Integer
Dim offSet As Integer
Dim ref As Integer
Dim bottomRight As Integer
Dim upperLeft As Integer
Dim rng As Range
Dim cVal As Integer

For Each wSheet In Worksheets
    If wSheet.Name = "Drywall Pricing" Then
        dwIndex = wSheet.Index - 1
    End If
Next wSheet

If IsMissing(sheetType) Then
    sheetType = " "
Else
    sheetType = UCase(sheetType)
End If
For i = 1 To dwIndex
    wSheetName = Sheets(i).Name
    If InStr(UCase(wSheetName), sheetType) > 0 Then
        count = 9
        offSet = 44
        ref = 27
        For wall = 0 To count - 1
            On Error Resume Next
            Err.Clear
            upperLeft = (ref + 12) + (offSet * wall)
            bottomRight = (ref + 30) + (offSet * wall)
            Set rng = Sheets(i).Range("A" & upperLeft & ":B" & bottomRight)
            cVal = Application.WorksheetFunction.VLookup(item, rng, 2, False)
            If Err.Number > 0 Then
                cVal = 0
            End If
            units = units + cVal
        Next wall
    Else
        units = units + 0
    End If
Next i

If units > 0 Then
    Material = units
Else
    Material = 0
End If

End Function
我已经设置了一个电子表格来手动计算一定数量的项目(即“=A13+B5+B26”等),并将其与运行相关函数的结果进行比较,当结果较低时,它们彼此相等,因此我知道函数本身工作正常。那么,这是内存问题吗

任何帮助都将不胜感激


提前谢谢

VBA中的整数是16位,这意味着最大值为32767(最小值为-32768)


不要使用整数,而是使用Long来存储结果,这样可以在结果达到极限之前为您提供超过20亿的结果。

您可以发布待搜索excel文档内容的摘录吗

两个小评论:

在线上

If wSheet.Name = "Drywall Pricing" Then
    dwIndex = wSheet.Index - 1
End If
您可能希望退出进行,因为您已经找到了工作表,不想继续搜索

你有什么理由想要找到的表减去1吗

另一个注释是
Else
-子句中的
units=units+0
,根本不起任何作用

关于VBA中的

“整数”不是16位整数吗?即-32768到+32767?如果是这样,那么这里的代码就是你的罪魁祸首:

Dim cVal As Integer
...
        cVal = Application.WorksheetFunction.VLookup(item, rng, 2, False)
        If Err.Number > 0 Then
            cVal = 0
        End If

另外,我建议使用“选项显式”

整数的最大值为(2^15),即32768。您的错误捕获正在强制cVal=0。尝试将数据类型从integer更改为long。最大长度为(2^31-1),即2147483647,可以轻松处理您的值。

您为什么不调暗单位?不。。。这是我的一个错误。好吧,这对于32000左右的数字来说很有意义。谢谢你的快速回复!我会马上换的。所有材料表都在“干式墙定价”表之前,因此“干式墙定价”表索引的索引号减去1将给出函数需要扫描的表的总数。还有,units=units+0是我留下来的东西,但我不能告诉你为什么我一开始就把它放在那里。