EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 120+ Courses All in One Bundle
  • Login
Home VBA VBA Resources VBA Conversion Functions VBA CDBL
Secondary Sidebar
VBA Conversion Functions
  • VBA Conversion Functions
    • VBA CDEC
    • VBA CLng
    • VBA Value
    • VBA CHR
    • VBA CDBL
    • VBA Val
    • VBA CSTR
    • VBA CDate
    • VBA Double
    • VBA CInt

VBA CDBL

By Madhuri ThakurMadhuri Thakur

VBA CDBL

What is Excel VBA CDBL?

CDBL is a VBA function that uses to convert the expressions into a double data type. VBA consists of a number of data conversion functions. This help to change the data type of a variable or value from one to another. In different calculations, the conversion is a necessary process to get the proper result. CDBL is one of the data conversion function included in the Type conversion functions in VBA. CDBL stands for ‘Convert to double’. While processing data you may come across situations to change the integer numbers to double. To print the accurate result this function will help you. VBA CDBL function only accepts numbers. Any expression apart from numbers will produce a type mismatch error.

Format of CDBL function in Excel VBA

CDBL is a simple function which requires a single expression to operate in VBA. The expression should be a number.

Syntax of CDBL

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

  • Expression: Is the number you want to convert into a floating number.

If the expression is anything apart from a number, the function will return a mismatch error. See the below example, where a non-number expression is passed to the function CDBL and it produced an error type mismatch.

All in One Financial Analyst Bundle(250+ Courses, 40+ Projects)
Financial ModelingInvestment BankingUS GAAPCFA-Level 1 & 2
Equity ResearchM & A ModelingPrivate Equity ModelingForex Trading
Price
View Courses
250+ Online Courses | 40+ Projects | 1000+ Hours | Verifiable Certificates | Lifetime Access
4.9 (86,580 ratings)

CDBL 1

Use of CDBL Function in VBA

Let’s check how the VBA CDBL function helps us on data processing. To get a clear view and understand the practical use of changing the data type see the below example. You can see the difference according to the data type used on the same variable.

  • Declare a variable as integer data type and assign a floating value to it. While printing the value see how it’s going to show the value.

Code:

Private Sub addconvert()

Dim b As Integer

End Sub

VBA CDBL Example 1-1

  • A floating value is assigned to the declared integer variable. 785.456923785475 is assigned to variable b which is an integer variable.

Code:

Private Sub addconvert()

Dim b As Integer
b = 785.456923785475

End Sub

VBA CDBL Example 1-2

  • Using a message box, print the assigned value.

Code:

Private Sub addconvert()

Dim b As Integer
b = 785.456923785475
MsgBox b

End Sub

VBA CDBL Example 1-3

See how the results show difference while printing the value.

Result of Example 1-4

You have assigned a number with floating values but the output shows only the integer part of the given number. Here the variable ‘b’ is declared as integer number so it will not accept a floating value. The integer part of the decimal value is taken and the decimal part is avoided. Since the variable is declared as an integer the number will be rounded to the nearest integer.

To print the data as it is, changing the variable data type into double Instead of the integer data type.

  • By making a simple change in the same code you can print the given number as it is. Declare the variable as a double datatype.

Code:

Private Sub addconvert()

Dim b As Double

End Sub

VBA CDBL Example 1-5

  • A number as double is assigned to the declared double variable. 785.456923785475 is assigned to variable b which is double in datatype.

Code:

Private Sub addconvert()

Dim b As Double
b = 785.456923785475

End Sub

VBA CDBL Example 1-6

  • Using a message box print the assigned value.

Code:

Private Sub addconvert()

Dim b As Double
b = 785.456923785475
MsgBox b

End Sub

VBA CDBL Example 1-7

See how the results show difference while printing the value. You have assigned a number with floating values.

Result of Example 1-8

Compare both outputs you can see the difference. Even the data and variable are the same the change in data type changed the entire output. From this, you will get the importance of datatype and how it influences the entire program. This is the reason for using a different type of conversion functions with VBA.

Examples of CDBL Function in Excel VBA

Below are the different examples of VBA convert to double.

You can download this VBA CDBL Excel Template here – VBA CDBL Excel Template

Example #1: Convert string data type to double

Let’s see how the data conversion is performing, and what are the changes that you can make with the help of data conversion functions. The string is a datatype which accepts all datatypes. A variable is defined as a string and assigns a number to it.

  • Use a private function to check the changes in different datatype, create a function convert as private the declare the variable ‘a’ as a string.

Code:

Private Sub convert()

Dim a As String

End Sub

VBA CDBL Example 2-1

  • To perform the conversion, you need a variable. Since the variable is converting into double data type declare it as a double datatype. After data conversion, the value will be assigned to this variable.

Code:

Private Sub convert()

Dim a As String
Dim convert As Double

End Sub

VBA CDBL Example 2-2

  • Assign a floating number 1234.5645879 to the string variable. The string datatype will accept the numbers with floating values.

Code:

Private Sub convert()

Dim a As String
Dim convert As Double
a = 1234.5645879

End Sub

VBA CDBL Example 2-3

  • Now use the VBA CDBL function to convert the value to double. You can use the ‘convert’ variable which is declared as a double datatype. Pass the variable ‘a’ as an expression to CDBL function.

Code:

Private Sub convert()

Dim a As String
Dim convert As Double
a = 1234.5645879
convert = CDbl(a)

End Sub

VBA CDBL Example 2-4

  • To view the output value, you can print using a message box.

Code:

Private Sub convert()

Dim a As String
Dim convert As Double
a = 1234.5645879
convert = CDbl(a)
MsgBox convert

End Sub

VBA CDBL Example 2-5

  • Run this code by hitting F5 directly or manually hitting the Run button on the upper left panel.

VBA CDBL Example 2-6

  • The value assigned to variable ‘a’ will be printed as same with floating points and an integer value. The value is converted into double data type and the same is printed as below.

Example #2 – Converting and Adding Two Numbers

You have two variables to add and find the sum as part of your calculation. Both are floating numbers with decimal values. But one variable is declared as integer and another as double.

  • Create a function add to find the sum of two numbers. Two variables A1 and A2 are declared as integer and double respectively.

Code:

Private Sub add()

Dim A1 As Integer
Dim A2 As Double

End Sub

Example 3-1

  • Assign the two numbers to the respective variables. Both are two floating numbers with decimal values.

Code:

Private Sub add()

Dim A1 As Integer
Dim A2 As Double
A1 = 1256.45
A2 = 1234.58

End Sub

Example 3-2

  • Declare a third variable sum as double data type since the result will be a double value.

Code:

Private Sub add()

Dim A1 As Integer
Dim A2 As Double
Dim sum As Double
A1 = 1256.45
A2 = 1234.58

End Sub

VBA CDBL Example 3-3

  • Add the given two numbers and put the result in the variable sum.

Code:

Private Sub add()

Dim A1 As Integer
Dim A2 As Double
Dim sum As Double
A1 = 1256.45
A2 = 1234.58
sum = A1 + A2

End Sub

Example 3-4

  • Using a message box lets print the output.

Code:

Private Sub add()

Dim A1 As Integer
Dim A2 As Double
Dim sum As Double
A1 = 1256.45
A2 = 1234.58
sum = A1 + A2
MsgBox sum

End Sub

VBA CDBL Example 3-5

  • Run this code by hitting F5 directly or manually hitting the Run button on the upper left panel.

Result of Example 3-6

The expected result is 2491.03 and while checking the output you can see some difference in the result. The mismatch in result occurred due to the value taken by variable A1. Since this is an integer variable this will not accept the decimal part of the number only the integer part is taken while processing the sum.

  • To avoid this, convert the number A1 to double and assign the converted number to another variable A3.

Code:

Private Sub add()

Dim A1 As Integer
Dim A2 As Double
Dim sum As Double
A1 = 1256.45
A2 = 1234.58
A3 = CDbl(1256.45)
sum = A1 + A2
MsgBox sum

End Sub

VBA CDBL Example 3-7

  • Now add the converted variable with A2 instead of A1. Since the number is converted to double datatype the value with floating numbers will be accepted and added with A2.

Code:

Private Sub add()

Dim A1 As Integer
Dim A2 As Double
Dim sum As Double
A1 = 1256.45
A2 = 1234.58
A3 = CDbl(1256.45)
sum = A2 + A3
MsgBox sum

End Sub

VBA CDBL Example 3-8

  • Run this code by hitting F5 directly or manually hitting the Run button on the upper left panel.

Result of Example 3-9

The sum value has been corrected and produced the expected result.

Things to Remember

  • VBA CDBL function will not accept values rather than a number.
  • A type mismatch error will be produced if a text value is given to the VBA CDBL function.
  • Double data type displays a 13 digit decimal values.
  • VBA CDBL function helps you to get a proper result while processing the numbers in Excel.

Recommended Articles

This is a guide to VBA CDBL Function. Here we discuss how to use VBA CDBL function to convert the value to Double data type in Excel along with some practical examples and downloadable excel template. You can also go through our other suggested articles –

  1. VBA Block Comment
  2. VBA Named Range
  3. VBA Type Mismatch
  4. VBA RGB
Popular Course in this category
VBA Training (4 Courses, 12+ Projects)
  4 Online Courses |  13 Hands-on Projects |  50+ Hours |  Verifiable Certificate of Completion
4.7
Price

View Course
1 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign In
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Excel Charts
  • Excel Tips
  • All Tutorials
Certification Courses
  • All Courses
  • Excel VBA Course - All in One Bundle
  • VBA Course
  • Excel Data Analysis Course
  • Excel for Marketing Course
  • Excel for Finance Course
  • Excel for HR Training

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more