Search code examples
vbauserform

IF statement for % change in userform?


I have a sub, which shows the % difference between a current hourly/annual pay & new hourly/annual pay.

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
            NewPayRate
        End If         'new hourly/annual pay
    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

I come across a percentchange mishap when this scenario happens:

employee enters in an hourly rate in CurrentPayRate but then an annual figure in the NewPayRate section.

example: employee enters $25/hr in the current, but then enters $60,000 in the new pay rate because they're moving to an annual figure. This allows the % change to be something like 1000000% increase.

Unsure if this is doable to compare it and properly show the increase as if it were an hourly rate (even if an annual rate is typed in?)


Solution

  • Since most likely nobody is paid more than idk.. 5000$/hour, you can add an if loop that determine if the entry is an hourly vs annualy rate :

    If Newpayrate > x 'the max hourly rate you think about
     Newpayrate = Newpayrate/HrsPerYr
    else 
    Endif