【发布时间】: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