简体   繁体   English

VBA Excel #VALUE错误

[英]VBA excel #VALUE error

I have a function that takes two string and it should add them. 我有一个需要两个字符串的函数,它应该将它们相加。

Function teiseLopp(tekst1 As String, tekst2 As String) As String
    Dim stringidKoos As String
    stringidKoos = tekst1 & "" & tekst2
    teiseLopp = stringidKoos
End Function

And it should return the two strings together but it shows me #VALUE! 它应该一起返回两个字符串,但显示#VALUE! = Value used in the formula is of the wrong data type. =公式中使用的值是错误的数据类型。 (Everything seems to be right datatype) (一切似乎都是正确的数据类型)

No need to add an empty string in the middle, and you need to define your data type. 无需在中间添加空字符串,并且您需要定义数据类型。 Try 尝试

Function teiseLopp(tekst1 As String, tekst2 As String) As String
    Dim stringidKoos As String
    stringidKoos = tekst1  & tekst2
    teiseLopp = stringidKoos
End Function

Also, why not use a regular formula, as in =A1&B1 另外,为什么不使用正则公式,如=A1&B1

Your code should work if there aren't any errors in the cells you refer from. 如果您引用的单元格中没有任何错误,那么您的代码应该可以正常工作。 Maybe you have selected more than a single cell as input? 也许您选择了多个单元格作为输入? You could give the following modification a try to see if that works. 您可以尝试以下修改,看看是否可行。 It handles you input as a Range and only operate on the value in the upper-left cell in each input range. 它将您的输入作为Range处理,并且仅对每个输入范围中左上角单元格中的值进行运算。

Function teiseLopp(tekst1 As Range, tekst2 As Range) As String
    Dim stringidKoos As String
    stringidKoos = tekst1.Range("A1").Value & "" & tekst2.Range("A1").Value
    teiseLopp = stringidKoos
End Function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM