变量到文本框 vba

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19163769/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 23:37:49  来源:igfitidea点击:

Variable to a Textbox vba

excelvbavariablestextbox

提问by charliejpg7

I'm trying to add a range to a variable and then that variable will be the text that will be shown on the text box. And what I did so far is the code below:

我正在尝试向变量添加一个范围,然后该变量将是将显示在文本框中的文本。到目前为止我所做的是下面的代码:

Private Sub TextBox1_Change()
Dim text1 As String
text1 = Sheet16.Range("C21:D40").Value
Sheet15.TextBox1.Value = text1
End Sub

and I got the error "Type mismatch" when I try to run it. Please any advise will be highly appreciated.

当我尝试运行它时出现错误“类型不匹配”。请任何建议将不胜感激。

回答by Ivanzinho

You can not assing a range to a string variable.

您不能将范围分配给字符串变量。

If you want to add up or to sum the range, you're code will look similar to this one:

如果您想将范围相加或求和,您的代码将类似于以下代码:

Private Sub myAddingFunction()
    Dim text1As String
    text1= Application.Sum(Range("C21:D40"))
    Range("c1").Value = text1
End Sub

Where Range("c1") can be replaced for Sheet15.TextBox1.Value

其中 Range("c1") 可以替换为 Sheet15.TextBox1.Value