VBA将文本更改为数字格式

时间:2018-06-19 12:12:42

标签: excel vba text format numeric

在vba中,我想进行一些替换,但是当看到类似数字的数字时,Excel会破坏格式。

我需要2种替代品

首先需要:

Original Data      =>   Wanted Data   =>  Best Approach   
['4', 6, '4']      =>   4,6,4         =>  4#6#4
[1.5, None, None]  =>   1.5           =>  1.5##
[256, 256]         =>   256,256       =>  256#256
[None, '', None]   =>   ,,            =>  ##
[4, 4]             =>   4,4           =>  4#4
[None, '1.50']     =>   1.5           =>  #1.5

无法获得所需数据,因为所需数据256,256会自动转换为256.256

该方法对我来说很好,因为我可以使用#作为分隔符进行分割

With RNG
        .Replace What:="None", Replacement:=""
        .Replace What:=",", Replacement:="#"
        .Replace What:="[", Replacement:=""
        .Replace What:="]", Replacement:=""
        .Replace What:="'", Replacement:=""
        .Replace What:=" ", Replacement:=""
End With

第二个需求(字符串中只有1个浮点数):

Original Data   =>   Wanted Data      
[None, '1.5']   =>   1.5      (breaks into 1,5)                  
[1.50]          =>   1.50     (breaks into 1,5) 
0.9,            =>   0.9      (breaks into 0,9)      
0.6             =>   0.6      (doesn't break because any replaced has been made)

With RNG
        .NumerFormat = "@" 'Doesn't work, although I do not want it either text format
        .Replace What:="None", Replacement:=""
        .Replace What:=",", Replacement:=""
        .Replace What:="[", Replacement:=""
        .Replace What:="]", Replacement:=""
        .Replace What:="'", Replacement:=""
        .Replace What:=" ", Replacement:=""
End With

有第二需求的想法吗?

解决第二个需求的下一个选项“工作正常”,但速度太慢(超过10000行),甚至在此过程中阻塞了我的Excel

For Each CL In RNG
        CL.NumberFormat = "@"
        CL.Value = Replace(CL.Value, ",", "")
        CL.Value = Replace(CL.Value, "None", "")
        CL.Value = Replace(CL.Value, "[", "")
        CL.Value = Replace(CL.Value, "]", "")
        CL.Value = Replace(CL.Value, "'", "")
        CL.Value = Replace(CL.Value, " ", "")
Next CL

非常感谢

2 个答案:

答案 0 :(得分:0)

您可以像这样强制使用文本格式:

Sub Test()

Dim RNG As Range, CL As Range
Dim LR as Long

'Get last used row in your sheet and set range (use a different type of lookup for LR if you want.
LR = Sheets(1).Cells.SpecialCells(xlCellTypeLastCell).Row
Set RNG = Sheets(1).Range(Cells(1, 14), Cells(LR, 16))

'Set values to text and replace values
With RNG
    .NumberFormat = "@"
    .Replace What:="[", Replacement:=""
    .Replace What:="]", Replacement:=""
End With

'Trim all values
For Each CL In RNG
    CL.Value = Replace(CL.Value, " ", "")
Next CL

End Sub
  • 尝试避免使用.selectSelection
  • 显然可以根据需要更改RNG。

注意::如果您不关心逗号,可以将其替换为“-”

Sub Test()

Dim RNG As Range
Dim LR As Long

'Get last used row in your sheet and set range (use a different type of lookup for LR if you want.
LR = Sheets(1).Cells.SpecialCells(xlCellTypeLastCell).Row
Set RNG = Sheets(1).Range(Cells(1, 14), Cells(LR, 16))

'Set values to text and replace values
With RNG
    .NumberFormat = "@"
    .Replace What:="[", Replacement:=""
    .Replace What:="]", Replacement:=""
    .Replace What:=" ", Replacement:="-"
    .Replace What:=",", Replacement:=""
End With

End Sub

在这种情况下,我认为numberformat根本不是必需的。

答案 1 :(得分:0)

您正在使用基本上属于文本类别的替换函数。因此您可以将结果乘以1或使用值函数。