问题
I have a column of mixed data in my Excel sheet where the numbers are formatted as "Text". Some examples of the values that are stored in my column:
- 36
- R4
- 56
- AF PL RFD
- 65
- DHU
- 14
I now need a macro that converts all the numeric values in the column to the format "Number" so that I can further process the data.
I tried the following, which throws an error 400 without any further explanation:
Sheet1.Range("A2","A50000").Select
With Selection
.NumberFormat = "General"
.Value = .Value
End With
Probably good to know is that I don't how many rows with values my column will contain so I had to take the range fairly big. Thanks for your help!
回答1:
Avoid trying to Select both a worksheet and a range in the same statement (in fact, you can avoid Select completely):
Sub ytrewq()
With Sheet1.Range("A2", "A50000")
.NumberFormat = "General"
.Value = .Value
End With
End Sub
seems to work just fine!
回答2:
I have noticed many try to answer this problem with some sort of "Number formatting" such as found in the Home Ribbon. I think that the concern lies in the extraction of data from a source and having the little green arrow in the cell with a "caution" pop-up when hovering over the cell. This cannot be fixed with number formatting and has most likely been tried by the person asking for assistance. I was one of them.
With ActiveSheet.Select
Range("A2:A10000").Select
For Each xCell In Selection
xCell.Value = CDec(xCell.Value)
Next xCell
End With
Mabbutt, Dan. "Convert Text to Number in Excel." ThoughtCo, Jun. 14, 2018, thoughtco.com/convert-text-to-number-in-excel-3424223.
来源:https://stackoverflow.com/questions/50569597/excel-vba-convert-number-stored-as-text-to-number-in-column