如何通过excel VBA将数据导入单元格

时间:2017-07-13 16:06:57

标签: excel vba excel-vba

android:grantUriPermissions

我只想要excel表中的一些单词,例如 来自以上数据" A1 = VISESHINFO" in" A2 = NUTEK"在单元格" A3 = ORIENTALTL"

请有人帮助我。 我是这种类型的新手。提前谢谢。

3 个答案:

答案 0 :(得分:1)

以下是使用MS脚本控件的方法:

Sub TestJSONParsing()
    'requires reference to "Miscosoft Script Control 1.0"
    Dim oScriptEngine As ScriptControl, i, n
    Set oScriptEngine = New ScriptControl
    oScriptEngine.Language = "JScript"

    'create a js object in the scriptengine
    '(loading json from a worksheet cell)
    oScriptEngine.Eval ("var json=(" + Range("A8").Value + ")")

    n = oScriptEngine.Eval("json.data.length") '<< how many items in "data"?
    'loop over and extract the symbols...
    For i = 1 To n
        Debug.Print oScriptEngine.Eval("json.data[" & (i - 1) & "].symbol")
    Next i

End Sub

答案 1 :(得分:0)

这只是我迄今为止构建的解决方案。它可以肯定更好地工作,它可以进一步更新,但到目前为止它的工作原理。

  • 首先确保复制整个数据并将其放在一个单元格中。
  • 选择列
  • 运行Sub TestMe()
  • 尽量做得更好

    Option Explicit
    
    Public Sub TestMe()
    
        Debug.Print GetMeSomeData(Selection, "symbol", 1)
        Debug.Print GetMeSomeData(Selection, "symbol", 2)
        Debug.Print GetMeSomeData(Selection, "symbol", 3)
    
    End Sub
    
    Function GetMeSomeData(strData1 As String, _
                       strData2 As String, _
                       lngRepeat As Long) _
                                As String
    
    Dim arrTotal As Variant
    Dim varText As Variant
    Dim lngPosition As Long
    Dim lngResult As Long
    Dim lngFound As Long
    
    arrTotal = Split(strData1)
    
    For Each varText In arrTotal
        lngPosition = lngPosition + 1
        varText = Replace(Replace(varText, ":", ""), """", "")
    
        If varText = strData2 Then
            lngFound = lngFound + 1
    
            If lngFound = lngRepeat Then lngResult = lngPosition
        End If
    
    Next varText
    
    GetMeSomeData = arrTotal(lngResult)
    
    
    End Function
    

这就是您在即时窗口中获得的内容:

"VISESHINFO",

"NUTEK",

"ORIENTALTL",

答案 2 :(得分:0)

我强烈建议在GitHub上使用:VBA-JSON parser。点击链接获取示例代码和视频教程。

相关网站的代码:

' Advanced example: Read .json file and load into sheet (Windows-only)
' (add reference to Microsoft Scripting Runtime)
' {"values":[{"a":1,"b":2,"c": 3},...]}

Dim FSO As New FileSystemObject
Dim JsonTS As TextStream
Dim JsonText As String
Dim Parsed As Dictionary

' Read .json file
Set JsonTS = FSO.OpenTextFile("example.json", ForReading)
JsonText = JsonTS.ReadAll
JsonTS.Close

' Parse json to Dictionary
' "values" is parsed as Collection
' each item in "values" is parsed as Dictionary
Set Parsed = JsonConverter.ParseJson(JsonText)

' Prepare and write values to sheet
Dim Values As Variant
ReDim Values(Parsed("values").Count, 3)

Dim Value As Dictionary
Dim i As Long

i = 0
For Each Value In Parsed("values")
  Values(i, 0) = Value("a")
  Values(i, 1) = Value("b")
  Values(i, 2) = Value("c")
  i = i + 1
Next Value

Sheets("example").Range(Cells(1, 1), Cells(Parsed("values").Count, 3)) = Values