Board logo

標題: 求精簡程式 [打印本頁]

作者: tureD    時間: 2024-9-19 15:07     標題: 求精簡程式

想破頭,不知如何精簡
Sub Json_split_Range()
Dim i As Integer
S = Range("A2")
arr = Split(S, "[")
Cells(3, 1) = arr(1)
ARR1 = Split(arr(1), ",")

ARR2 = Split(Trim(ARR1(0)), ":")
Cells(4, 1) = ARR2(1)
ARR2 = Split(Trim(ARR1(1)), ":")
Cells(4, 2) = ARR2(1)
ARR2 = Split(Trim(ARR1(2)), ":")
Cells(5, 1) = ARR2(1)
ARR2 = Split(Trim(ARR1(3)), ":")
Cells(5, 2) = ARR2(1)
ARR2 = Split(Trim(ARR1(4)), ":")
Cells(6, 1) = ARR2(1)
ARR2 = Split(Trim(ARR1(5)), ":")
Cells(6, 2) = ARR2(1)
End Sub
作者: doomtwo222    時間: 2024-10-7 16:43

Sub Json_split_Range()
    Dim i As Integer
    Dim S As String
    Dim arr As Variant
    Dim ARR1 As Variant
    Dim ARR2 As Variant

    ' 取得儲存格 A2 的值
    S = Range("A2").Value

    ' 先將整個 JSON 字串分割
    arr = Split(S, "[")
    Cells(3, 1).Value = arr(1)

    ' 將 JSON 中的逗號分隔值分割
    ARR1 = Split(arr(1), ",")

    ' 迴圈解析每個分隔值,並填入相應的儲存格
    For i = 0 To UBound(ARR1)
        ARR2 = Split(Trim(ARR1(i)), ":")
        ' i 偶數時放在左側欄位,奇數時放在右側欄位
        Cells(4 + Int(i / 2), 1 + (i Mod 2)).Value = ARR2(1)
    Next i
End Sub




歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)