0

I have a function which returns value as string.

Function Trimcell(cellvalue As varnant) As String
    Trimcell = Replace(CStr(cellvalue), " ", "")
End Function

I want to change the data type string to long . Any help.

Mark Fitzgerald
  • 3,048
  • 3
  • 24
  • 29

2 Answers2

0

Change your function to this:

Function Trimcell(cellvalue As varnant) As Long
    Trimcell = Val(Replace(CStr(cellvalue), " ", ""))
End Function
dotNET
  • 33,414
  • 24
  • 162
  • 251
0

You have a spelling error - varnant instead of Variant.

A better option than using Replace is to use Val which removes blanks, tabs, and linefeed characters from a string and returns a Double. It also stops reading the string at the first non-numeric character apart from period (.) which it recognises as the decimal separator.

As you have declared cellvalue As Variant you shouldn't need CStr either.

Function Trimcell2(cellvalue As Variant) As Long

    Trimcell = Val(cellvalue)
End Function
Mark Fitzgerald
  • 3,048
  • 3
  • 24
  • 29