在 UDF 中将句号更改为逗号,反之亦然

在 UDF 中将句号更改为逗号,反之亦然

我有这个 Excel UDF 来排除单元格中的所有非数字字符(句号和逗号除外);但在清除我的数字之前,我想将所有逗号更改为句号,反之亦然。我不知道在我的代码中哪里可以进行此更改。谢谢

Function cleannumber(texto As String) As String 
Dim volta As String 
Dim pedaco As String 

volta = "" 
For i = 1 To Len(texto) 
   pedaco = Mid(texto, i, 1) 
   If pedaco >= "0" And pedaco <= "9"  Or pedaco = “,” Or pedaco =”.” Then volta = volta & pedaco 
Next i 
cleannumber = volta 
End Function

答案1

声明之前If

pedaco = Replace(pedaco, ".", "whatever")
pedaco = Replace(pedaco, ",", ".")
pedaco = Replace(pedaco, "whatever", ",")

答案2

修改后的代码:

Function cleannumber(texto As String) As String 
Dim volta As String 
Dim pedaco As String 

volta = "" 
For i = 1 To Len(texto) 
   pedaco = Mid(texto, i, 1) 

   // Add the below line
   pedaco = Replace(pedaco, ",", ".")

   If pedaco >= "0" And pedaco <= "9"  Or pedaco = “,” Or pedaco =”.” Then volta = volta & pedaco 
Next i 
cleannumber="'" & volta
End Function

相关内容