骰子符号在Excel中,获得最小的价值

在一些朋友的帮助下,我做了这个:它用标准的骰子符号expression,并给出最小的结果。

Public Function Rollmin(r As Range) As Variant Application.Volatile Dim v As String, NewForm As String, deemode As Boolean Dim dee As String dice = "d" glute = False dicemode = False v = r.Value NewForm = "=" For I = 1 To Len(v) ch = Mid(v, I, 1) If ch = dice Then NewForm = NewForm & "*" dicemode = True glute = True Else If Not IsNumeric(ch) And dicemode Then dicemode = False NewForm = NewForm & "" End If If glute Then ch = "1" glute = False End If NewForm = NewForm & ch End If Next I Rollmin = Evaluate(NewForm) 

结束function

当我用一个数字的骰子(如D4,D6,D8)饲料时,所有工作正常。

如果我给它1d4 + 4,我得到1 * 1 + 4 = 5

这是我的问题,当我给它一个d10,d12,d20或d100(或任何骰子超过一个数字),它只会采取第一个数字。 给定1d12 + 2,它会给1 * 12 + 2。 给定1d14 + 2它会给1 * 14 + 2。

我需要它考虑“d”和下一个操作符号+, – ,/之间的所有数字。

我试图用很多方法修改它,但是我不能把d和下一个运算符之间的所有数字都取出来,它总是取第一个数字,然后在第二个数字之后加上其他数字。

确切地说,我需要使我的函数calculcate任何骰子expression式的最小值。 给定XdY + Z,它会做X * 1 + Z,给定XdY + 2 + XdZ它会做X * 1 + 2 + X * 1。

提前致谢。

编辑1:

通过做这个 :

 Public Function Rollmin(r As Range) As Variant Application.Volatile Dim v As String, NewForm As String, deemode As Boolean Dim dee As String dice = "d" glute = False dicemode = False v = r.Value NewForm = "=" For I = 1 To Len(v) ch = Mid(v, I, 1) If ch = dice Then NewForm = NewForm & "*" dicemode = True glute = True Else If Not IsNumeric(ch) And dicemode Then dicemode = False NewForm = NewForm & "1+" End If If glute Then ch = "1" glute = False End If If IsNumeric(ch) And Not dicemode Then NewForm = NewForm & "+" & ch End If End If Next I Rollmin = (NewForm) End Function 

它的工作原理,但expression式有几个++。 这不妨碍它运作良好,但我想知道如何避免这…

编辑2:不,它不工作。

编辑3:可以拆分function有几个分隔符?

编辑4:这篇文章进行了审查,这个评论被添加到我的注意:“评论:删除RPG标签,因为这个问题与RPG语言无关”骰子符号只用于RPG(RolePlaying Games)我不反对到IBM / RPG语言。

这应该做你想要的:

 Public Function Rollmin(byval diceString As String) As Long dim splitByPlus as Variant 'Example 1d14+2+5d13 'Split string in all addition parts splitByPlus = Split(diceString, "+") 'splitByPlus is now Array(1d14,2,5d13) dim i as long 'Loop through each element of the array and adds the current number for i = LBound(splitByPlus) To Ubound(splitByPlus) Rollmin = Rollmin + Split(splitByPlus(i), "d")(0) 'First 1d14 gets split to (1,14) and the first element (1) gets added 'Second 2 gets split to (2) and the first element (2) gets added 'Third 5d13 gets split to (5,13) and the first element (5) gets added next 'Rollmin is now 1+2+5 End Function 

编辑:
好吧,我的解决scheme不是很容易修改。 用这个代替:

 Public Function Rollmin(byval diceString As String) As Long dim i as long dim convertedFormula as String dim currentCharacter as String for i = 1 to Len(diceString) currentCharacter = Mid(diceString,i,1) if currentCharacter = "d" then 'Skip d and all following numbers while i<Len(diceString) and IsNumeric(Mid(diceString,i+1,1)) i=i+1 wend else convertedFormula = convertedFormula & currentCharacter end if next Rollmin = Evaluate("=" & convertedFormula) End Function