在EXCEL VBA中,如何将整数分成单个数字,然后可以数学运算,例如整数123456789分为1,2,3,4,5,6,7,8,9然后(1 * 8),(2 * 16),(3 * 32)等; 1 * 8,2 * 16,3 * 32仅是示例,实际乘数可以是不同的,例如1 * 1024,2 * 2048等。初始整数(123456789)是VBA项目中另一个模块的变量(id)。如果我没有清楚解释,请道歉。抱歉格式化(这是我在stackoverflow上的第一篇文章)。
Option Explicit
Sub NUMalpha()
Private Property Get num() As Integer
NUMlong = Int(id)
End Property
Dim Digit_1 As Integer
Dim Digit_2 As Integer
Dim Digit_3 As Integer
Dim Digit_4 As Integer
Dim Digit_5 As Integer
Dim Digit_6 As Integer
Dim Digit_7 As Integer
Dim Digit_8 As Integer
Dim Digit_9 As Integer
Digit_1 = left(NUMlong, 1)
Digit_2 = Mid(NUMlong, 2, 1)
Digit_3 = Mid(NUMlong, 3, 1)
Digit_4 = Mid(NUMlong, 4, 1)
Digit_5 = Mid(NUMlong, 5, 1)
Digit_6 = Mid(NUMlong, 6, 1)
Digit_7 = Mid(NUMlong, 7, 1)
Digit_8 = Mid(NUMlong, 8, 1)
Digit_9 = right(NUMlong, 1)
End Property
End Sub
答案 0 :(得分:0)
Sub test()
Dim sNum As string, i As Long, mulitp As Variant, aryNum As Variant
sNum = Format(Range("A1").Value, "0")
mulitp = 8
ReDim aryNum(1 To Len(sNum)) As Variant
For i = 1 To Len(sNum)
aryNum(i) = Mid(sNum, i, 1) * mulitp * 2 ^ (i - 1)
Next i
MsgBox Application.Sum(aryNum)
End Sub
使用以下公式
=SUMPRODUCT(MID(A1,ROW(INDIRECT("1:"&LEN(A1))),1)*1,8*2^(ROW(INDIRECT("1:"&LEN(A1)))-1))
这不是数组公式,所以按输入,而不是 ctrl + shift + 输入。< / p>
答案 1 :(得分:0)
123456789大于VBA中的最大整数大小(-32,768到32,767)。
Sub SplitLong()
Dim digits() As Long
Dim intValue As Long
Dim i As Long
Dim strValue As String
intValue = 123456789
strValue = intValue
ReDim digits(Len(strValue) - 1) As Long
For i = 1 To Len(strValue)
digits(i - 1) = Mid$(strValue, i, 1)
Next
End Sub