Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/26.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 为什么使用变量的计算结果与使用单元格引用的计算结果不同?_Excel_Vba - Fatal编程技术网

Excel 为什么使用变量的计算结果与使用单元格引用的计算结果不同?

Excel 为什么使用变量的计算结果与使用单元格引用的计算结果不同?,excel,vba,Excel,Vba,因此,我试图进行一些计算,我想知道为什么我的宏给出的结果与我在工作表上使用它时得到的结果不同,所以我测试了以下内容: 守则: Option Explicit Sub Test() With ThisWorkbook.Sheets("Programaciones") Dim Efectivos As Single: Efectivos = .Cells(62, 21) '4.65000009536743 Dim Llamadas As Single: L

因此,我试图进行一些计算,我想知道为什么我的宏给出的结果与我在工作表上使用它时得到的结果不同,所以我测试了以下内容:

守则:

Option Explicit
Sub Test()

    With ThisWorkbook.Sheets("Programaciones")
        Dim Efectivos As Single: Efectivos = .Cells(62, 21) '4.65000009536743
        Dim Llamadas As Single: Llamadas = .Cells(65, 21) '12
        Dim TMO As Long: TMO = .Cells(66, 21) '398.108567311734
        Debug.Print Utilisation(Efectivos, Llamadas, TMO) * Efectivos * 1800 / TMO / Llamadas
        Debug.Print Utilisation(.Cells(62, 21), .Cells(65, 21), .Cells(66, 21)) * .Cells(62, 21) * 1800 / .Cells(66, 21) / .Cells(65, 21)
    End With

End Sub
为什么会发生这种情况?我尝试将
Single
更改为
Double
变量类型,得到了相同的结果。我知道vba中的浮点数,但当它们是否存储在变量中时,这更是一个问题

编辑:Utilization是Erlang library的一个函数,具有以下代码

'-----------------------------------------------------------------------
Public Function Utilisation(Agents As Single, CallsPerHalfAnHour As Single, AHT As Long) As Single
'Copyright © T&C Limited 1996, 1999
'Calculate the utilisation percentage for the given number of agents
' Agents is the number of agents available
' CallsPerHalfAnHour is the number of calls received in one hour period
' AHT (Average handle time) is the call duration including after call work in seconds  e.g 180
Dim BirthRate As Single, DeathRate As Single, TrafficRate As Single
Dim Util As Single

On Error GoTo UtilError

     BirthRate = CallsPerHalfAnHour
     DeathRate = 1800 / AHT
'calculate the traffic intensity
     TrafficRate = BirthRate / DeathRate
     Util = TrafficRate / Agents

UtilExit:
     Utilisation = MinMax(Util, 0, 1)
     Exit Function

UtilError:
    Util = 0
    Resume UtilExit

End Function
'-----------------------------------------------------------------------
Private Function MinMax(val As Single, Min As Single, Max As Single) As Single
'Apply minimum and maximum bounds to a value
    MinMax = val
    If val < Min Then MinMax = Min
    If val > Max Then MinMax = Max
End Function
'-----------------------------------------------------------------------
'-----------------------------------------------------------------------
公共功能利用(代理为单一,呼叫服务为单一,AHT为单一)
版权所有©T&C有限公司,1996年、1999年
'计算给定数量代理的使用百分比
'代理是可用的代理数
'CallsPerHalfAnHour是一小时内收到的呼叫数
'AHT(平均处理时间)是通话持续时间,包括通话后的工作时间,以秒为单位,例如180
低出生率为单一,脱笼率为单一,交通量为单一
将Util设置为单个
关于错误转到UtilError
出生率=CallsPerHalfAnHour
脱色率=1800/AHT
计算交通强度
交通率=出生率/脱胎率
Util=流量/代理
UtilExit:
利用率=最小最大值(Util,0,1)
退出功能
UtilError:
Util=0
恢复UtilExit
端函数
'-----------------------------------------------------------------------
私有函数最小值最大值(val为单值,最小值为单值,最大值为单值)为单值
'对值应用最小和最大界限
最小最大值=val
如果valMax,则MinMax=Max
端函数
'-----------------------------------------------------------------------

您可以
将TMO的长度设置为
并读取数值
TMO=.Cells(66,21)
,即您在单元格中所说的
398.108567311734
。由于
Long
只能保存整数(无小数),
TMO
中的值将是
398
,而不是
398.108567311734

因此,当你用…计算时,你会得到不同的结果

  • TMO
    哪个是
    398
  • .Cells(66,21)
    这是
    398.108567311734
将其声明为
Dim TMO as Double
,以便变量可以采用十进制值,或者确保在使用
时将其转换为早于
cLng(.Cells(66,21))
的小数


这应给出与变量相同的结果:

Utilisation(.Cells(62, 21), .Cells(65, 21), .Cells(66, 21)) * cSng(.Cells(62, 21)) * 1800 / cLng(.Cells(66, 21)) / cSng(.Cells(65, 21))
请注意,这一行

Dim Efectivos As Single: Efectivos = .Cells(62, 21) '4.65000009536743
将实际将双精度
4.6500009536743
转换为单精度。因此变量
Efectivos
中的值是
4.65
,因为它被声明为
Single
,因此不能包含像
4.6500009536743
这样的精确数字

下表显示了值如何随所使用变量的类型而变化:


另一个例子:

  • 现实生活:
    10/3=333333…
    “不允许3的数量
  • 单个:
    cSng(10/3)=3333333
    '7位精度
  • 双精度:cDbl(10/3)=333

将从
Double
转换为
Single
,这将改变精度,但我们需要查看
利用率
代码。@Rory从
Double
转换为
Single
是什么意思?我所有的变量都是单变量。此外,还添加了您要求的代码。单元格值存储为双精度。相关单元格中的实际值是什么?@Rory用变量声明旁边的值编辑了初始代码。这些值看起来不正确,因为在
Long
中有小数。您是如何确定这些值的?单元格是文字值还是公式的结果?谢谢您的回答!我不知道为什么我会认为一个
单曲
能处理的小数比它实际能处理的小数多得多。@Damian,如果你想深入研究这个问题的话。单精度限制为7位。检查我的编辑。