Arrays 如何使用VBA从excel中的数组中提取值

Arrays 如何使用VBA从excel中的数组中提取值,arrays,excel,vba,Arrays,Excel,Vba,在我的表中,我有一个单元格A1,其中包含一个数组作为字符串,格式如下:[{'type':'general','name':'light'},{'type':'brand','name':'lighti'},{'type':'misc','name':'Sale%} 现在我想创建一个新的表单,将品牌“lighti”的名称作为单独的单元格值。这意味着:我想得到A1中的值,找到类型“brand”,返回品牌名称并粘贴到A2。就这些 如何使用VBA提取数组的值?假设单词brand每次都在品牌名称之前 Fu

在我的表中,我有一个单元格A1,其中包含一个数组作为字符串,格式如下:
[{'type':'general','name':'light'},{'type':'brand','name':'lighti'},{'type':'misc','name':'Sale%}

现在我想创建一个新的表单,将品牌“lighti”的名称作为单独的单元格值。这意味着:我想得到A1中的值,找到类型“brand”,返回品牌名称并粘贴到A2。就这些


如何使用VBA提取数组的值?

假设单词brand每次都在品牌名称之前

Function GetNameOfBrand(celltext As String) As String
Dim x As Long
Dim s As String
x = InStr(celltext, "brand")
If x = 0 Then
    GetNameOfBrand = ""
Else
    s = Mid(celltext, x + 16, Len(celltext) - x + 15)
    x = InStr(s, "'")
    s = Left(s, x - 1)
    GetNameOfBrand = s
End If
End Function

假设品牌这个词每次都在品牌名称之前

Function GetNameOfBrand(celltext As String) As String
Dim x As Long
Dim s As String
x = InStr(celltext, "brand")
If x = 0 Then
    GetNameOfBrand = ""
Else
    s = Mid(celltext, x + 16, Len(celltext) - x + 15)
    x = InStr(s, "'")
    s = Left(s, x - 1)
    GetNameOfBrand = s
End If
End Function

您可以使用自定义函数从A1中读取值,使用搜索词应用拆分,并解析出所需的信息。使用JSON解析器似乎有点过火,尽管该字符串是JSON,您可以通过这种方式提取

Option Explicit

Public Sub test()
    [A2] = GetValue([a1], "brand")
End Sub
Public Function GetValue(ByVal rng As Range, ByVal searchTerm As String) As Variant
    '[{'type':'general', 'name':'light'},{'type':'brand', 'name':'lighti'},{'type':'misc', 'name':'Sale%'}]
    On Error GoTo errhand
    GetValue = Split(Split(rng.Value, "{'type':'" & searchTerm & "', 'name':'")(1), "'")(0)
    Exit Function
errhand:
    GetValue = CVErr(xlErrNA)
End Function

如果要使用类似于JSONParser的JSON解析器,可以按如下方式解析JSON。注意:将.bas添加到项目后,您需要转到VBE>工具>引用并添加对Microsoft脚本运行时的引用

Option Explicit
Public Sub test()
    [A2] = GetValue([a1], "brand")
End Sub
Public Function ExtractItem(ByVal rng As Range, ByVal searchTerm As String) As Variant
    Dim json As Object, key As Object
    json = JsonConverter.ParseJson(rng.Value)
    For Each item In json
        For Each key In item
            If key = searchTerm Then
                GetValue = item(key)
                Exit Function
            End If
        Next
    Next
    ExtractItem = CVErr(xlErrNA)
End Function

您可以使用自定义函数从A1中读取值,使用搜索词应用拆分,并解析出所需的信息。使用JSON解析器似乎有点过火,尽管该字符串是JSON,您可以通过这种方式提取

Option Explicit

Public Sub test()
    [A2] = GetValue([a1], "brand")
End Sub
Public Function GetValue(ByVal rng As Range, ByVal searchTerm As String) As Variant
    '[{'type':'general', 'name':'light'},{'type':'brand', 'name':'lighti'},{'type':'misc', 'name':'Sale%'}]
    On Error GoTo errhand
    GetValue = Split(Split(rng.Value, "{'type':'" & searchTerm & "', 'name':'")(1), "'")(0)
    Exit Function
errhand:
    GetValue = CVErr(xlErrNA)
End Function

如果要使用类似于JSONParser的JSON解析器,可以按如下方式解析JSON。注意:将.bas添加到项目后,您需要转到VBE>工具>引用并添加对Microsoft脚本运行时的引用

Option Explicit
Public Sub test()
    [A2] = GetValue([a1], "brand")
End Sub
Public Function ExtractItem(ByVal rng As Range, ByVal searchTerm As String) As Variant
    Dim json As Object, key As Object
    json = JsonConverter.ParseJson(rng.Value)
    For Each item In json
        For Each key In item
            If key = searchTerm Then
                GetValue = item(key)
                Exit Function
            End If
        Next
    Next
    ExtractItem = CVErr(xlErrNA)
End Function

您可以使用ActiveX
ScriptControl
,将
Language
设置为
JScript
,并将字符串解析为实际的JSON

然后,您可以编写一个Javascript函数,根据“类型”返回“名称”。为此,您不需要任何外部库/其他宏等:

Option Explicit
Public Sub UseScriptControlAndJSON()
    Dim JsonObject As Object
    Dim resultString As String
    Dim ScriptEngine As Object

    'get the script control:
    Set ScriptEngine = CreateObject("ScriptControl")
    ScriptEngine.Language = "JScript"

    'Add javascript to get the "name" based on "typeName":
    ScriptEngine.AddCode "function findByType(jsonObj, typeName) { for (var i = 0; i < jsonObj.length; i++) { if (jsonObj[i].type == typeName){ return jsonObj[i].name; }}}"

    'Get the string and parse it:
    Set JsonObject = ScriptEngine.Eval("(" & Range("A1").Value & ")")

    'Now get the resulting "name" using the JS function, by passing in "brand" as type:
    resultString = ScriptEngine.Run("findByType", JsonObject, "brand")

    'Will pop-up: "lighti"
    MsgBox resultString
End Sub
选项显式
公共子用途ScriptControlAndJSON()
Dim JsonObject作为对象
Dim resultString作为字符串
将脚本引擎设置为对象
'获取脚本控件:
设置ScriptEngine=CreateObject(“ScriptControl”)
ScriptEngine.Language=“JScript”
'添加javascript以根据“typeName”获取“name”:
ScriptEngine.AddCode“函数findByType(jsonObj,typeName){for(var i=0;i
注1:JS函数将返回第一次出现的情况

注2:严格来说,您并没有使用VBA来提取值


注3:在64位机器上使用32位Excel 2016进行测试;脚本控件是一个32位的组件-例如,请参见此-在64位上,您可以根据该链接中的一个答案使用一些变通方法。

您可以使用ActiveX
ScriptControl
,将
语言设置为
JScript
,并将字符串解析为实际的JSON

然后,您可以编写一个Javascript函数,根据“类型”返回“名称”。为此,您不需要任何外部库/其他宏等:

Option Explicit
Public Sub UseScriptControlAndJSON()
    Dim JsonObject As Object
    Dim resultString As String
    Dim ScriptEngine As Object

    'get the script control:
    Set ScriptEngine = CreateObject("ScriptControl")
    ScriptEngine.Language = "JScript"

    'Add javascript to get the "name" based on "typeName":
    ScriptEngine.AddCode "function findByType(jsonObj, typeName) { for (var i = 0; i < jsonObj.length; i++) { if (jsonObj[i].type == typeName){ return jsonObj[i].name; }}}"

    'Get the string and parse it:
    Set JsonObject = ScriptEngine.Eval("(" & Range("A1").Value & ")")

    'Now get the resulting "name" using the JS function, by passing in "brand" as type:
    resultString = ScriptEngine.Run("findByType", JsonObject, "brand")

    'Will pop-up: "lighti"
    MsgBox resultString
End Sub
选项显式
公共子用途ScriptControlAndJSON()
Dim JsonObject作为对象
Dim resultString作为字符串
将脚本引擎设置为对象
'获取脚本控件:
设置ScriptEngine=CreateObject(“ScriptControl”)
ScriptEngine.Language=“JScript”
'添加javascript以根据“typeName”获取“name”:
ScriptEngine.AddCode“函数findByType(jsonObj,typeName){for(var i=0;i
注1:JS函数将返回第一次出现的情况

注2:严格来说,您并没有使用VBA来提取值


注3:在64位机器上使用32位Excel 2016进行测试;脚本控件是一个32位的组件-例如,请参见此-在64位上,您可以根据该链接中的一个答案让它使用一些变通方法。

我还没有开始,因为我甚至不确定这是否可行。因此,没有理由否决我——这里并不禁止从头开始。我怀疑否决票是由于问题的不明确。这是以数组形式输入此值的单个单元格吗?您希望将此值视为一个数组,但您希望提取哪个值,还是希望能够灵活地指定该值的索引?(等)好吧,我试着把我的问题尖锐化。数组值以字符串形式存在于单元格A1中。我想得到A1中的值,找到类型“brand”,返回品牌名称并粘贴到A2。仅此而已。对于这个特定的用例,您可以通过反复使用
Mid
(删除括号)和
Split
(在
”、“
”:“
)来拼凑一些东西,但最好是找到一个VBA JSON解析器(其中有几个),因为这是一个很难正确改造的轮子。我没有使用过它,但我看到了建议的堆栈溢出。@Nixen85在被视为“免费编写代码”服务时,可能会被否决并被标记为“太宽”或“不清楚您在问什么”。“如何使用VBA提取数组的值?”有很多答案,这不是您需要帮助的特定编程问题。人们对“零努力”问题不太友好,我有