2015-02-06 67 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”并使用自动完成和条件格式来获得关于该计划的分析视图。

最后,我有和以前一样的结果,但是以更方便和可维护的形式。