【问题标题】:VBA change text to number formatVBA将文本更改为数字格式
【发布时间】:2018-06-19 12:12:41
【问题描述】:

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

我需要两种替代品

首先需要:

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

非常感谢

【问题讨论】:

  • 将值分成两个不同的单元格怎么样?

标签: excel vba text format numeric


【解决方案1】:

您可以像这样强制执行文本格式:

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 根本不需要。

【讨论】:

  • 您好 JvdV,感谢您的回复。虽然 "Selection.NumberFormar = "@" 强制将文本格式保留在单元格中,但问题仍然比我有 "256, 256" 并删除空格,转换为 "256,256",excel 删除逗号 (, ) 自动认为是英里分隔符 :(
  • @Angel,我无法在我的机器上重现它,这就像一个魅力。您能否逐行通过并告诉我上次替换后的值。所以在Cells(1 ,1) = STR之前,请先看一下STR值(Debug.print STR)
  • 嗨,JvdV,看来使用你的方法 STR= Replace(STR, "[","") 工作正常(我没能尝试这么多)。问题是我想在一些行数英里的列中进行此替换,所以我对所有列执行 Selection.Replace,而不是逐个单元格。我想使用excel函数Selection.Replace(如我的示例)比逐个单元格更快。但是使用 Selection.Replace 将文本 vell 值转换为数字。有什么想法吗?
  • @Angel,因为 Excel 最强大的本能是将任何看起来有点像数字的东西转换为数字,这似乎很难。实现您想要的最快方法是在更新的答案中。
  • 感谢 JvdV。第一种方法似乎有效,我会尝试的。秒它还不错,所以我将拆分分隔符更改为转换为数组,但它可以像一个一样好
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-27
    相关资源
    最近更新 更多