我已經發現自己陷入了一個看起來非常簡單和愚蠢的問題,但是現在我一直在努力解決這個問題超過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