Excel VBA会员费计算器帮助

时间:2019-05-04 10:35:31

标签: excel vba

我的课程项目需要准备以下程序。 这是体育俱乐部的会员费计算程序。费用变化如下。这应该没有任何日期,年或月功能。

2014年1月 至 2014-02 ---> $ 100美元(2014年是Year 01,是月)

2014年03月 至 2018年02月---> $ 120

2018年03月 至 2019 -------->-$ 150

https://i.imgur.com/TTWLz9m.jpg?1

我可以为上述程序创建一个变量(x1),以计算任意年份范围内的月份。

X1 = (((Val(C.Value) - 1) - Val(A.Value)) * 12) + (12 - Val(B.Value) + Val(D.Value) + 1)

计算从2014年开始。如果从2014-01或2014-02开始,则应在A1中显示。如果结束年份大于2014,则A1值应显示200, 其余的其他值应显示在B1中,如下图所示。 (表示,当年末值(C)> 2014时,计算 停止并在A1中显示该值,然后继续进行其他计算,其余部分应在B1中显示) https://i.imgur.com/Se0CFyI.jpg?1

问题:如果C值> 2018和D值> 02,我无法停止B1值

问题:我如何才能在Excel VBA中修改此程序,直到2018年2月才正确,但是在2018-03年和2019年之后导致错误的答案。

Private Sub CommandButton1_Click()

A1.Value = 0
B1.Value = 0
D1.Value = 0

X1 = (((Val(C.Value) - 1) - Val(A.Value)) * 12) + (12 - Val(B.Value) + Val(D.Value) + 1)
X2 = (((Val(C.Value) - 1) - Val(A.Value)) * 12) + (10 - Val(B.Value) + Val(D.Value) + 1)
X3 = (((Val(C.Value) - 1) - Val(A.Value)) * 12) + (11 - Val(B.Value) + Val(D.Value))
X4 = (((Val(C.Value) - 1) - Val(A.Value)) * 12) + (12 - Val(B.Value) + Val(D.Value) + 1) - Val(D.Value) + 2
X11 = (((Val(C.Value) - 1) - Val(A.Value)) * 12) + (12 - Val(B.Value) + Val(D.Value) + 1) - Val(D.Value) + 1
X5 = (((Val(C.Value) - 1) - Val(A.Value)) * 12) + 12
X6 = (((Val(C.Value) - 1) - Val(A.Value)) * 12) + 11
X7 = (((Val(C.Value) - 1) - Val(A.Value)) * 12) + (12 - Val(B.Value) + Val(D.Value)) - Val(D.Value) + 1
X8 = (((Val(C.Value) - 1) - Val(A.Value)) * 12) + (12 - Val(B.Value) + Val(D.Value)) - Val(D.Value) + 2
X9 = (((Val(C.Value) - 1) - Val(A.Value)) * 12) + (12 - Val(B.Value) + Val(D.Value) + 1) - Val(D.Value) + 1
X10 = (((Val(C.Value) - 1) - Val(A.Value)) * 12) + (12 - Val(B.Value) + Val(D.Value) + 1) - Val(D.Value) + 2

Y1 = ((Val(A.Value)) * 0) + (Val(B.Value))

'2014 January and February ===========================================================

If Val(A.Value) = 2014 And Val(B.Value) = 1 And Val(C.Value) > 2014 And Val(D.Value) = 1 Then
    A1.Value = 2 * Y1 * 100
End If

If Val(A.Value) = 2014 And Val(B.Value) = 1 And Val(C.Value) = 2014 And Val(D.Value) = 1 Then
    A1.Value = Y1 * 100
End If

If Val(A.Value) = 2014 And Val(B.Value) = 1 And Val(C.Value) >= 2014 And Val(D.Value) >= 2 Then
    A1.Value = 2 * Y1 * 100
End If

If Val(A.Value) = 2014 And Val(B.Value) = 2 And Val(C.Value) >= 2014 And Val(D.Value) <= 12 Then
    A1.Value = Y1 / 2 * 100
End If

'===================================================================================================

'2014- March to 2018 - February

If Val(A.Value) = 2014 And Val(B.Value) = 1 And Val(C.Value) < 2018 And Val(D.Value) >= 1 Then
    B1.Value = (X2 * 120)
End If

If Val(A.Value) = 2014 And Val(B.Value) = 2 And Val(C.Value) < 2018 And Val(D.Value) >= 1 Then
    B1.Value = (X3 * 120)
End If

If Val(A.Value) = 2014 And Val(B.Value) >= 3 And Val(C.Value) < 2018 And Val(D.Value) >= 1 Then
    B1.Value = (X1 * 120)
End If

If Val(A.Value) > 2014 And Val(B.Value) >= 1 And Val(C.Value) < 2018 And Val(D.Value) >= 1 Then
    B1.Value = (X1 * 120)
End If

If Val(A.Value) > 2014 And Val(B.Value) >= 1 And Val(C.Value) >= 2018 And Val(D.Value) >= 2 Then
    B1.Value = (X4 * 120)
End If

If Val(A.Value) > 2014 And Val(B.Value) >= 1 And Val(C.Value) >= 2018 And Val(D.Value) = 1 Then
    B1.Value = (X11 * 120)
End If

If Val(A.Value) = 2014 And Val(B.Value) = 1 And Val(C.Value) = 2018 And Val(D.Value) = 1 Then
    B1.Value = (X2 * 120)
End If

If Val(A.Value) = 2014 And Val(B.Value) = 1 And Val(C.Value) = 2018 And Val(D.Value) = 2 Then
    B1.Value = (X3 * 120)
End If

If Val(A.Value) = 2014 And Val(B.Value) = 2 And Val(C.Value) = 2018 And Val(D.Value) = 1 Then
    B1.Value = (X6 * 120)
End If

If Val(A.Value) = 2014 And Val(B.Value) = 2 And Val(C.Value) = 2018 And Val(D.Value) = 2 Then
    B1.Value = (X5 * 120)
End If

If Val(A.Value) = 2014 And Val(B.Value) >= 3 And Val(C.Value) = 2018 And Val(D.Value) = 1 Then
    B1.Value = (X9 * 120)
End If

If Val(A.Value) = 2014 And Val(B.Value) >= 3 And Val(C.Value) = 2018 And Val(D.Value) >= 2 Then
    B1.Value = (X10 * 120)
End If

If Val(A.Value) = 2014 And Val(B.Value) = 1 And Val(C.Value) = 2018 And Val(D.Value) > 2 Then
    B1.Value = (X7 * 120)
End If

If Val(A.Value) = 2014 And Val(B.Value) = 2 And Val(C.Value) = 2018 And Val(D.Value) >= 3 Then
    B1.Value = (X8 * 120)
End If

'===================================================================================================
Me.Answer = (Me.A1 + 0) + (Me.B1 + 0) + (Me.D1 + 0)

End Sub

1 个答案:

答案 0 :(得分:0)

我不确定是否禁止CDate和IsDate。我还假定> =“ 2018-03-01”的任何日期均为$ 150,尽管您可以将其更改为。如果这些是可以接受的假设。我已将“ -01”串联起来以与您的演示文稿保持一致,但也许您也可以直接附加而无需该步骤。

Option Explicit
Public Sub test()
    Dim x As String, var As String
    var = "2014-01"
    var = var & "-01"
    If Not IsDate(CDate(var)) Then
        Debug.Print "please ensure valid input"
        Exit Sub
    End If
    Select Case True
    Case var >= CDate("2018-03" & "-01")
        x = "$150"
    Case var >= CDate("2014-03" & "-01")
        x = "$120"
    Case var >= CDate("2014-01" & "-01")
        x = "$100"
    Case Else
        x = "undefined"
    End Select
    Debug.Print x
End Sub