Table of Contents
Converting a string to a long in VBA is the process of changing a string variable into a numerical long data type. This is useful when working with data that needs to be stored or manipulated as numbers rather than text. To convert a string to a long in VBA, the built-in function CLng() can be used. This function takes a string value as input and returns a long integer. Some examples of converting a string to a long in VBA include converting a user-inputted string to a long for mathematical operations, converting a string representing a date to a long for date calculations, or converting a string representing a phone number to a long for sorting purposes. It is important to ensure that the string being converted can be represented as a long integer, otherwise an error may occur.
Convert String to Long in VBA (With Examples)
You can use the CLng function in VBA to convert a text string to a long data type.
Here are two common ways to use this function in practice:
Method 1: Convert String to Long in VBA
Sub ConvertStringToLong() Dim i As Integer For i = 2 To 11 Range("B" & i) = CLng(Range("A" & i)) Next i End Sub
This particular macro will convert each string in the range A2:A11 to a long data type and display the long values in the range B2:B11.
Method 2: Convert String to Long in VBA (Only if String is a Number)
Sub ConvertStringToLong()
Dim i As Integer
For i = 2 To 11
If IsNumeric(Range("A" & i)) Then
Range("B" & i) = CLng(Range("A" & i))
Else
Range("B" & i) = 0
End IfNext i
End Sub
This particular macro will convert each string in the range A2:A11 to a long data type only if the string is a number. Otherwise, the string will be converted to a value of zero.
The following examples show how to use each method in practice.
Example 1: Convert String to Long in VBA
Suppose we have the following column of values in Excel that are currently formatted as text strings:

Suppose we would like to convert each string to a long data type and display it in column B.
We can create the following macro to do so:
Sub ConvertStringToLong() Dim i As Integer For i = 2 To 11 Range("B" & i) = CLng(Range("A" & i)) Next i End Sub
When we run this macro, we receive the following output:

Example 2: Convert String to Long in VBA (Only if String is a Number)
Suppose we have the following column of values in Excel that are currently formatted as text strings:

Suppose we would like to convert each string to a long data type only if the string is a number and display them in column B.
We can create the following macro to do so:
Sub ConvertStringToLong()
Dim i As Integer
For i = 2 To 11
If IsNumeric(Range("A" & i)) Then
Range("B" & i) = CLng(Range("A" & i))
Else
Range("B" & i) = 0
End IfNext i
End SubWhen we run this macro, we receive the following output:

Notice that only the text strings in column A that are numbers are converted to long data types in column B.
Otherwise, the text strings are simply converted to a value of zero.
Note: You can find the complete documentation for the VBA CLng function .
Cite this article
stats writer (2024). How can I convert a string to a long in VBA and what are some examples of how to do so?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-convert-a-string-to-a-long-in-vba-and-what-are-some-examples-of-how-to-do-so/
stats writer. "How can I convert a string to a long in VBA and what are some examples of how to do so?." PSYCHOLOGICAL SCALES, 24 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-convert-a-string-to-a-long-in-vba-and-what-are-some-examples-of-how-to-do-so/.
stats writer. "How can I convert a string to a long in VBA and what are some examples of how to do so?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-convert-a-string-to-a-long-in-vba-and-what-are-some-examples-of-how-to-do-so/.
stats writer (2024) 'How can I convert a string to a long in VBA and what are some examples of how to do so?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-convert-a-string-to-a-long-in-vba-and-what-are-some-examples-of-how-to-do-so/.
[1] stats writer, "How can I convert a string to a long in VBA and what are some examples of how to do so?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can I convert a string to a long in VBA and what are some examples of how to do so?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
