The idiosyncrasies of Val don't help if you are trying to actually get a value normally from a string that might include such "formatting" characters though.
I don't suppose there is a way to force it to select numbers only.
Basically you need to validate the input string, before applying to Val().
The only simple way I can think of is to iterate through the string using IsNumeric for each character and remove the char if it's not. This, of course, won't work for Octal or Hex values.
Simple example attached:
Code:
Public Function StripForVal(pstrIn As String) As String
Dim strOutput As String
Dim lngChar As Long
Dim strChar As String * 1
strOutput = vbNullString
lngChar = 1
Do While lngChar < Len(pstrIn) + 1
strChar = Mid(pstrIn, lngChar, 1)
If IsNumeric(strChar) Then
strOutput = strOutput & strChar
End If
lngChar = lngChar + 1
Loop
StripForVal = strOutput
End Function
Basically you need to validate the input string, before applying to Val().
The only simple way I can think of is to iterate through the string using IsNumeric for each character and remove the char if it's not. This, of course, won't work for Octal or Hex values.
Simple example attached:
Code:
Public Function StripForVal(pstrIn As String) As String
Dim strOutput As String
Dim lngChar As Long
Dim strChar As String * 1
strOutput = vbNullString
lngChar = 1
Do While lngChar < Len(pstrIn) + 1
strChar = Mid(pstrIn, lngChar, 1)
If IsNumeric(strChar) Then
strOutput = strOutput & strChar
End If
lngChar = lngChar + 1
Loop
StripForVal = strOutput
End Function
.. but actually, on reflection, a better and simpler way is to use RegEx
Code:
Public Function StripCharsForVal(pstrIn As String) As String
Static RE As New VBScript_RegExp_55.RegExp
RE.Pattern = "[^0-9]{1,}"
RE.Global = True
StripCharsForVal = RE.Replace(pstrIn, vbNullString)
End Function