Excel数据导入为“15/2,macros转换为数字?
我正在通过电源查询将数据从API导入到Excel中。
由于一些数据在一小部分,这导致了一点噩梦。 我不能使用'数字'因为这个,它会抛出一个错误,所以我必须导入为文本。
当这样做的数据import如下,与一个“'”盈方:
因为我需要数据更新每一分钟,然后运行公式,我需要这是一个数字。 什么是最简单的方法(要么从macros观转换为数字从'15 / 2)或修复电力查询,并允许它导入/转换分数(这将是完美的!)。
非常感谢
这是一个macros使用(假设你的屏幕截图有正确的列位置)。 你需要做的唯一的调整就是调整“intLastRow”和最后一行数据。
Sub SO() Dim intLastRow As Integer, strIn As String intLastRow = 100 For I = 2 To intLastRow For t = 4 To 21 If InStr(1, Cells(I, t).Value, "/") > 0 Then strIn = Cells(I, t).Value Cells(I, t).Value = Val(Left(strIn, InStr(1, strIn, "/") - 1)) / Val(Right(strIn, Len(strIn) - InStr(1, strIn, "/"))) Else Cells(I, t).Value = Val(Cells(I, t).Value) End If Next t Next I End Sub
然后,您可以逐步选中单元格,并检查每个单元格的前缀字符是否是撇号。 如果是这样,那么你所要做的就是让macros做相当于手动重新input单元格的内容,方法如下:
For Each c In Selection If c.PrefixCharacter = "'" Then c.Value = c.Value End If Next c
请注意,该macros将检查PrefixCharacter属性中的内容。 该属性可以在VBA中读取,但不能直接更改。 这就是为什么macros需要使用看似简单的线将每个单元格的值分配回单元格 – 实质上是重新input内容。
有关完整的指南,请参阅: http : //excel.tips.net/T003332_Searching_for_Leading_Apostrophes.html
这是我自己的代码:
Public Sub tryit() Dim i As Long For i = 1 To 20 'repeat until your last record With ThisWorkbook.Sheets("Sheet1") If .Cells(i, 5).PrefixCharacter = "'" Then MsgBox "removing 1 apostrophe..." .Cells(i, 5).FormulaR1C1 = "=" & .Cells(i, 5) End If End With Next i End Sub
而…瞧!
也许这是你想看到的另一个问题的macrosvba代码:
Public Sub tryit() Dim i As Long For i = 1 To 20 'repeat until your last record With ThisWorkbook.Sheets("Sheet1") .Cells(i, 5).FormulaR1C1 = "=" & .Cells(i, 5) End With Next i End Sub
Power Query解决scheme将mathexpression式parsing为数字非常简单!
只需添加一个自定义的最后一步Table.TransformColumns(SomeLastStep, {}, Expression.Evaluate)
。 这可以通过运行像9
或15/2
这样的文本input作为一个返回数字的小程序来工作。
(您可能还想将列转换为数字。)
查看>高级编辑器完整的例子:
let SomeLastStep = #table( {"data1", "data2"}, { {"9", "15/2"}, {"10", "8"}, {"11", "10"}, {"11", "10"} } ), Custom1 = Table.TransformColumns(SomeLastStep, {}, Expression.Evaluate), #"Changed Type" = Table.TransformColumnTypes(Custom1,{{"data1", type number}, {"data2", type number}}) in #"Changed Type"