vba 清除文本框的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26957580/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Clear Value of a TextBox
提问by Facosenpai
I've got an excel UserForm with two TextBox and a Button. How can I delete the value of the TextBox without using TextBox1.Value = ""
or TextBox1.Text = ""
?
我有一个带有两个文本框和一个按钮的 excel 用户窗体。如何在不使用TextBox1.Value = ""
or 的情况下删除 TextBox 的值TextBox1.Text = ""
?
I can't use these two operations because then the instruction: TextBox2.Value = TextBox1.value*2
give me back an error. (The error is Runtime error 13)
我不能使用这两个操作,因为然后指令:TextBox2.Value = TextBox1.value*2
给我一个错误。(错误是运行时错误 13)
Thanks.
谢谢。
采纳答案by Portland Runner
Try looking at the problem a different way. If box1 is empty then why would you want to multiply it by 2? In this case you should error check the value of box1 before you multiply it by box2. The code below checks if Textbox1 is a number and is not empty before multiplying it.
尝试以不同的方式看待问题。如果 box1 是空的,那么为什么要乘以 2?在这种情况下,您应该在将 box1 乘以 box2 之前错误检查 box1 的值。下面的代码在乘以它之前检查 Textbox1 是否为数字并且不为空。
Private Sub CommandButton1_Click()
'Error check: does textbox1 have a number and is not null
If IsNumeric(TextBox1.Value) And TextBox1.Value <> vbNullString Then
TextBox2.Value = TextBox1.Value * 2
Else
TextBox2.Value = "Box1 is empty or invalid"
End If
End Sub
Valid Number:
有效号码:
Invalid or empty:
无效或为空: