473,382 Members | 1,247 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,382 developers and data experts.

VBA Data Types

MMcCarthy
14,534 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. Type        MemSize RetVal of VarType() Declaration Char   Conversion
  2. Boolean     2b      vbBoolean(11)                          CBool()
  3. Byte        1b      vbByte(17)                             CByte()
  4. Currency    8b      vbCurrency(6)       @                  CCur()
  5. Date        8b      vbDate(7)                              CDate()
  6. Decimal     14b     vbDecimal(14)                          CDec()
  7. Integer     2b      vbInteger(2)        %                  CInt()
  8. Long        4b      vbLong(3)           &                  CLng()
  9. LongLong    8b      vbLongLong(20)      ^                  CLngLng()
  10. Single      4b      vbSingle(4)         !                  CSng()
  11. Double      8b      vbDouble(5)         #                  CDbl()
  12. String      10b+    vbString(8)         $                  CStr()
  13. Object      4b      vbObject(9)
  14. Variant     16b     vbVariant(12)                          CVar()

The Boolean Data Type (True or False)
Use the Boolean numeric data type to store logical data that contains only two values: on and off, true and false, yes and no, and so on. The keywords True and False are predefined constants and are interchangeable with the values –1 and 0, respectively.

The Byte Data Type (0 to 255)
Byte is VBA's smallest numeric data type and holds a numeric value from 0 to 255. This data type doesn't support negative values. If you attempt to assign one, VBA returns an error.

The Currency Data Type
Use the Currency numeric data type to store monetary values from –922,337,203,477.5808 to 922,337,203,685,477.5807.

The Date Data Type
The Date data type stores a specially formatted numeric value that represents both the date and time. You don't have to store both the date and time value. The Date data type accepts either the date or the time, or both. Possible values range from January 1, 100 to December 31, 9999.

The Decimal Data Type
The Decimal data type is a subtype of Variant and not a truly separate data type all its own, accommodating values from –79,228,162,514,264,337,593,543,950,335 to 79,228,162,514,264,337,593,543,950,335 if the value contains no decimal places. The data type maintains precision up to 28 decimal places with values from –7.9228162514264337593543950335 to 7.9228162514264337593543950335.

The Integer Data Type
This is probably the most common data type in use, besides String. Use this data type to store only whole numbers that range from –32,768 to 32,767.

The Long Data Type
The Long data type is also an Integer data type storing only whole numbers, but the range is much larger than the traditional Integer data type. Use Long to store values from –2,147,483,648 to 2,147,483,647.

The LongLong Data Type (64-bit only)
The LongLong data type is also an Integer data type storing only whole numbers, but the range is much larger than the traditional Integer & Long data types. Use the LongLong data type to store values from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.

The Single Data Type
The Single data type stores precision numbers—numbers with decimal places or fractional numbers. The data type is similar to Double, but the range is smaller. Use this data type to store values from –3402823E38 to –1.401298E–45 or from 1.401298E–45 to 3.402823E38.

The Double Data Type
Use the Double data type to store precision floating point numbers from –1.79769313486232E308 to –4.94065645841247E-324 or 1.79769313486232E308 to 4.94065645841247E-324.

The String Data Types (Fixed & Variable lengths)
String is another very common data type; it stores values or numbers, but treats them as text. There are two varieties: fixed and variable. A fixed string can handle from 1 to 65,400 characters. The Dim for a Fixed String is of the form:
Dim variablename As String * stringlength

In contrast, the Variable String data type grows and shrinks as required to fit its stored value. By default, all String variables are of this type. To declare this type, use the Dim statement in the form
Dim variablename As String

The Object Data Type
An Object variable is actually a reference to an Access object, such as a form, report, or control. Or, the data type can reference an ActiveX component, or a class object created in a class module.

The Variant Data Type
The Variant data type stores numeric and non-numeric values. This data type is the most flexible of the bunch because it stores very large values of almost any type (matches the Double numeric data type). Use it only when you're uncertain of the data's type or when you're accommodating foreign data and you're not sure of the data type's specifications.

The Variant data type is VBA's default, so the following code interprets varValue as a Variant:
Dim varValue

Although the Variant data type is flexible, VBA processes these data types a little slower because it must determine the most accurate data type for the assigned value. However, most likely, you'll never notice the performance hit.

The biggest disadvantage is the data type's lack of readability. By that, we mean that you can't easily determine the appropriate data type by viewing the code, and that can be a problem.

Note:
Please see Matthew's post for information on updates in VBA7 for 64-bit arithmetic and pointer compatibility.

NB:
The LongLong data type has now been added in as well as a table showing some further helpful information.
Feb 13 '07 #1
8 193911
The Long Data Type
2,147,486,647 should read 2,147,483,647
(Open Windows Calculator, select scientific view, and try 2^31 - 1, to get this result. It's a 32-bit signed integer.)
Aug 2 '10 #2
MMcCarthy
14,534 Expert Mod 8TB
Thanks, I'll edit this in the original :) Probably just a type originally.

Mary
Aug 4 '10 #3
Good notes, thanks
Nov 5 '10 #4
Updates in VBA7 for 64-bit arithmetic and pointer compatibility:

http://msdn.microsoft.com/en-us/libr...ice.14%29.aspx

http://msdn.microsoft.com/en-us/libr...ice.11%29.aspx

Note, for example, the new 64-bit LongLong type and various Ptr types.
Nov 23 '10 #5
MMcCarthy
14,534 Expert Mod 8TB
Thank you for that Matthew. I've edited the original article to add a link to your post.
Nov 25 '10 #6
NeoPa
32,556 Expert Mod 16PB
I've just taken the liberty (Sorry Mary) of updating the OP of this, still very popular, thread to bring it up-to-date with the main new 64 bit Long Long data type mentioned previously in the discussion. I also took the opportunity, while I was about it, to add in a table at the top showing some often useful information about the data types included here.
Oct 23 '21 #7
svtsolutionllc
2 2Bits
Good one
Noted!
Nov 3 '21 #8
NeoPa
32,556 Expert Mod 16PB
I wanted to add a link here (Type Characters) to an article on this subject from Microsoft for further reading if required.
Dec 4 '21 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Anon Email | last post by:
Hi people, I'm learning about header files in C++. The following is code from Bartosz Milewski: // Code const int maxStack = 16; class IStack
13
by: Shailesh Humbad | last post by:
I wrote a short page as a quick reference to c++ integer data types. Any feedback welcome: http://www.somacon.com/blog/page11.php
3
by: lovecreatesbeauty | last post by:
Prof. Bjarne Stroustrup said "built-in data types have default constructor" at §10.4.2 in `TC++PL, special ed.'. He also said that "built-in data types are not classes" at §11.4 in `TC++PL,...
4
by: No One | last post by:
Here is my problem: I have a certain set of well-defined manipulations that I have to apply to different types of data. In all cases the manipulations are exactly the same, and are to be...
11
by: theshowmecanuck | last post by:
As a matter of academic interest only, is there a way to programmatically list the 'c' data types? I am not looking for detail, just if it is possible, and what function could be used to...
8
by: ramu | last post by:
Hi, I want to call a vc++ function from a c program on linux. So when I do this dosen't the VC++ datatypes differ with c datatypes. Because we don't have some vc++ data types in c. I have to...
11
by: mesut demir | last post by:
Hi All, When I create fields (in files) I need assign a data type like char, varchar, money etc. I have some questions about the data types when you create fields in a file. What is the...
4
by: mshetty | last post by:
Hi, We need a list of all VC++ Data Types. Searched through MSDN but could not find a single list. Since we are new to VCC+ we really don't know if what we are getting is sufficient or there is...
15
by: Madhur | last post by:
Hi All, I would like you help me in creating an array of data types. I am interested in look at the the data type which looks like this Array...
7
by: Maximus Decimus | last post by:
HI all, I am using python v2.5 and I am an amateur working on python. I am extending python for my research work and would like some help and guidance w.r.t this matter from you experienced...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.