Dictionary of Dictionaries - vba Office for Mac



简而言之,我们从一个应用程序(而不是我们自己的应用程序)获得excel中的报告,该应用程序在每次提交时将单个表单中的所有用户响应放入单个单元格中。我正在尝试为Office:macExcel编写VBA宏。

因此,"修饰符"列中的单个单元格可能看起来是这样的:

单元格1:第一教师选择|冈德森;第二教师选择|Barnes;你喜欢杰洛吗|对超人还是绿巨人|绿巨人

单元格2:第一名教师| Smith;第二教师选择|冈德森;你喜欢杰洛吗|是

单元格3:第一名教师| Wulfenbach;第二名教师|丰塔纳;你喜欢杰洛吗|不超人还是绿巨人|超人

单元格4:第一名教师|丰塔纳;第二名教师| Smith;你喜欢杰洛吗|对超人还是绿巨人|超人

单元格5:首选教师| Heterodyne;第二届教师评选| Wulfenback;你喜欢杰洛吗|对超人还是绿巨人|超人

我的目标是收集所有问题(介于;和|之间的文本),为每个问题创建一列,然后用答案填充这些列。我设想哈希表为:

{
 "1st Teacher Choice":{1:"Gunderson", 2:"Smith", 3:"Wulfenbach", 4:"Fontana", 5:"Hetrodyne"},
 "2nd Teacher Choice":{1:"Barnes", 2:"Gunderson", 3:"Fontana", 4:"Smith", 5:"Wulfenbach"},
 "Do you like Jello?":{1:"yes", 2:"yes", 3:"no", 4:"yes", 5:"yes"},
 "Superman or Hulk":{1:"Hulk", 2:"",3:"Superman",4:"Superman",5:"Superman"}
}

现在,在所有的序言之后,以下是我试图开始工作的代码:

Dim modifierColumn As Integer
Dim rawModifiers As String
Dim oneMod As String
Dim oneResp As String
Dim modifierList As Dictionary
Set modifierList = New Dictionary
For theRow = 2 To lastRow
    'Get the string of modifiers in the current row
    rawModifiers = Cells(theRow, modifierColumn).value
    'Break each modifier string in the row into a separate array element
    rowModifiersArray = Split(rawModifiers, ";")
    'Iterate through each of the modifiers and value in the new array
    For Each modResp In rowModifiersArray
        'Seperate the modifier from the response in another temp array, 'singleModifier’.
        'The first element of the array will be the name of the modifier, the second will be the response to the modifier.
        singleModifier = Split(modResp, "|")
        oneMod = singleModifier(0)
        oneResp = singleModifier(1)
        'If the modifier exists as a key in the ModifierList, add the row and the value into the dictionary associated to that key
        'If the modifier has already been entered in modifierList, add the row and value to the sub-dictionary
        If (Not modifierList.Exists(oneMod)) Then
            modifierList.Add oneMod, New Dictionary
        End If
        'ERROR IS THROWN ON LINE BELOW
        modifierList(oneMod).Add theRow, oneResp
    Next
Next theRow

上面的代码只是创建哈希表。在那之后创建列非常简单,所以出于这个问题的目的,我将省略它。

我已经在Patrick O'Bierne的优秀博客上安装了DictionaryKeyValue类的插件。然而,我在底部的第三行收到一个运行时错误("438"),上面写着"Object不支持此属性或方法",并用注释标记。第一个Dictionary.Add方法运行良好。有什么想法吗?这可能是类实现中的错误吗?

看起来你需要更明确一点的

代替

modifierList(oneMod).Add theRow, oneResp

试试这个

modifierList.KeyValuePairs(oneMod).value.Add theRow, oneResp

最新更新