REAL-TIME

VBA Projects

Full Access with Source Code
  • Designed and Developed by PNRao

  • Full Access with VBA Source Code

  • Well Commented Codes Lines

  • Creative and Professional Design

Effortlessly
Manage Your Projects

120+ Project Management Templates

Seamlessly manage your projects with our powerful & multi-purpose templates for project management.

120+ PM Templates Includes:
  • 50+ Excel Templates

  • 50+ PowerPoint Templates

  • 25+ Word Templates

Share Post

The VBA CLngPtr function is a conversion function used to convert a given expression into a long integer data type. The function returns a 64-bit signed integer value, which allows for a greater range of numbers to be stored and accessed compared to the standard long data type. The CLngPtr function is particularly useful for handling large numbers or calculations that have the potential to exceed the limitations of the standard long data type.

VBA CLngPtr Function – Purpose, Syntax and Arguments

Syntax:

CLngPtr(expression)

Arguments:

  • expression: Required. Any valid expression that evaluates to a numerical value.

Example:

For example, suppose we have a large dataset with numbers that exceed the limitations of the standard long data type. We can use the CLngPtr function to convert these numbers into a long integer data type and perform calculations with them.

Dim rng as Range
Dim i as LongPtr
Dim sum as LongPtr
set rng = Range("A1:A10000")
For Each i in rng
sum = sum + CLngPtr(i.Value)
Next i
MsgBox sum

In this example, we have a dataset in range A1:A10000, and we want to calculate the sum of all the values in this range. Since the numbers in this dataset exceed the limitations of the standard long data type, we use the CLngPtr function to convert them to long integer data type before adding them to the sum. Without the CLngPtr function, this calculation would not be possible.

Remarks and Important Notes:

  • The CLngPtr function was introduced in Microsoft Excel 2013 and is not available in older versions.
  • The CLngPtr function can only be used in 64-bit versions of Microsoft Excel.
  • The CLngPtr function is preferred over the standard CLng function when dealing with large numbers or calculations involving numbers that exceed the limitations of the standard long data type.
  • The range of numbers that can be stored and accessed using the CLngPtr function is -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
  • If an expression provided to the CLngPtr function results in an error, the function returns a zero value.

In conclusion, the CLngPtr function is a helpful tool in VBA for dealing with large numbers and calculations that surpass the limitations of the standard long data type. It allows for greater flexibility and accuracy in handling numerical data, making it a valuable addition to any VBA programmer’s toolbox.

Understanding VBA CLngPtr Function with Examples

Basic Usage

The CLngPtr function is a type conversion function in VBA that converts a given expression or value into a long integer data type. The function takes in the argument as a variant data type and returns a long integer result. The basic syntax of the CLngPtr function is as follows:

CLngPtr(expression)

An expression can be any valid VBA expression or value that can be converted to a long integer. Let’s look at an example to understand the function better:

  1. Open a new Excel workbook.
  2. Press ALT + F11 to open the VBA editor.
  3. Insert a new module by clicking on Insert > Module.
  4. In the module, enter the following code:
Dim num As Variant
Dim result As Long
num = 5.67
result = CLngPtr(num)
MsgBox "The converted value is: " & result

When you run this code, the value of the variable “num” is first declared as a variant data type. Then, the CLngPtr function is used to convert the value of “num” to a long integer and store it in the variable “result”. Finally, a message box is displayed showing the converted value. In this case, the output would be 5, as the decimal portion of the number is truncated when converted to a long integer.

Handling Null Values

The CLngPtr function can also handle null values when converting to a long integer. Null values represent an empty or non-existent value. Let’s look at an example:

Dim num As Variant
Dim result As Long
num = Null
result = CLngPtr(num)
MsgBox "The converted value is: " & result

In this example, the variable “num” is assigned a null value. When the CLngPtr function is applied, it returns a result of 0, as null values are considered to be 0 when converted to a long integer.

Overflow Error

One thing to note about the CLngPtr function is that it has a limited range of values that it can convert. If the value exceeds this range, an overflow error is generated. Let’s look at an example:

Dim num As Variant
Dim result As Long
num = 50000
result = CLngPtr(num)
MsgBox "The converted value is: " & result

In this example, the value of “num” is larger than the maximum value that can be stored in a long integer data type. When the CLngPtr function is applied, an overflow error will occur as the value cannot be converted to a long integer.

Using the “DataType” Argument

The CLngPtr function also has a “DataType” argument that allows the user to specify the data type of the expression being converted. The syntax of the CLngPtr function when using this argument is:

CLngPtr(expression, DataType)

The “DataType” can be specified as “vbInteger” or “vbLong”. By default, if the “DataType” argument is not specified, the function will convert the expression to a long integer. Let’s look at an example of using the “DataType” argument:

Dim num As Variant
Dim result As Long
num = 500
result = CLngPtr(num, vbInteger)
MsgBox "The converted value is: " & result

In this example, we have specified the “DataType” argument as “vbInteger”, which indicates that we want the expression to be converted to an integer instead of a long integer. This will return a result of 500 in the message box.

Using the “ByRef” and “ByVal” Keywords

The CLngPtr function also allows you to declare whether the argument should be passed “ByRef” or “ByVal”. If no keyword is specified, the argument is passed “ByRef” by default. Let’s look at an example of using “ByVal”:

Function DoubleValue(ByVal x As Long) As Long
DoubleValue = x * 2
End Function
Sub Example()
Dim num As Long
Dim result As Long
num = 10
result = DoubleValue(num)
MsgBox "The doubled value is: " & result
End Sub

In this example, we have declared the function “DoubleValue” with the “ByVal” keyword. This means that when the function is called, the value of “num” will be passed as an argument instead of its reference. Therefore, changing the value of “num” within the function will not affect the original value outside of the function. In this case, the output would be 20, as the value of “num” is passed as an argument and is not changed within the function.

Conclusion

The CLngPtr function is a very useful function in VBA for converting values to long integers. It is especially useful when dealing with large amounts of data or calculations. By understanding the different examples and usage of the function, you can efficiently use it in your VBA projects.

Effortlessly Manage Your Projects and Resources
120+ Professional Project Management Templates!

A Powerful & Multi-purpose Templates for project management. Now seamlessly manage your projects, tasks, meetings, presentations, teams, customers, stakeholders and time. This page describes all the amazing new features and options that come with our premium templates.

Save Up to 85% LIMITED TIME OFFER
Project Management Templates

All-in-One Pack
120+ Project Management Templates

Essential Pack
50+ PM Templates

Excel Pack
50+ Excel PM Templates

PowerPoint Pack
50+ Excel PM Templates

MS Word Pack
25+ Word PM Templates

Ultimate Project
Management Template
Ultimate Resource
Management Template
Project Portfolio
Management Templates
Categories: VBA FunctionsTags: , , , Last Updated: September 30, 2023

Leave A Comment