[英]Excell cell value is not read as Number?
如果您將任何值應用到運算符,Excel都會嘗試將其轉換為數字,並且此轉換將處理空格。 因此,您可以使用=A1*1
或A1+0
將A1中的值轉換為數字,或者在函數=SUM(IFERROR(A1*1,0))
中將其轉換為數字。
這種隱式轉換會自動執行trim()
。 您也可以使用功能N()
或NumberValue()
對於較新版本的Excel NumberValue()
顯式地進行此轉換。 但是,正如其他人指出的那樣,許多字符不會自動處理,您可能需要使用Substitute()
刪除它們。 例如,用Substitute(A1,160,"")
表示不間斷的空格,由於它在html中盛行,所以是主要的懷疑對象 。 Clean()
函數可以通過對一堆已知有問題的字符執行此操作,從而為您提供快捷方式,但是它並不全面,您仍然需要為不間斷的空格添加自己的處理方式。 您可以使用Code()
函數Code(Mid(A1,1,1))
您的任何特定字符的ASCII碼...例如Code(Mid(A1,1,1))
字符處理UDF
下面的UDF通過允許從一個范圍內的每個單元格中刪除多個字符,從而為字符處理方法提供了靈活性,並產生了可用作參數的結果。 例如, Sum(RemoveChar(A1:A5,160))
將從要求和的范圍中刪除所有不間斷空格。 可以通過在范圍或數組中指定多個字符來刪除,例如Sum(RemoveChar(A1:A5,B1:B3))
或Sum(RemoveChar(A1:A5,{160,150}))
。
Function RemoveChar(R As Range, ParamArray ChVal() As Variant)
Dim x As Variant
Dim ResVals() As Variant
ReDim ResVals(1 To R.Count)
'Loop through range
For j = 1 To R.Count
x = R(j).Value2
If x <> Empty Then
'Try treating character argument as array
'If that fails, then try treating as Range
On Error Resume Next
For i = 1 To UBound(ChVal(0))
x = Replace(x, Chr(ChVal(0)(i)), "")
Next
If Err = 92 Then
Err.Clear
For Each Rng In ChVal(0)
x = Replace(x, Chr(Rng.Value2), "")
Next
End If
Err.Raise (Err)
On Error GoTo 0
'If numeric then convert to number
'so that numbers will be treated as such
'when array is passed as an argument
If IsNumeric(x) Then
ResVals(j) = Val(x)
Else
ResVals(j) = x
End If
End If
Next
'Return array of type variant
RemoveChar = ResVals
End Function
數值驗證UDF
替換字符的缺點是它不全面。 如果您想要更多的東西,那么也許是這樣。
Function GetNumValues(R As Range)
Dim c, temp As String
Dim NumVals() As Double
ReDim NumVals(1 To R.Count)
'Loop through range
For j = 1 To R.Count
'Loop through characters
'Allow for initial short-circuit if already numeric
For i = 1 To Len(R(j).Value2)
c = Mid(R(j).Value2, i, 1)
'If character is valid for number then include in temp string
If IsNumeric(c) Or c = Application.DecimalSeparator Or c = Application.ThousandsSeparator Then
temp = temp + c
End If
Next
'Assign temp string to array of type double
'Use Val() function to convert string to number
NumVals(j) = Val(temp)
'Reset temp string
temp = Empty
Next
'Return array of type double
GetNumValues = NumVals
End Function
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.