[英]How can I change a column of unformatted numbers so that Excel can actually read them as numbers with VBA
The problem I've run into is that after exporting a series of data from an online source and pasting it to Excel the numbers come all wrong, essentialy they are read as text instead of numbers.我遇到的问题是,从在线来源导出一系列数据并将其粘贴到 Excel 后,数字全都出错了,基本上它们被读取为文本而不是数字。 I've found that this happens for two reasons:我发现发生这种情况有两个原因:
In the picture below I have exactly how the numbers come to me, what I need is a code that runs for every cell in column "M" and then changes that cell, fixing the above problems so that Excel can actually read it as numbers What shows up to me at Excel在下面的图片中,我确切地知道数字是如何来到我的,我需要的是为“M”列中的每个单元格运行的代码,然后更改该单元格,解决上述问题,以便 Excel 可以实际将其读取为数字什么在 Excel 中出现在我面前
So at firts I tried this:所以起初我试过这个:
Sub Fatura()
Dim c As Range
For Each c In Range("M1:M1000")
c.Value = Replace(c, ".", "")
c.Value = Replace(c, " ", "")
c.NumberFormat = "General"
c.NumberFormat = "0.00"
Next c
Wich worked fine but for some reason c.value = replace(c, " ", "") did not replace the last " " character in my string Wich 工作正常但由于某种原因 c.value = replace(c, " ", "") 没有替换我的字符串中的最后一个 " " 字符
Then I tried this:然后我尝试了这个:
Sub Fatura()
Dim c As Range
For Each c In Range("M1:M1000")
c.Value = Replace(c, ".", "")
c.Value = Left(c, Len(c) - 1)
c.NumberFormat = "General"
c.NumberFormat = "0.00"
Next c
But I get run time error '5'但我收到运行时错误“5”
Thanks for the help in advance我在这里先向您的帮助表示感谢
I was able to reach an answer with @BigBen 's help.在@BigBen 的帮助下,我得到了答案。 Turns out the character at the end of my string wasn't really a space " " but a non-breaking space, wich is quite annoying.原来我的字符串末尾的字符并不是真正的空格“”,而是一个不间断的空格,这很烦人。 So after that discovery it was just a matter of rewriting my code as follows:因此,在发现之后,只需按如下方式重写我的代码即可:
Sub Fatura()
Dim c As Range
For Each c In Range("M1:M1000")
c.Value = Replace(c, ".", "")
c.Value = Replace(c, Chr(160), "")
c.NumberFormat = "General"
c.NumberFormat = "0.00"
Next c
Thanks for the help @BigBen !!!!感谢@BigBen 的帮助!!!!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.