简体   繁体   English

使用句点值迭代日期范围内的值,以及如何将其用作公式

[英]Iterate over range of dates with values by period, how to use as formula

I have few start/end dates, few prices per month, and table with dates and course for that dates one per month, so i need to iterate over course values depending on how long period between two dates, I think alot about standard formulas nothing is matches, so I try to put my PHP knowledge, learn suntax and write my first vba code, I place it here: 我几乎没有开始/结束日期,每个月只有几个价格,并且每个月都有一个日期和课程表,因此我需要根据两个日期之间的间隔时间来遍历课程值,我认为很多关于标准公式的内容都没有是匹配项,因此我尝试介绍我的PHP知识,学习suntax并编写我的第一个vba代码,将其放在此处:

http://pastebin.com/XXLKvdA4 http://pastebin.com/XXLKvdA4

excel data mean table of contents looks like this: excel数据均值目录如下所示:

  [startDate]| [endDate]  |[inMonth]|           [totalByCourse]
  11.01.2010 | 20.02.2011 |   200   | =ConvertCourse( A1, B1, C1, myRange )
  15.05.2010 | 25.03.2011 |   400   | =ConvertCourse( A2, B2, C2, myRange )

... so on for about 100 times ...大约持续100次

#     [date]   |[course]
1   30.08.2010 |   5
    ...
5   30.12.2010 |   18
6   10.01.2011 |   2
    ...
10  10.05.2011 |   6
... so on for about 20 times

The range of date | 日期范围| course presented to macro is named as myRange it isn't begins from first month of year and not ends as last month of year so it make some lot's complexity to calc algorithm, and made me to write some bruteforce loops, thats why code have many if check statenements. 呈现给宏的课程名为myRange,它不是从一年的第一个月开始,也不是到一年的最后一个月结束,所以它使calc算法变得有些复杂,并且使我编写了一些bruteforce循环,这就是为什么代码有很多如果检查声明。

Excel compiler crushes at first line of ConvertCourse function call and tell nothing more than the comments is may appear etc, its also write strange output directly to module like Excel编译器压碎ConvertCourse函数调用的第一行,只说出可能出现的注释等内容,它还会将奇怪的输出直接写入模块,例如

ActiveCell.FormulaR1C1 = _
        "=ConvertCourse(R[-1]C[-6],RC[-5],R[-1]C[-3],CourseRange)"
    Range("K3").Select

I just want to use it as formula withour output to modules with functions, whats wrong with call and how to do it right? 我只想将其用作公式,然后将其输出到具有功能的模块,调用有什么问题以及如何正确执行?

The function takes the [totalByCourse] / 30 and multiplies it by the [Course] cost for each month between two dates while adjusting the cost for a short month. 该函数将[totalByCourse] / 30乘以两个日期之间每个月的[Course]费用,同时调整短个月的费用。

I recommend replicating the function with built-in Excel WorkSheet functions. 我建议使用内置的Excel WorkSheet函数复制该函数。 I could not do it without complete sample data and the actual expected results to compare the output. 如果没有完整的样本数据和实际的预期结果来比较输出,我将无法做到。

Two new named ranges are hard coded into the formula. 两个新的命名范围被硬编码到公式中。

CourseDates =OFFSET( Sheet1!$H$1,1,0,COUNTA(Sheet1!$H:$H)-1,1) CourseDates = OFFSET(Sheet1!$ H $ 1,1,0,COUNTA(Sheet1!$ H:$ H)-1,1)

Courses =OFFSET(Sheet1!$I$1,1,0,COUNTA(Sheet1!$I:$I)-1,1) 课程= OFFSET(Sheet1!$ I $ 1,1,0,COUNTA(Sheet1!$ I:$ I)-1,1)

Formula

SUMPRODUCT(--(CourseDates>=--"2011-07-20"),--(CourseDates<=--"2011-8-20"),Courses*DailyCost) SUMPRODUCT(-(课程日期> =-“ 2011-07-20”),-(课程日期<=-“ 2011-8-20”),课程*每日费用)

在此处输入图片说明

Function ConvertCourse(StartDate As Date, EndDate As Date, pSumMonthly As Double) As Double
    Const dLength As Integer = 30
    Dim x As Long
    Dim periodInDays As Integer, DailyCost As Double, result As Double
    Dim NewEndDate As Date

    DailyCost = pSumMonthly / dLength
    periodInDays = DateDiff("d", StartDate, EndDate)
    NewEndDate = StartDate + Int(periodInDays / dLength) * 30

    result = getCourseTotal(StartDate, NewEndDate, DailyCost)

    If NewEndDate < EndDate Then

        result = result + (EndDate - NewEndDate) * DailyCost * getCourseTotal(StartDate, NewEndDate, 1)

    End If

    ConvertCourse = result

End Function

'SUMPRODUCT(--(CourseDates>=--"2011-07-20"),--(CourseDates<=--"2011-8-20"),Courses*130)
Function getCourseTotal(StartDate As Date, EndDate As Date, DailyCost As Double)
    Dim Formula As String
    Formula = "=SUMPRODUCT(--(CourseDates>=--" & Format(StartDate, "\""YYYY-MM-DD\""") & _
              "),--(CourseDates<=--" & Format(EndDate, "\""YYYY-MM-DD\""") & "),Courses)"

    getCourseTotal = Evaluate(Formula) * DailyCost
End Function

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

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