计算excel-vba中的租赁成本

时间:2014-11-15 13:11:55

标签: vba excel-vba excel

您好我正在尝试为租车设置计算器。 您将在汽车类别,租赁日期内,无论您是否要预订燃油舱,以及您将要行驶多少公里,无论您是否需要冬季轮胎,你到了某个目的地,有多少人会在车里。

我的数据电子表格目前看起来像这样: http://i.imgur.com/P2kz6ts.png

所以,例如,如果你想租两辆福特嘉年华2天,冬季轮胎和燃料平坦80公里到目的地2。 计算器现在应该选择1-3天的价格并将其乘以2天两天。 为此,将增加冬季轮胎的费用,每天3欧元。然后它会增加燃油舱的成本50到150公里,因为你将行驶80公里。由于您正在前往目的地2,租赁公司愿意给予15欧元的折扣。最后,计算出的成本将除以2,因为2人将租车并均匀分摊成本。 该计算应如下所示: ((30 * 2 +(3 * 2)+13,80)-15)/ 2) 因此,在最后,总成本32,4€将显示在msgbox中。

现在,我如何编码,如果租赁天数在1-3之间,excel应该采用该特定列的值并使用它来计算成本。此外,如果您租车超过一周,则应使用6-7天的价格,并根据类别添加额外的天数。

问题已解决,请看答案。

2 个答案:

答案 0 :(得分:0)

以下是解决问题的代码:

If rentaldays > 30 Then
Application.Cells(3, 3).Value = "You're trying to rent a Car for more than 30 Days. Please use the longterm program."
price = 0
End If

If rentaldays = 30 Then
monat = (Application.WorksheetFunction.VLookup(car, Sheets("Data").Range("A:K"), 7, False))
End If

If rentaldays > 7 Then
zusatz = price + (((rentaldays - 7) * Application.WorksheetFunction.VLookup(car, Sheets("Data").Range("A:K"), 6, False)))
rentaldays = rentaldays - (rentaldays - 7)
End If

If rentaldays = 6 Or rentaldays = 7 Then
woche = (Application.WorksheetFunction.VLookup(car, Sheets("Data").Range("A:K"), 5, False))
End If

If rentaldays > 3 And rentaldays <= 5 Then
tage = price + (Application.WorksheetFunction.VLookup(car, Sheets("Data").Range("A:K"), 4, False) * rentaldays)
End If

If rentaldays >= 1 And rentaldays <= 3 Then
tage = price + (Application.WorksheetFunction.VLookup(car, Sheets("Data").Range("A:K"), 3, False) * rentaldays)
End If

答案 1 :(得分:-1)

你需要让这更容易,而不是更聪明&#34;

只需添加更多列!

enter image description here

然后你有3个简单的规则(英文代码)

if days >= 30  then 
     price = Col K   'lookup for car
     days = days - 30
end if 

if days > 7 then 
     price = price + ((days - 7) * Col J)     'lookup for car
     days = days - (days - 7)
end if

if days > 0 then
     price = price + hlookup( days, C:I  .....)    'lookup for car
end if