我编写了这个VBA脚本来进行一些复杂的计算,但是输出始终为0。我已经在代码中描述了实际的答案。为什么它不起作用?
代码输出
Private Sub Button1_Click()
Dim city_name As String, deal_type As String, cft As Integer, discount_percentage As Integer, city_check As Integer, cft_check As Integer, deal_check As Integer, discount_check As Integer, weekday_bookingfee As Double, weekend_bookingfee As Double
discount_percentage = Range("b4").Value
city_name = Range("b5").Text
deal_type = Range("b6").Text
cft = Range("b7").Value
If city_name = "Delhi" Then
city_check = 1
ElseIf city_name = "Mumbai" Then
city_check = 1
ElseIf city_name = "Bangalore" Then
city_check = 1
ElseIf city_name = "Chennai" Then
city_check = 3
ElseIf city_name = "Kolkata" Then
city_check = 3
ElseIf city_name = "Hyderabad" Then
city_check = 2
ElseIf city_name = "Pune" Then
city_check = 3
ElseIf city_name = "Ahmedabad" Then
city_check = 4
ElseIf city_name = "Chandigarh" Then
city_check = 4
ElseIf city_name = "Jaipur" Then
city_check = 4
ElseIf city_name = "Goa" Then
city_check = 4
Else:
End If
If deal_type = "Food" Then
deal_check = 2
ElseIf deal_type = "Total" Then
deal_check = 1
ElseIf deal_type = "Buffet" Then
deal_check = 1
ElseIf deal_type = "Alcohol" Then
deal_check = 3
ElseIf deal_type = "Coupon" Then
deal_check = 4
ElseIf deal_type = "Explosive" Then
deal_check = 4
Else:
End If
If cft >= 5000 Then
cft_check = 1
ElseIf cft < 5000 And cft >= 2500 Then
cft_check = 2
ElseIf cft < 2500 And cft >= 1500 Then
cft_check = 3
ElseIf cft < 1500 And cft >= 800 Then
cft_check = 4
ElseIf cft < 800 And cft >= 500 Then
cft_check = 5
ElseIf cft < 500 Then
cft_check = 6
Else:
End If
If (discount_percentage = 50) And (WorksheetFunction.Sum(city_check, deal_check, cft_check) = 3) Then
weekend_bookingfee = 80 And weekday_bookingfee = 60
ElseIf (discount_percentage = 50) And (WorksheetFunction.Sum(city_check, deal_check, cft_check) = 4) Then
weekend_bookingfee = 70 And weekday_bookingfee = 60
ElseIf (discount_percentage = 50) And (WorksheetFunction.Sum(city_check, deal_check, cft_check) = 5) Then
weekend_bookingfee = 50 And weekday_bookingfee = 40
ElseIf (discount_percentage = 50) And (WorksheetFunction.Sum(city_check, deal_check, cft_check) = 6) Then
weekend_bookingfee = 50 And weekday_bookingfee = 40
ElseIf (discount_percentage = 50) And (WorksheetFunction.Sum(city_check, deal_check, cft_check) = 7) Then
weekend_bookingfee = 35 And weekday_bookingfee = 30
ElseIf (discount_percentage = 50) And (WorksheetFunction.Sum(city_check, deal_check, cft_check) = 8) Then
weekend_bookingfee = 30 And weekday_bookingfee = 25
ElseIf (discount_percentage = 50) And (WorksheetFunction.Sum(city_check, deal_check, cft_check) = 9) Then
weekend_bookingfee = 25 And weekday_bookingfee = 20
ElseIf (discount_percentage = 50) And (WorksheetFunction.Sum(city_check, deal_check, cft_check) = 10) Then
weekend_bookingfee = 20 And weekday_bookingfee = 15
ElseIf (discount_percentage = 50) And (WorksheetFunction.Sum(city_check, deal_check, cft_check) = 11) Then
weekend_bookingfee = 20 And weekday_bookingfee = 15
ElseIf (discount_percentage = 50) And (WorksheetFunction.Sum(city_check, deal_check, cft_check) = 12) Then
weekend_bookingfee = 15 And weekday_bookingfee = 10
ElseIf (discount_percentage = 50) And (WorksheetFunction.Sum(city_check, deal_check, cft_check) = 13) Then
weekend_bookingfee = 15 And weekday_bookingfee = 10
ElseIf (discount_percentage = 50) And (WorksheetFunction.Sum(city_check, deal_check, cft_check) = 14) Then
weekend_bookingfee = 5 And weekday_bookingfee = 5
Else:
End If
If (discount_percentage = 40) And (WorksheetFunction.Sum(city_check, deal_check, cft_check) = 3) Then
weekend_bookingfee = (80 * 0.75) And weekday_bookingfee = (60 * 0.75)
ElseIf (discount_percentage = 40) And (WorksheetFunction.Sum(city_check, deal_check, cft_check) = 4) Then
weekend_bookingfee = (70 * 0.75) And weekday_bookingfee = (60 * 0.75)
ElseIf (discount_percentage = 40) And (WorksheetFunction.Sum(city_check, deal_check, cft_check) = 5) Then
weekend_bookingfee = (50 * 0.75) And weekday_bookingfee = (40 * 0.75)
ElseIf (discount_percentage = 40) And (WorksheetFunction.Sum(city_check, deal_check, cft_check) = 6) Then
weekend_bookingfee = (50 * 0.75) And weekday_bookingfee = (40 * 0.75)
ElseIf (discount_percentage = 40) And (WorksheetFunction.Sum(city_check, deal_check, cft_check) = 7) Then
weekend_bookingfee = (35 * 0.75) And weekday_bookingfee = (30 * 0.75)
ElseIf (discount_percentage = 40) And (WorksheetFunction.Sum(city_check, deal_check, cft_check) = 8) Then
weekend_bookingfee = (30 * 0.75) And weekday_bookingfee = (25 * 0.75)
ElseIf (discount_percentage = 40) And (WorksheetFunction.Sum(city_check, deal_check, cft_check) = 9) Then
weekend_bookingfee = (25 * 0.75) And weekday_bookingfee = (20 * 0.75)
ElseIf (discount_percentage = 40) And (WorksheetFunction.Sum(city_check, deal_check, cft_check) = 10) Then
weekend_bookingfee = (20 * 0.75) And weekday_bookingfee = (15 * 0.75)
ElseIf (discount_percentage = 40) And (WorksheetFunction.Sum(city_check, deal_check, cft_check) = 11) Then
weekend_bookingfee = (20 * 0.75) And weekday_bookingfee = (15 * 0.75)
ElseIf (discount_percentage = 40) And (WorksheetFunction.Sum(city_check, deal_check, cft_check) = 12) Then
weekend_bookingfee = (15 * 0.75) And weekday_bookingfee = (10 * 0.75)
ElseIf (discount_percentage = 40) And (WorksheetFunction.Sum(city_check, deal_check, cft_check) = 13) Then
weekend_bookingfee = (15 * 0.75) And weekday_bookingfee = (10 * 0.75)
ElseIf (discount_percentage = 40) And (WorksheetFunction.Sum(city_check, deal_check, cft_check) = 14) Then
weekend_bookingfee = (5 * 0.75) And weekday_bookingfee = (5 * 0.75)
Else:
End If
If (discount_percentage = 30 Or discount_percentage = 20) And (WorksheetFunction.Sum(city_check, deal_check, cft_check) = 3) Then
weekend_bookingfee = (80 * 0.5) And weekday_bookingfee = (60 * 0.5)
ElseIf (discount_percentage = 30 Or discount_percentage = 20) And (WorksheetFunction.Sum(city_check, deal_check, cft_check) = 4) Then
weekend_bookingfee = (70 * 0.5) And weekday_bookingfee = (60 * 0.5)
ElseIf (discount_percentage = 30 Or discount_percentage = 20) And (WorksheetFunction.Sum(city_check, deal_check, cft_check) = 5) Then
weekend_bookingfee = (50 * 0.5) And weekday_bookingfee = (40 * 0.5)
ElseIf (discount_percentage = 30 Or discount_percentage = 20) And (WorksheetFunction.Sum(city_check, deal_check, cft_check) = 6) Then
weekend_bookingfee = (50 * 0.5) And weekday_bookingfee = (40 * 0.5)
ElseIf (discount_percentage = 30 Or discount_percentage = 20) And (WorksheetFunction.Sum(city_check, deal_check, cft_check) = 7) Then
weekend_bookingfee = (35 * 0.5) And weekday_bookingfee = (30 * 0.5)
ElseIf (discount_percentage = 30 Or discount_percentage = 20) And (WorksheetFunction.Sum(city_check, deal_check, cft_check) = 8) Then
weekend_bookingfee = (30 * 0.5) And weekday_bookingfee = (25 * 0.5)
ElseIf (discount_percentage = 30 Or discount_percentage = 20) And (WorksheetFunction.Sum(city_check, deal_check, cft_check) = 9) Then
weekend_bookingfee = (25 * 0.5) And weekday_bookingfee = (20 * 0.5)
ElseIf (discount_percentage = 30 Or discount_percentage = 20) And (WorksheetFunction.Sum(city_check, deal_check, cft_check) = 10) Then
weekend_bookingfee = (20 * 0.5) And weekday_bookingfee = (15 * 0.5)
ElseIf (discount_percentage = 30 Or discount_percentage = 20) And (WorksheetFunction.Sum(city_check, deal_check, cft_check) = 11) Then
weekend_bookingfee = (20 * 0.5) And weekday_bookingfee = (15 * 0.5)
ElseIf (discount_percentage = 30 Or discount_percentage = 20) And (WorksheetFunction.Sum(city_check, deal_check, cft_check) = 12) Then
weekend_bookingfee = (15 * 0.5) And weekday_bookingfee = (10 * 0.5)
ElseIf (discount_percentage = 30 Or discount_percentage = 20) And (WorksheetFunction.Sum(city_check, deal_check, cft_check) = 13) Then
weekend_bookingfee = (15 * 0.5) And weekday_bookingfee = (10 * 0.5)
ElseIf (discount_percentage = 30 Or discount_percentage = 20) And (WorksheetFunction.Sum(city_check, deal_check, cft_check) = 14) Then
weekend_bookingfee = (5 * 0.5) And weekday_bookingfee = (5 * 0.5)
Else:
End If
Range("b10").Value = weekday_bookingfee
Range("b11").Value = weekend_bookingfee
End Sub
我需要实际的Weekday_bookingfee和Weekend_bookingfee,正如我在代码中所描述的那样
答案 0 :(得分:1)
代码行
weekend_bookingfee = 35 And weekday_bookingfee = 30
不评估两个不同的语句,因为“ AND”代替了一个布尔表达式
更改所有:
Then
weekend_bookingfee = ... And weekday_bookingfee = ...
收件人:
Then
weekend_bookingfee = .... : weekday_bookingfee = ...
“:”实际上是分隔两个不同的语句
答案 1 :(得分:1)
通过使用几个数组,您可以完全消除最后所有If ElseIf
的麻烦,并将其简化为仅两行代码。我们知道数组元素的地址,因此我们在代码的后两行中使用简单的数学运算来确定应使用的元素。
Private Sub Button1_Click()
Dim city_name As String, deal_type As String, cft As Long, discount_percentage As Long, city_check As Long, cft_check As Long, deal_check As Long, discount_check As Long, weekday_bookingfee As Double, weekend_bookingfee As Double, Disc_arr As Variant, Weekend_arr As Variant, Weekday_arr As Variant
Dim resultCalc
Disc_arr = Array(, , 0.5, 0.5, 0.75, 1)
Weekend_arr = Array(, , , 80, 70, 50, 50, 35, 30, 25, 20, 20, 15, 15, 5)
Weekday_arr = Array(, , , 60, 60, 40, 40, 30, 25, 20, 15, 15, 10, 10, 5)
discount_percentage = Range("b4").Value
city_name = Range("b5").Text
deal_type = Range("b6").Text
cft = Range("b7").Value
Select Case city_name
Case "Delhi", "Mumbai", "Bangalore"
city_check = 1
Case "Hyderabad"
city_check = 2
Case "Chennai", "Kolkata", "Pune"
city_check = 3
Case "Ahmedabad", "Chandigarh", "Jaipur", "Goa"
city_check = 4
End Select
Select Case deal_type
Case "Total", "Buffet"
deal_check = 1
Case "Food"
deal_check = 2
Case "Alcohol"
deal_check = 3
Case "Coupon", "Explosive"
deal_check = 4
End Select
If cft >= 5000 Then
cft_check = 1
ElseIf cft < 5000 And cft >= 2500 Then
cft_check = 2
ElseIf cft < 2500 And cft >= 1500 Then
cft_check = 3
ElseIf cft < 1500 And cft >= 800 Then
cft_check = 4
ElseIf cft < 800 And cft >= 500 Then
cft_check = 5
ElseIf cft < 500 Then
cft_check = 6
End If
resultCalc = WorksheetFunction.Sum(city_check, deal_check, cft_check)
weekend_bookingfee = CDbl(Weekend_arr(resultCalc)) * CDbl(Disc_arr(discount_percentage / 10))
weekday_bookingfee = CDbl(Weekday_arr(resultCalc)) * CDbl(Disc_arr(discount_percentage / 10))
End Sub
还将您所有的Integer声明更改为Long,我们不在VBA中使用Integer。
答案 2 :(得分:-1)
Private Sub Button1_Click()
Dim city_name As String, deal_type As String, cft As Integer, discount_percentage As Integer, city_check As Integer, cft_check As Integer, deal_check As Integer, discount_check As Integer, weekday_bookingfee As Double, weekend_bookingfee As Double
Dim resultCalc
discount_percentage = Range("b4").Value
city_name = Range("b5").Text
deal_type = Range("b6").Text
cft = Range("b7").Value
Select Case city_name
Case "Delhi", "Mumbai", "Bangalore"
city_check = 1
Case "Hyderabad"
city_check = 2
Case "Chennai", "Kolkata", "Pune"
city_check = 3
Case "Ahmedabad", "Chandigarh", "Jaipur", "Goa"
city_check = 4
End Select
Select Case deal_type
Case "Total", "Buffet"
deal_check = 1
Case "Food"
deal_check = 2
Case "Alcohol"
deal_check = 3
Case "Coupon", "Explosive"
deal_check = 4
End Select
If cft >= 5000 Then
cft_check = 1
ElseIf cft < 5000 And cft >= 2500 Then
cft_check = 2
ElseIf cft < 2500 And cft >= 1500 Then
cft_check = 3
ElseIf cft < 1500 And cft >= 800 Then
cft_check = 4
ElseIf cft < 800 And cft >= 500 Then
cft_check = 5
ElseIf cft < 500 Then
cft_check = 6
End If
resultCalc = WorksheetFunction.Sum(city_check, deal_check, cft_check)
If (discount_percentage = 50) And (resultCalc = 3) Then
weekend_book ingfee = 80 And weekday_bookingfee = 60
ElseIf (discount_percentage = 50) And (resultCalc = 4) Then
weekend_bookingfee = 70 And weekday_bookingfee = 60
ElseIf (discount_percentage = 50) And (resultCalc = 5) Then
weekend_bookingfee = 50 And weekday_bookingfee = 40
ElseIf (discount_percentage = 50) And (resultCalc = 6) Then
weekend_bookingfee = 50 And weekday_bookingfee = 40
ElseIf (discount_percentage = 50) And (resultCalc = 7) Then
weekend_bookingfee = 35 And weekday_bookingfee = 30
ElseIf (discount_percentage = 50) And (resultCalc = 8) Then
weekend_bookingfee = 30 And weekday_bookingfee = 25
ElseIf (discount_percentage = 50) And (resultCalc = 9) Then
weekend_bookingfee = 25 And weekday_bookingfee = 20
ElseIf (discount_percentage = 50) And (resultCalc = 10) Then
weekend_bookingfee = 20 And weekday_bookingfee = 15
ElseIf (discount_percentage = 50) And (resultCalc = 11) Then
weekend_bookingfee = 20 And weekday_bookingfee = 15
ElseIf (discount_percentage = 50) And (resultCalc = 12) Then
weekend_bookingfee = 15 And weekday_bookingfee = 10
ElseIf (discount_percentage = 50) And (resultCalc = 13) Then
weekend_bookingfee = 15 And weekday_bookingfee = 10
ElseIf (discount_percentage = 50) And (resultCalc = 14) Then
weekend_bookingfee = 5 And weekday_bookingfee = 5
End If
If (discount_percentage = 40) And (resultCalc = 3) Then
weekend_bookingfee = (80 * 0.75) And weekday_bookingfee = (60 * 0.75)
ElseIf (discount_percentage = 40) And (resultCalc = 4) Then
weekend_bookingfee = (70 * 0.75) And weekday_bookingfee = (60 * 0.75)
ElseIf (discount_percentage = 40) And (resultCalc = 5) Then
weekend_bookingfee = (50 * 0.75) And weekday_bookingfee = (40 * 0.75)
ElseIf (discount_percentage = 40) And (resultCalc = 6) Then
weekend_bookingfee = (50 * 0.75) And weekday_bookingfee = (40 * 0.75)
ElseIf (discount_percentage = 40) And (resultCalc = 7) Then
weekend_bookingfee = (35 * 0.75) And weekday_bookingfee = (30 * 0.75)
ElseIf (discount_percentage = 40) And (resultCalc = 8) Then
weekend_bookingfee = (30 * 0.75) And weekday_bookingfee = (25 * 0.75)
ElseIf (discount_percentage = 40) And (resultCalc = 9) Then
weekend_bookingfee = (25 * 0.75) And weekday_bookingfee = (20 * 0.75)
ElseIf (discount_percentage = 40) And (resultCalc = 10) Then
weekend_bookingfee = (20 * 0.75) And weekday_bookingfee = (15 * 0.75)
ElseIf (discount_percentage = 40) And (resultCalc = 11) Then
weekend_bookingfee = (20 * 0.75) And weekday_bookingfee = (15 * 0.75)
ElseIf (discount_percentage = 40) And (resultCalc = 12) Then
weekend_bookingfee = (15 * 0.75) And weekday_bookingfee = (10 * 0.75)
ElseIf (discount_percentage = 40) And (resultCalc = 13) Then
weekend_bookingfee = (15 * 0.75) And weekday_bookingfee = (10 * 0.75)
ElseIf (discount_percentage = 40) And (resultCalc = 14) Then
weekend_bookingfee = (5 * 0.75) And weekday_bookingfee = (5 * 0.75)
End If
If (discount_percentage = 30 Or discount_percentage = 20) And (resultCalc = 3) Then
weekend_bookingfee = (80 * 0.5) And weekday_bookingfee = (60 * 0.5)
ElseIf (discount_percentage = 30 Or discount_percentage = 20) And (resultCalc = 4) Then
weekend_bookingfee = (70 * 0.5) And weekday_bookingfee = (60 * 0.5)
ElseIf (discount_percentage = 30 Or discount_percentage = 20) And (resultCalc = 5) Then
weekend_bookingfee = (50 * 0.5) And weekday_bookingfee = (40 * 0.5)
ElseIf (discount_percentage = 30 Or discount_percentage = 20) And (resultCalc = 6) Then
weekend_bookingfee = (50 * 0.5) And weekday_bookingfee = (40 * 0.5)
ElseIf (discount_percentage = 30 Or discount_percentage = 20) And (resultCalc = 7) Then
weekend_bookingfee = (35 * 0.5) And weekday_bookingfee = (30 * 0.5)
ElseIf (discount_percentage = 30 Or discount_percentage = 20) And (resultCalc = 8) Then
weekend_bookingfee = (30 * 0.5) And weekday_bookingfee = (25 * 0.5)
ElseIf (discount_percentage = 30 Or discount_percentage = 20) And (resultCalc = 9) Then
weekend_bookingfee = (25 * 0.5) And weekday_bookingfee = (20 * 0.5)
ElseIf (discount_percentage = 30 Or discount_percentage = 20) And (resultCalc = 10) Then
weekend_bookingfee = (20 * 0.5) And weekday_bookingfee = (15 * 0.5)
ElseIf (discount_percentage = 30 Or discount_percentage = 20) And (resultCalc = 11) Then
weekend_bookingfee = (20 * 0.5) And weekday_bookingfee = (15 * 0.5)
ElseIf (discount_percentage = 30 Or discount_percentage = 20) And (resultCalc = 12) Then
weekend_bookingfee = (15 * 0.5) And weekday_bookingfee = (10 * 0.5)
ElseIf (discount_percentage = 30 Or discount_percentage = 20) And (resultCalc = 13) Then
weekend_bookingfee = (15 * 0.5) And weekday_bookingfee = (10 * 0.5)
ElseIf (discount_percentage = 30 Or discount_percentage = 20) And (resultCalc = 14) Then
weekend_bookingfee = (5 * 0.5) And weekday_bookingfee = (5 * 0.5)
End If
End