为什么我的 if 语句会扭曲我的用户表单增加百分比?
Why is my if statement skewing my % increase in userform?
有一个 if 语句来计算输入数字中的百分比 increase/decrease。
If NewPayRate > 100 Then
NewPayRate = NewPayRate / HrsPerYr
Else
End If
这样做的目的是,如果有人输入 $/hr 数字并且新的工资率按年计算,它会将新的工资率转换为小时数并准确显示增长百分比。
当我添加上面的 IF 语句时,我得到:
黄色部分不应该显示,这显然是错误的。我错过了什么?其余代码:
Private Sub txtNewPayRate_Change()
Const HrsPerYr As Long = 2080 'Work Hours in Year
Dim CurrentPayRate As Double, NewPayRate As Double
Dim PercentChange As Double
On Error Resume Next
CurrentPayRate = CDbl(Me.txtCurrentPayRate) 'current hourly/annual pay
NewPayRate = CDbl(Me.txtNewPayRate) 'new hourly/annual pay
If NewPayRate > 100 Then
NewPayRate = NewPayRate / HrsPerYr
Else
End If
PercentChange = (NewPayRate - CurrentPayRate) / CurrentPayRate
txtPercentage.Value = Format(PercentChange, "0.00%")
If Me.cmbHourlyAnnual = "Hourly" Then
Me.txtNewHourlyPay = Format(NewPayRate, "0.00")
Me.txtNewAnnualPay = CStr(NewPayRate * HrsPerYr)
ElseIf Me.cmbHourlyAnnual = "Annual" Then
Me.txtNewHourlyPay = Format(NewPayRate / HrsPerYr, "0.00")
Me.txtNewAnnualPay = CStr(NewPayRate)
End If
End Sub
我会将所有内容转换为每小时,然后从那里开始
Private Sub txtNewPayRate_Change()
Dim rawCurrentPayRate As Double
Dim hrlyCurrentPayRate As Double
Dim hrlyNewPayRate As Double
Const HrsPerYr As Long = 2080 'Work Hours in Year
rawCurrentPayRate = CDbl(Me.txtCurrentPayRate.Value)
If CDbl(Me.txtNewPayRate.Value) < 100 Then
hrlyNewPayRate = CDbl(Me.txtNewPayRate.Value)
Else
hrlyNewPayRate = CDbl(Me.txtNewPayRate.Value) / HrsPerYr
End If
If Me.cmbHourlyAnnual = "Hourly" Then
hrlyCurrentPayRate = rawCurrentPayRate
Else
hrlyCurrentPayRate = rawCurrentPayRate / HrsPerYr
End If
Me.txtNewHourlyPay = Format(hrlyNewPayRate, "0.00")
Me.txtNewAnnualPay = Format(hrlyNewPayRate * HrsPerYr, "#,##0.00")
Me.txtPercentage = Format((hrlyNewPayRate - hrlyCurrentPayRate) / hrlyCurrentPayRate, "0.00%")
End Sub
有一个 if 语句来计算输入数字中的百分比 increase/decrease。
If NewPayRate > 100 Then
NewPayRate = NewPayRate / HrsPerYr
Else
End If
这样做的目的是,如果有人输入 $/hr 数字并且新的工资率按年计算,它会将新的工资率转换为小时数并准确显示增长百分比。
当我添加上面的 IF 语句时,我得到:
黄色部分不应该显示,这显然是错误的。我错过了什么?其余代码:
Private Sub txtNewPayRate_Change()
Const HrsPerYr As Long = 2080 'Work Hours in Year
Dim CurrentPayRate As Double, NewPayRate As Double
Dim PercentChange As Double
On Error Resume Next
CurrentPayRate = CDbl(Me.txtCurrentPayRate) 'current hourly/annual pay
NewPayRate = CDbl(Me.txtNewPayRate) 'new hourly/annual pay
If NewPayRate > 100 Then
NewPayRate = NewPayRate / HrsPerYr
Else
End If
PercentChange = (NewPayRate - CurrentPayRate) / CurrentPayRate
txtPercentage.Value = Format(PercentChange, "0.00%")
If Me.cmbHourlyAnnual = "Hourly" Then
Me.txtNewHourlyPay = Format(NewPayRate, "0.00")
Me.txtNewAnnualPay = CStr(NewPayRate * HrsPerYr)
ElseIf Me.cmbHourlyAnnual = "Annual" Then
Me.txtNewHourlyPay = Format(NewPayRate / HrsPerYr, "0.00")
Me.txtNewAnnualPay = CStr(NewPayRate)
End If
End Sub
我会将所有内容转换为每小时,然后从那里开始
Private Sub txtNewPayRate_Change()
Dim rawCurrentPayRate As Double
Dim hrlyCurrentPayRate As Double
Dim hrlyNewPayRate As Double
Const HrsPerYr As Long = 2080 'Work Hours in Year
rawCurrentPayRate = CDbl(Me.txtCurrentPayRate.Value)
If CDbl(Me.txtNewPayRate.Value) < 100 Then
hrlyNewPayRate = CDbl(Me.txtNewPayRate.Value)
Else
hrlyNewPayRate = CDbl(Me.txtNewPayRate.Value) / HrsPerYr
End If
If Me.cmbHourlyAnnual = "Hourly" Then
hrlyCurrentPayRate = rawCurrentPayRate
Else
hrlyCurrentPayRate = rawCurrentPayRate / HrsPerYr
End If
Me.txtNewHourlyPay = Format(hrlyNewPayRate, "0.00")
Me.txtNewAnnualPay = Format(hrlyNewPayRate * HrsPerYr, "#,##0.00")
Me.txtPercentage = Format((hrlyNewPayRate - hrlyCurrentPayRate) / hrlyCurrentPayRate, "0.00%")
End Sub