如何声明变量并汇总变量值

时间:2019-07-04 09:24:00

标签: excel vba addition

嗨,我想对这3个变量求和,然后将总和值自动填充到另一个变量-Excel上的txtTotalMonthly Salary。

  1. txtLastMonthlyDrawnSalary
  2. txtAWS
  3. txtAllowance

有人可以建议吗?

Private Sub txtLastMonthlyDrawnSalary_Change()

If Me.txtLastMonthlyDrawnSalary.Value <> "" Then
    Dim Product As Long
    Product = Me.txtLastMonthlyDrawnSalary.Value * 12

    Me.txtLastAnnualDrawnSalary.Value = Product
End If

If Me.txtLastMonthlyDrawnSalary.Value <> "" Then
    Dim Sum As Long

  Me.txtLastMonthlyDrawnSalary.Value Me.txtAWS.Value + Me.txtAllowance.Value = Sum

End If

End Sub

1 个答案:

答案 0 :(得分:0)

首先,作为一种好的做法,我将在一开始就声明所有变量。

所以从DIM产品ect开始... 还有,“我”是什么

此外,如果txtlastmonthdrawnsalary为数字,则无需在末尾添加“ .value”。

为避免任何可能的错误,我在变量末尾添加了“ z”,以区别于VB coomands。

我将从以下内容开始,如果仍然无法解决问题,让我们看看它的中断之处:

Private Sub txtLastMonthlyDrawnSalary_Change()
    Dim Productz As Long, Sumz As Long, txtLastMonthlyDrawnSalary as long, txtLastAnnualDrawnSalary as long

    If not txtLastMonthlyDrawnSalary = "" Then

        Productz = txtLastMonthlyDrawnSalary * 12

        txtLastAnnualDrawnSalary = Productz
    End If

    If not txtLastMonthlyDrawnSalary = "" Then


      sumz = txtLastMonthlyDrawnSalary +  txtAWS + txtAllowance 

    End If

    End Sub