0

Hi just start to learn VBA here. The additions works just fine. However, division won't work. It's printing msgbox like the title above any solution?

Sub var_test1()

    Dim lngT    As Long
    Dim lngA    As Long
    Dim lngB    As Long
    Dim strA    As String
    
    lngA = 10
    lngB = 20
    strA = " is "    
    
    lngT = lngA + lngB
    MsgBox lngA & "+" & lngB & strA & lngT
    
    lngT = lngA / lngB
    MsgBox lngA & "/" & lngB & strA & lngT    
    
End Sub
1
  • 1
    Division works and the result is correct. 10 divided by 20 is 0 with a remainder of 10. If you want to have the remainder you need to use the MOD operator. But this is basic mathematics. If you want to use another number range you have to use another data type for your variables. Commented Aug 10, 2021 at 6:31

1 Answer 1

1

Your function runs fine here, but the result by division is zero because of the integer data type.

So, use a data type that allows decimals. Currency is well suited if you need no more than four decimals:

Sub var_test1()

    Dim Result  As Currency
    Dim lngA    As Long
    Dim lngB    As Long
    Dim strA    As String
    
    lngA = 10
    lngB = 20
    strA = " is "
    
    Result = lngA + lngB
    MsgBox lngA & "+" & lngB & strA & Result
    
    Result = lngA / lngB
    MsgBox lngA & "/" & lngB & strA & Result
    
End Sub

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.