2014-09-05 11 views
1

Вкратце, мы получаем отчет в excel из приложения, которое не является нашим, которое помещает все ответы пользователя из одной формы в одну ячейку для каждой заявки. Я пытаюсь написать макрос VBA для Office: mac Excel.Словарь словарей - vba Office for Mac

Таким образом, одна ячейка в «модификаторов» столбец может выглядеть так:

Cell 1: первый учитель Выбор | Gunderson; второй учитель Выбор | Barnes, как Желе ли вам | Да; ? Супермен или Hulk | Hulk

Cell 2: первый учитель Выбор | Смит, второй учитель Выбор | Gunderson, как Желе ли вам | Да

Cell 3:? первый учитель Выбор | Wulfenbach, второй учитель Выбор | Fontana, как Желе У вас | Нет, Супермен или Hulk | Супермен

Cell 4:? первый учитель Выбор | Fontana, второй учитель Выбор | Смит; Do вы как Желе | Да, Супермен или Hulk | Супермен

Cell 5:?? первый учитель Выбор | Гетеродин, второй учитель Выбор | Wulfenback, как Желе ли вам | Да, Супермен или Hulk | Супермен

Моя цель - собрать все вопросы (текст между; и |), создайте столбец для каждого вопроса, затем заполните столбцы ответами. Я представляю хэш-таблицу, как:

{ 
"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 

Приведенный выше код просто создает хэш-таблицу. Создание столбцов после этого довольно просто, поэтому я оставлю это для целей этого вопроса.

Я установил надстройку словарь и KeyValue классы, созданные Patrick O'Bierne в his excellent blog. Однако я получаю ошибку времени выполнения («438»), в которой «Объект не поддерживает это свойство или метод» в третьей строке снизу, помеченной комментарием. Первый метод Dictionary .Add работал нормально. Есть идеи? Может ли это быть ошибкой в ​​реализации класса?

ответ

1

Кажется, вы должны быть немного более явным

Вместо

modifierList(oneMod).Add theRow, oneResp 

Попробуйте

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

Это было его! Хотя, по-видимому, словари REQUIRE у вас есть индекс, который НЕ является целым (yuck!), Поэтому мне пришлось немного подправить ваш ответ: 'modifierList.KeyValuePairs (oneMod) .value.Add CStr (theRow), oneResp' – KCL

Смежные вопросы