2014-11-14 25 views
0

我已经发现自己陷入了一个看起来非常简单和愚蠢的问题,但是现在我一直在努力解决这个问题超过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值。

希望这是有道理的,答案很简单(我认真考虑在分配数据类型时错过了一些东西)。

非常感谢您的帮助!

+0

我想在你的选择的情况下,你的意思是写'strSplit'而不是'JsSting',但是这不是我已经意识到这个问题后。 –

+0

对不起,我修好了。生成的类型,因为我已经替换了变量名称(内部问题) – user3352559

回答

0

你也许是指...?

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) 

无论如何,问题是,你还总结strSplit(38)这是1721即使您的样品等于0,正好Excel和VBA之间的区别在写;)

与检查您的代码中有一个MsgBox strSplit(38)

+0

现在感觉很愚蠢。我做了一个DEBUG MSGBOX,它总结了所有内容,并使用了38个38.非常感谢! – user3352559

+0

我敢打赌,每个开发者都至少花了一周的时间在这样的错误上,不要担心;) –

0

strsplit(38)= 1721,这是43096和41375的区别

+0

这不能解答这个问题。要批评或要求作者澄清,在他们的帖子下留下评论 - 你总是可以评论你自己的帖子,一旦你有足够的[声誉](http://stackoverflow.com/help/whats-reputation),你会能够[评论任何帖子](http://stackoverflow.com/help/privileges/comment)。 – user35443

+0

我不同意。他问为什么他的VBA代码和Excel中的添加有所不同。他认为jsString(38)是0,而它实际上是1721,这正好等于所述差异。 – Bart

+0

......所有这些都可以发表评论。 – user35443