Excel VBA - 如何替换数值中的字符(不是字符串!)?

时间:2016-11-07 20:57:06

标签: excel vba replace

我正在尝试解析JSON对象,但我遇到了数值问题。 json示例及其解析在我之前的问题here中。

[{"Id":"2604","Price": 520.25, "State": "true"},{"Id":"2605","Price": 322.15, "State": "false"}]

我需要在" Price"中用逗号替换一个点。项:

ws.Cells(currentRow, startColumn + 1).Value = Replace(jsonRow("Price"), ".", ",")

价格采用数字格式,但我需要更换"。"用","但它显然只适用于字符串,而不是这里的情况。

"Price": 520.25

我需要将它改为" 520,25"在它插入细胞之前。它应该保持数字类型,但逗号是必要的。

完整代码:

   Sub Test()
    Dim jsonText As String
    Dim jsonObj As Dictionary
    Dim jsonRows As Variant
    Dim jsonRow As Variant
    Dim ws As Worksheet
    Dim currentRow As Long
    Dim startColumn As Long
    Dim i As Long

    Set ws = Worksheets("VIEW")


    'Create a real JSON object
    jsonText = ws.Range("A1").Value

'Parse it
Set jsonRows = JSON.parse(jsonText)

'Set the starting row where to put the values
currentRow = 1

'First column where to put the values
startColumn = 2 'B

'Loop through all the values received
For Each jsonRow In jsonRows
    'Now set all the values in this row
    ws.Cells(currentRow, startColumn).Value = jsonRow("Id")
    ws.Cells(currentRow, startColumn + 1).Value = Replace(CStr(jsonRow("Price")), ".", ",")
    ws.Cells(currentRow, startColumn + 2).Value = jsonRow("State")


    'Increment the row to the next one
    currentRow = currentRow + 1
Next jsonRow

End Sub

0 个答案:

没有答案