Home > OS >  userform withe vba if statement
userform withe vba if statement

Time:09-21

Need some help please.

I have a user form that has multiple combo boxes. I then have some textboxes that contain vlookups that are working fine and one textbox that requires an "IF" statement.

The statement will come run after a weight is entered into Weighttb.

When then is done I need the "UKTB" to calculate a price for me.

This needs to read as:

if the weight is less than 35kg then calculate 3.98 (weight - 35) *0.2 else the price is 3.98

however if the postcode selected is "BT" the the calculation will be 12.88 (weight -30) * 0.6

This is how my code looks

Private Sub Weighttb_Change()

If PostCodecb = "BT" Then
UKMtb = 12.88   (Weighttb - 30) * 0.6

ElseIf Weighttb <= "35" Then
UKMtb = 3.98   (Weighttb - 35) * 0.2

Else

UKMtb = "3.98"

End If
End Sub

A calculation is being ran but it does not seem correct.

Any help?

Thank you

CodePudding user response:

Without any more info about the code, I see two things that may be influencing the "if" statement:

  • It seems that if PostCodecb = "BT", then it does not matter the value of Weighttb, right? But in that case, just for precaution, declare it in the second statement.
  • The 35 is an integer, but with the quotation marks that you are using, it transforms into a string. This goes as well for the third statement, in case you need UKMtb for further calculations.

Try using something like:

Private Sub Weighttb_Change()
    If PostCodecb = "BT" Then
        UKMtb = 12.88   (Weighttb - 30) * 0.6
    ElseIf Weighttb <= 35 and PostCodecb <> "BT" Then
        UKMtb = 3.98   (Weighttb - 35) * 0.2
    Else
        UKMtb = 3.98
    End If
End Sub

Hope it helps! If not, give me more details and I'll try to find other problems.

Mike

  • Related