VB and VBA Users Source Code: Error proof method of converting variants to doubles
[
Home
|
Contents
|
Search
|
Reply
| Previous | Next ]
VB/VBA Source Code
Error proof method of converting variants to doubles
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Wednesday, July 13, 2005
Hits:
1832
Category:
Visual Basic General
Article:
Below is a simple alternative to Cdbl, which should prevent any "type mismatch" errors when coverting variants to numeric values. 'Purpose : Provides a error checked function for converting variants into numeric values. 'Inputs : vValue The value to convert to a double 'Outputs : Returns a double representation of the variant. 'Date : 25/03/2001 'Author : Andrew Baker (copyright www.vbusers.com) 'Notes : Public Function CDbl2(vValue As Variant) As Double On Error GoTo ErrFailed 'Try to cast using the regionally aware Cdbl function CDbl2 = CDbl(vValue) Exit Function ErrFailed: If IsNull(vValue) Then 'Value is a null, return 0 CDbl2 = 0 Else 'Try to casting using val (handles strings better) CDbl2 = Val(vValue) End If End Function
Terms and Conditions
Support this site
Download a trial version of the Excel Workbook Rebuilder