简体   繁体   中英

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 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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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