简体   繁体   English

在Excel中清除代码?

[英]Clean Code in Excel?

As a former programmer I like clean, maintainable and documented code. 作为一名前程序员,我喜欢干净,可维护的文档化代码。

As a project manager I have to do complex excels from time to time and want to write "clean" formulas in the same way I wrote programs. 作为项目经理,我必须时常做复杂的工作,并希望以与编写程序相同的方式编写“干净”的公式。

  1. (How) Can I add "comments" into a (multiline) formula? (如何)可以在“(多行)”公式中添加“注释”?

  2. (How) Can I "name" a (cell-relative) formula and reuse it? (如何)可以“命名”(相对于单元格的)公式并重复使用? (eg write it as a vb (or even f#) function with parameters) (例如,使用参数将其编写为vb(甚至f#)函数)

Example1: Instead of 示例1:代替

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

I'd like to write: 我想写:

// 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

And instead of example1 I might write a user defined function (UDF) 而不是example1,我可能会写一个用户定义函数(UDF)

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

And then write 然后写

=Utilization(L11,A15)

Being a former functional programmer, I came up with the user defined functions given below for my problem. 作为一名前函数程序员,我想出了以下针对用户问题的用户定义函数。

Note the following points: 请注意以下几点:

  • I only write "pure" mathematical functions that map in-params to results, no state-changes, input/output, for-loops or similar involved. 我只写了将参数映射到结果的“纯”数学函数,没有状态变化,输入/输出,for循环或类似的函数。 (At least I have this in mind, "Let" and VBA is obviously not "pure"..) (至少我牢记这一点,“ Let”和VBA显然不是“纯”的。)
  • cell referencing and auto-completion is to be done in the sheets (excel is strong here) 单元格引用和自动完成将在工作表中完成(excel在这里很强)
  • constants are defined as named ranges in a special sheet called "Constants" 常数在称为“常数”的特殊工作表中定义为命名范围

Given a task with an estimated effort and start and end-date, how many ppl do I need? 给定一项估计工作量以及开始和结束日期的任务,我需要多少人? => 100% means one person needs to work on this full-time (assuming she is working x daysPerWeek, as fixed in the constants) => 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

Since I have many tasks and many teams working on them, I'd like to know how many ppl from a team I need for a task. 由于我有很多任务,并且有很多团队在处理这些任务,所以我想知道一个团队需要多少人来完成任务。 The following function reuses the function from above. 以下函数重用了上面的函数。 Note the readable names for the complex Vlookup-calls and the reference to "BaseLine1" which is my actual project plan. 注意复杂的Vlookup调用的可读名称,以及对“ BaseLine1”的引用,这是我的实际项目计划。 It will be very easy to extend this to get other scenarios etc. 将其扩展到其他方案等将非常容易。

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

Finally I use it in the following way: I have a main table with all tasks including their dates and estimated efforts per team. 最后,我以以下方式使用它:我有一个包含所有任务的主表,包括它们的日期和每个团队的估计工作量。 In another sheet, I have the weeks on the horizontal and the tasks per team on the vertical. 在另一张纸上,我有水平的几周和垂直的每个团队的任务。 In the cells I use the function "currentUtil" and use autocompletion and conditional formatting to get a analytical view on the plan. 在单元格中,我使用函数“ currentUtil”,并使用自动完成和条件格式来获取计划的分析视图。

Finally I have the same result as before, but in a much more convenient and maintainable form. 最终,我得到了与以前相同的结果,但是形式更加方便和可维护。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM