
我在 Excel 中建立了一個流程圖,並使用 visio 將其視覺化。我有流程和決定。過程需要時間,決策結果以機率加權。假設我的數字是正確的(它們可能不是;)),這應該允許我計算整個過程的平均執行時間。
現在為了獲得平均執行時間,我需要沿著路徑進行總結,同時考慮權重。這本身並不是一項艱鉅的任務,但我更喜歡一個不特定於地圖當前狀態的論壇,但如果我添加更多流程和決策,它能夠自我更新。原則上,這應該可以透過某種遞歸函數來實現,因為路徑是在 Excel 工作表中定義的。然而,如果可能的話,我對如何在 Excel 中實現遞歸函數有點迷失。
有人這樣做過嗎?或有關於這是如何運作的提示?或者我需要求助於外部工具嗎?
因此「處理步驟」和「下一步ID」一起定義了從「開始」到「結束」的多條路徑。決策的結果具有一定的權重(以百分比表示),即採用兩個「下一步 ID」中的第一個的機率。
所以在這種情況下,它會開始這樣的事情: 1 + 0.4*(2 ...) + 0.6*(2 ...) ... 如你所見,即使我永遠不會修改Excel,它也很難手動使其正確。現在想像我添加幾個步驟...
我希望這能夠澄清任務。我現在正在嘗試用 VBA 解決這個問題...但我從未使用過 VBA,所以我仍然希望得到任何提示。
答案1
好吧,我終於用遞歸 VBA 函數自己解決了這個問題:
Function ProcessTime(row, time) As Double
Application.Volatile
' Define Columns
ProcessStepCol = 2
NextStepIDCol = 4
ShapeTypeCol = 6
TimeCol = 7
PyesCol = 8
' Do actual calculation
ShapeType = Worksheets("Process").Cells(row, ShapeTypeCol).Value
' Exit at the End
If ShapeType = "End" Then
ProcessTime = time
Exit Function
End If
' Simply add current time if we have a Process
If ShapeType = "Process" Then
NextStepRow = GetNextStepRows(row, 0)
TimeOfThisRow = Worksheets("Process").Cells(row, TimeCol).Value
ProcessTime = time + ProcessTime(NextStepRow, TimeOfThisRow)
Exit Function
End If
' Add wheights to branches if we have a Decision
If ShapeType = "Decision" Then
NextStepRowYes = GetNextStepRows(row, 0)
NextStepRowNo = GetNextStepRows(row, 1)
P_yes = Worksheets("AlertProcess").Cells(row, PyesCol).Value / 100
P_no = 1 - P_yes
ProcessTime = time + (P_yes * ProcessTime(NextStepRowYes, 0)) + (P_no * ProcessTime(NextStepRowNo, 0))
Exit Function
End If
End Function
' Find the row of the next step
Function GetNextStepRows(row, stepNo) As Long
Application.Volatile
' Define Columns
ProcessStepCol = 2
NextStepIDCol = 4
' Do actual calculation
NextStepIDs = Worksheets("AlertProcess").Cells(row, NextStepIDCol).Value
NextStepIDsSplit = Split(NextStepIDs, ",")
NextStepID = NextStepIDsSplit(stepNo)
GetNextStepRows = Worksheets("AlertProcess").Range("B:B").Find(What:=NextStepID).row
End Function
是的,我知道這不是最優雅的代碼;)