VBA Variable types?

Jarodjp42

New Member
Joined
Nov 21, 2017
Messages
35
I was hoping someone can shed some more light on variable types in VBA for me.

I understand that integer & long integer are for numbers only but when it comes to the rest like string, fixed string, variant, boolean, object, & Byte I am a little more confused.

I have written a formula before and used a variable type as string but was getting an error message. I changed the variable type to variant and the code worked. Not sure why it worked and why I needed to use the variant type in the code.

How do you know which variable type to use in your VBA coding?

Please help if you can, Thanks! :)
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
It all depends on what you expect to return with your variables, and the integrity of your data. If there is a possibility to return errors (because you have bad data), you may need to use Variant.
However, your don't want to use it all the time. These errors can actually help you identify data issues (i.e. if you expect to get a number and return text instead).
Generally, you want to use the data type that you expect to return, and add error handling to your code to handle errors.

I have written a formula before and used a variable type as string but was getting an error message.
If you can provide an example of this, we may be able to tell you why or where you may have gone wrong.
 
Upvote 0

Forum statistics

Threads
1,215,048
Messages
6,122,862
Members
449,097
Latest member
dbomb1414

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top