2015-02-06 97 views
1

作爲一名前程序員,我喜歡乾淨,可維護和文檔化的代碼。在Excel中清潔代碼?

作爲一名項目經理,我不得不復雜的擅長不時,並希望寫我寫程序的「乾淨」公式。

  1. (如何)我可以添加 「意見」 爲(多)的公式?

  2. (How)我可以「命名」(細胞相關)公式並重用它嗎? (例如,它寫的VB(或參數甚至F#)函數)

例1:代替

=IF(AND(L$11+(L$13-1)*7>=$C15;L$11+(L$13-1)*7<$D15);VLOOKUP($A15;RessourcePlan;12+$E15;WRONG);0) 

我想寫:

// check if this columns (L) date is inbetween startdate (Col C) and enddate (Col D) 
=IF (AND(L$11+(L$13-1)*7>=$C15;L$11+(L$13-1)*7<$D15); 

    // then select the the utilisation (12+E15) for the resp. team from the resource plan 
    VLOOKUP($A15;RessourcePlan;12+$E15;WRONG); 

    // else default to 0 
    0 

) //ENDIF 

,而是example1我可能會寫一個用戶自定義函數(UDF)

Function Utilization(thisDate As Date, task As String) As Double 
    ... // clean VB or F# code 
End Function 

然後寫

=Utilization(L11,A15) 
+1

「將」註釋「添加到(多行)公式中」在我看來,與「清潔」相反。 – pnuts 2015-02-06 14:49:00

+0

恕我直言,它比複雜的oneliner更好,它的意義/內部工作原理在寫作後幾天我不記得了。我添加一個例子... – Bastl 2015-02-06 16:26:50

+0

你有沒有嘗試過使用.net的excel互操作? – mydogisbox 2015-02-06 17:49:07

回答

0

身爲前功能程序員,我想出了下面給出我的問題在用戶定義的函數。

注意以下幾點:

  • 我僅編寫在-PARAMS映射到 結果「純」的數學函數,沒有狀態的變化,輸入/輸出,for循環或參與類似 。 (至少我有這樣的想法,「讓」和VBA顯然不是「純」..)
  • 單元格引用和自動完成是在工作表中完成(excel在這裏很強)
  • 常量是在一個特殊的表中定義爲命名區域稱爲「常量」

給定的任務,估計的努力和開始和結束日期,有多少美眉們,我需要? => 100%意味着一個人需要在這個全職工作(假設她正X daysPerWeek,如固定在常數)

Public Function util(ByVal startDate As Date, ByVal endDate As Date, ByVal estimation As Double) As Double 

Dim duration As Integer 
Let duration = DateDiff("d", startDate, endDate) 

Dim weeks As Double 
Let weeks = duration/7 

Dim pdays As Integer 
Let pdays = weeks * [daysPerWeek] 

util = estimation/pdays 

End Function 

因爲我有很多的任務,很多球隊對他們的工作,我d想知道我需要一個任務的團隊中有多少人。以下功能重用了上面的功能。請注意複雜的Vlookup調用的可讀名稱和對我的實際項目計劃「BaseLine1」的引用。這將是很容易擴展,以獲得其他方案等

Public Function currentUtil(currentDate As Date, id As String, team As Integer) As Double 
    Dim result As Double 

    Let startDate = Application.WorksheetFunction.VLookup(id, [BaseLine1], 11, 0) 
    Let endDate = Application.WorksheetFunction.VLookup(id, [BaseLine1], 12, 0) 
    Let estimation = Application.WorksheetFunction.VLookup(id, [BaseLine1], 5 + team, 0) 

    If (currentDate >= startDate And currentDate < endDate) Then 
    result = util(startDate, endDate, estimation) 
    Else 
    result = 0 
    End If 

    currentUtil = result 

End Function 

最後我用它的方式如下:我有一個主表的所有任務,包括他們的日期和每個小組估計的努力。在另一張紙上,我有橫向的星期和垂直上的每個團隊的任務。在單元格中,我使用函數「currentUtil」並使用自動完成和條件格式來獲得關於該計劃的分析視圖。

最後,我有和以前一樣的結果,但是以更方便和可維護的形式。