2014-09-05 140 views
1

簡而言之,我們從一個不是我們自己的應用程序中獲得了一個excel報告,它將來自單個表單的所有用戶響應放入每個提交的單個單元格中。我正在爲Office寫一個VBA宏:mac excel。字典詞典 - vba Office for Mac

因此,在「修飾」單細胞列可能會出現像這樣:

細胞1: 1日教師選擇|岡德森; 2教師的選擇|巴恩斯;你喜歡爽|是; ?超人或綠巨人|綠巨人

小區2: 1日教師選擇|史密斯; 2教師的選擇|岡德森;你喜歡爽|是

細胞3:? 1日教師選擇| Wulfenbach; 2教師的選擇|豐塔納;你喜歡爽|沒有;超人和綠巨人|超人

細胞4:?? 1日教師選擇|豐塔納; 2教師的選擇|史密斯;做好你喜歡爽|是,超人或綠巨人|超人

細胞5:???? 1日教師選擇|外差; 2教師的選擇| 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 

上面的代碼只是創建哈希表。之後創建列是非常簡單的,所以我將把它留在這個問題的目的之外。

我已經安裝了外接詞典KEYVALUE帕特里克O'Bierne在his excellent blog創建類。但是,我在底部的第三行發現運行時錯誤('438'),指出「對象不支持此屬性或方法」,並標註了註釋。第一個Dictionary.Add方法工作正常。有任何想法嗎?這可能是類實現中的錯誤嗎?

回答

1

看來你需要多一點明確的

而不是

modifierList(oneMod).Add theRow, oneResp 

試試這個

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

這就是它!雖然顯然字典要求你有一個不是整數的索引(yuck!),所以我不得不稍微調整你的答案: 'modifierList.KeyValuePairs(oneMod).value.Add CStr(theRow),oneResp' – KCL 2014-09-07 06:16:17