Long Data Type in Excel VBA

VBA Long Data Type in Excel

Long Data Type in Excel VBA explained with syntax and examples. We use the long VBA data type to store whole integer numbers. It is an alternative to integer. It occupies more range than integers. The default value is 0. The long VBA data type contains range of values between -2, 147, 483, 647 to 2, 147, 483, 647. Integers cannot have decimal places. The long number can be positive number, negative number or zero. It is most commonly used VBA data type. It occupies 4 bytes(32 bits) in memory. One byte represents the sign(positive or negative). The long VBA data type preceded with either positive(+) or negative(-) sign. Long does the fastest performance.

Long VBA Data Type Syntax in Excel

Let us see the Data Type Long Syntax in Excel VBA.

Dim VariableName as Long

Where VariableName represents the name of the variable.
and Long represents the type of data.

Example on Long VBA Data Type in Excel

Here is an example on the Long VBA Data Type in Excel. The below example returns the number of rows in Sheet1.

Sub VBAF1_Long_DataType()
    
    'Integer Variable Declaration
    Dim iValue As Long
    
    'Assign value to integer variable
    iValue = Worksheets("Sheet1").Rows.Count
    
    MsgBox "Number of Rows: " & iValue, vbInformation, "VBAF1"
     
End Sub

Here is the screenshot of above macro output.

Long VBA Data Type

Example on Long VBA Data Type in Excel

Here is an example on the Long VBA Data Type in Excel. The below example returns the overflow error, because the number of cells in excel are 17,179,869,184 cells. But long range is 2, 147, 483, 647.
Note: The number of cells: 1,048,576 rows × 16,384 columns = 17,179,869,184 cells

Sub VBAF1_Long_DataType_Example()
    
    'Integer Variable Declaration
    Dim iValue As Long
    
    'Assign value to integer variable
    iValue = Worksheets("Sheet1").Cells.Count
    
    MsgBox "Number of Cells: " & iValue, vbInformation, "VBAF1"
     
End Sub

Here is the screenshot of above macro output.

VBA OverFlow Error

Convert an Expression to Long Data Type in Excel VBA

You can convert an expression to Long VBA data type using VBA CLng function. Click on the following link to learn about CLng function and complete tutorial and examples.

VBA CLng Function

VBA Data Types

Also read about all other data types.

VBA Data Types

Instructions to Run VBA Macro Code or Procedure:

You can refer the following link for the step by step instructions.

Instructions to run VBA Macro Code

Other Useful Resources:

Click on the following links of the useful resources. These helps to learn and gain more knowledge.

VBA Tutorial VBA Functions List VBA Arrays VBA Text Files VBA Tables

VBA Editor Keyboard Shortcut Keys List VBA Interview Questions & Answers Blog

Leave a Comment