如何用vba将形成a*b+c+d*e的数学表达式展开?

比如将“75*2+90*3+120*1”形式的字符串展开为”75+75+90+90+90+120″形式的字符串。

可以使用如下的自定义函数:

Function GetString(ByVal oRng As Variant) As String
    arr = Split(oRng, "+")
    For i = 0 To UBound(arr)
        k = 0
        arr1 = Split(arr(i), "*")
        If UBound(arr1) > 0 Then
            For j = 1 To arr1(1) * 1
                n = arr1(0)
                ReDim Preserve arrTemp(k)
                arrTemp(k) = n
                k = k + 1
            Next j
            arr(i) = Join(arrTemp, "+")
        End If
    Next i
    sResult = Join(arr, "+")
    GetString = sResult
End Function
       

发表评论