Option Explicit
Sub CalculateAndB()
Dim ColA as Integer
Dim ColB as Integer
Dim Result as Integer
ColA = Range(“A1”).Value
ColB = Range(“B1”).Value
Result = ColA*ColB ‘Multiply value of Range A1 and B1
Msgbox (Result)
End Sub
Now copy the above code into your module and try.
1. Input 8 into Range A1 and 100 into Range B1 and Run the macro
Your message box will prompt 800 (No problem because still within integer range)
2. Input 80 into Range A1 and 1000 into Range B1 and Run the macro
You will receive error message “Run time error 6 Overflow”. This is because after multiply result will be 80,000 (Out of integer range)
From the above example you should declare “Result” as Long instead of integer because Long can accept data range in between -2,147,483,648 to 2,147,483,648
Repeat the above testing but this time changed
Dim Result as Integer ---à Dim Result as Long
Sometimes we tend to forgot to declare if we have too many variables. As a reminder you can use “Option Explicit” statement above Sub or 1st line in a module. This will force you declare.
Click here if you have any new project! Let us create for u for free.
Thanks
Sub CalculateAndB()
Dim ColA as Integer
Dim ColB as Integer
Dim Result as Integer
ColA = Range(“A1”).Value
ColB = Range(“B1”).Value
Result = ColA*ColB ‘Multiply value of Range A1 and B1
Msgbox (Result)
End Sub
No comments:
Post a Comment