我已经发现自己陷入了一个看起来非常简单和愚蠢的问题,但是现在我一直在努力解决这个问题超过24小时。VBA中的总变量
我有一个字符串(由|分隔的一串数字),我想要转换成数组,然后根据大小写求和一些数组键。
我发现的第一个问题是整数长度限制,我无法相信VBA无法返回高于32767的数字(然后我发现了很多...)。在“求解”之后,我发现当试图求和一些0值时,它实际上增加了我的总和,我找不到任何解释。
下面你可以看到我现在有:
Public Function calcTime(TimeType As String)
Dim jsSting As String
Dim strSplit As Variant
Dim tempTime as Double
jsSting = "100|0|10080|400|0|4320|70|0|1440|30|0|2280|10|0|7400|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|300|0|15855|90|0|1721"
'Split the string by delimiter
strSplit = Split(jsSting, "|")
Select Case UCase(TimeType)
Case "TOTAL"
tempTime = WorksheetFunction.Sum(strSplit(2), strSplit(5), strSplit(8), strSplit(11), strSplit(14), strSplit(17), strSplit(20), strSplit(23), strSplit(26), strSplit(29), strSplit(32), strSplit(35), strSplit(38))
Case "GROUP1" ' Team 1 + Team2
tempTime = WorksheetFunction.Sum(strSplit(2), strSplit(5), strSplit(8), strSplit(11), strSplit(14))
Case "GROUP2" ' Team 1 + Team2 + Team3
tempTime = WorksheetFunction.Sum(strSplit(2), strSplit(5), strSplit(8), strSplit(11), strSplit(14), strSplit(38))
Case "GROUP3" ' Team 5
tempTime = WorksheetFunction.Sum(strSplit(17), strSplit(20), strSplit(23), strSplit(26))
Case "GROUP4" ' Team 2
tempTime = strSplit(14)
Case "GROUP5" ' Team 6
tempTime = WorksheetFunction.Sum(strSplit(29), strSplit(32), strSplit(35))
End Select
Return tempTime
End Function
在这个例子中,我曾尝试使用Excel的SUM函数,以获得期望的结果,但它并不成功。
坚持TOTAL情况。它总结如下键 - 值:
jsString(2) - 10080
jsString(5) - 4320
jsString(8) - 1440
jsString(11) - 2280
jsString(14) - 7400
jsString(17) - 0
jsString(20) - 0
jsString(23) - 0
jsString(26) - 0
jsString(29) - 0
jsString(32) - 0
jsString(35) - 15855
jsString(38) - 0
这给出了一个总的41375,但是,当我做总和VBA我得到43096,我不明白为什么。如果我从SUM中删除0值,它将返回正确的41k值。
希望这是有道理的,答案很简单(我认真考虑在分配数据类型时错过了一些东西)。
非常感谢您的帮助!
我想在你的选择的情况下,你的意思是写'strSplit'而不是'JsSting',但是这不是我已经意识到这个问题后。 –
对不起,我修好了。生成的类型,因为我已经替换了变量名称(内部问题) – user3352559