简体   繁体   English

如何更改一列未格式化的数字,以便 Excel 可以使用 VBA 将它们实际读取为数字

[英]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:我发现发生这种情况有两个原因:

  1. There's a space " " on the end of every number每个数字的末尾都有一个空格“ ”
  2. Numbers that are over a thousand have a "."超过一千的数字有一个“。” character in the middle of them, separating the rest (eg 13.540,50)中间的字符,将其余字符分开(例如 13.540,50)

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.

相关问题 如何将CSV文件中的长数字强制转换为字符串,以使Excel不会将其转换为E表示法? - How can I coerce long numbers in CSV files to strings so that Excel does not convert them to E notation? 在Excel VBA中对数字求和时如何计数到10? - How can I count to 10 while summing the numbers in Excel VBA? VBA Excel如何使用数字定义一组列? - VBA Excel how can I define a set of columns using numbers? 如果行已排序,所以行号不连续,如何使用VBA在excel中遍历行 - How can I loop through rows in excel using VBA if rows are sorted so row numbers aren't consecutive 如何将 Excel 单元格中的数字字符串读取为字符串(而非数字)? - How can I read numeric strings in Excel cells as string (not numbers)? 如何更改 excel 中的一列数字,这些数字在 python 上都有 2 个小数点? - How can I change a column of numbers in excel that all have 2 decimal points on python? 如何在Excel中的VBA中正确关闭UserForm,以便在必要时可以重新打开它们? - How can I close the UserForms properly in VBA in Excel, so that I can reopen them if necessary? 如何在 excel 中格式化复数? - How can I format complex numbers in excel? 如何将数字添加到Excel单元格? - How can I add numbers to a excel cell? 如何通过VBA或Excel公式从文本字符串中提取特定的字母和数字 - How can I extract a specific Letter(s) and numbers from a text string via VBA Or excel Formula
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM