VBA如何将整数分成不同的部分

我正在使用Excel中的VBA工作。 我需要将整数分成两部分,特别是前两位数字和最后两位数字。 数字最多有四位数字,至less有一位数字。 (我已经整理了空白值)例如。

7应该变成0和7,

23应该变成0和23,

642应该变成6和42,

1621应该变成16和21。

这是迄今为止的代码

Function Bloog(value1 As Integer) Dim value1Hours, value1Mins As Integer Select Case Len(value1) 'gives a number depending on the length of the value1 Case 1, 2 ' eg, 2 = 0, 2 or 16 = 0, 16 value1Hours = 0 value1Mins = value1 Case 3 ' eg, 735 = 7, 35 value1Hours = Left(value1, 1) ' 7 value1Mins = Right(value1, 2) ' 35 Case 4 ' eg, 1234 = 12, 34 value1Hours = Left(value1, 2) ' 12 value1Mins = Right(value1, 2) ' 34 End Select 

然而,当去获得的价值,我发现他们没有被分割成单独的部分,因为左()和右()函数会让我相信。 Len()似乎也没有工作,当它被赋值723时,它返回的长度是2。

任何提示将不胜感激。

=======================================

build议之后,我将这些值作为string进行转换,然后完成case语句并在之后将其转换回来。 (因为我需要他们进行一些计算)

 Private Function Bloog(value1 As Integer) As Integer Dim strValue1 As String Dim strValue1Hours, strValue1Mins As String Dim value1Hours, value1Mins As Integer 'converts the values into strings for the Left() and Right() functions strValue1 = CStr(value1) Select Case Len(value1) 'gives a number depending on the length of the value1 Case 1, 2 ' eg, 2 = 0, 2 or 16 = 0, 16 strValue1Hours = 0 strValue1Mins = value1 Case 3 ' eg, 735 = 7, 35 strValue1Hours = Left(value1, 1) ' 7 strValue1Mins = Right(value1, 2) ' 35 Case 4 ' eg, 1234 = 12, 34 strValue1Hours = Left(value1, 2) ' 12 strValue1Mins = Right(value1, 2) ' 34 End Select value1Hours = CInt(strValue1Hours) value1Mins = CInt(strValue1Mins) 

Len()仍然认为string的长度是2,所以case 2的语句被触发了,尽pipe这个strValue1Mins和value1Mins仍然等于832。

=======================

Len()正在testingValue1而不是strValue1,之后一切正常。

value1是一个整数,为什么不使用算术运算?

 Dim value1Hours as Integer,value1Mins as Integer value1Mins = value1 Mod 100 value1Hours = Int(value1 / 100) 

希望这可以帮助。 这是我能想到的最简单的方法。

 ValueAsString = Right("0000" & value1,4) strValue1Hours = Left(ValueAsString, 2) strValue1Mins = Right(ValueAsString, 2) 

物有所值:

  value1Hours = CInt(Left(Format(x, "0000"), 2)) value1Mins = CInt(Right(Format(x, "0000"), 2))