将TextBox.Value转换为Double到VBA(Excel 2013) [英] Convert TextBox.Value to Double into VBA (Excel 2013)
本文介绍了将TextBox.Value转换为Double到VBA(Excel 2013)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我将TextBox放入表单中,用户可以在其中输入值。
在VBA中,我需要将值从字符串转换为双精度。
I have TextBox into my form, where user can input a value. In VBA I need to convert the value from string to double.
我这样做的方式是:
Private Sub UserForm_Initialize()
'....some code
Dim new_value As Double
new_value = CDbl(TextBox6.Value)
End sub
但是我在下面得到错误:
But I'm getting the error below:
推荐答案
CDbl
应该已经有一个数字,但是如果文本框为空,则 TextBox6.Value
是一个空字符串。 CDbl
不能将空字符串转换为双精度字符。
CDbl
expects already a number but if the textbox is empty then TextBox6.Value
is an empty string. CDbl
can't cast an empty string into a double.
您可以验证文本框是否为数值首先避免这种情况
You can validate if the textbox is a numeric value first to avoid this
If IsNumeric(TextBox6.Value) Then
new_value = CDbl(TextBox6.Value)
Else
new_value = 0
End If
或者 Val()
函数可能是一个
Alternatively the Val()
function might be an option for you.
new_value = Val(TextBox6.Value)
这篇关于将TextBox.Value转换为Double到VBA(Excel 2013)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文