VBA 将文本更改为数字格式

VBA change text to number format

在 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

对第二个需求有什么想法吗?

解决第二个需求的下一个选项"works fine"但是太慢了(超过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

非常感谢

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

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

在这种情况下,我认为数字格式根本不需要。

您正在使用基本上属于文本的替换函数category.So您可以将结果乘以 1,也可以使用值函数。