I am trying to store a list from a multi dimensional array in a dictionary using VBA.
The second last line gives a wrong dimension error. I know the keyArray is 2 dimensions but I want to store the first item in keyArray which is a list then so on.
keys is an array which stores the primary keys from my table there isn’t any issues with that.
For I = 0 To 15
'Data which should be going in for that key
If I < 8 Then
keyArray(I, 0) = dptData(I)
Else
keyArray(I, 0) = data2(I - 8)
End If
keyArray(I, 1) = keys(I, 0)
keyArray(I, 2) = keys(I, 1)
keyArray(I, 3) = keys(I, 2)
dict.Add keys(I, 0) & " " & keys(I, 1) & " " & keys(I, 2), keyArray(I)
Next I
asked Jul 12, 2018 at 18:35
8
Omitting a subscript from a 2D array doesn’t automatically slice it into a 1D array. Use this function if you need complete efficiency and a function that can be used outside of Excel. This function uses the Index
worksheet function to perform the slicing, so won’t work outside of Excel (and is less efficient, too).
Say you named your function Slice
, this should do it (assuming keyArray
is 0-based):
dict.Add keys(I, 0) & " " & keys(I, 1) & " " & keys(I, 2), Slice(keyArray, 0)(I)
answered Jul 12, 2018 at 18:56
Mathieu GuindonMathieu Guindon
69.8k8 gold badges107 silver badges235 bronze badges
Search code, repositories, users, issues, pull requests…
Provide feedback
Saved searches
Use saved searches to filter your results more quickly
Sign up
Trying to set up a multi-dim array..not sure what im doing wrong here…the number of the indices are correct the whole way down…
Dim arrPhrases(1 To 63, 1 To 4) As Variant
arrPhrases(1)(1) = «Restore server»
arrPhrases(1)(2) = «cleared out»
arrPhrases(1)(3) = «add»
arrPhrases(1)(4) = «b is for bat»
arrPhrases(2)(1) = «a is for apple»
arrPhrases(2)(2) = «b is for bat»
arrPhrases(2)(3) = «b is for bat»
arrPhrases(2)(4) = «b is for bat»
arrPhrases(3)(1) = «b is for bat»
arrPhrases(3)(2) = «b is for bat»
arrPhrases(3)(3) = «b is for bat»
arrPhrases(3)(4) = «b is for bat»
…goes on …
Я пытаюсь сохранить список из многомерного массива в словаре с помощью VBA.
Вторая последняя строка дает неправильную ошибку измерения. Я знаю, что keyArray имеет 2 измерения, но я хочу сохранить первый элемент в keyArray, который является списком, а затем и так далее.
Keys — это массив, в котором хранятся первичные ключи из моей таблицы, с этим нет никаких проблем.
For I = 0 To 15
'Data which should be going in for that key
If I < 8 Then
keyArray(I, 0) = dptData(I)
Else
keyArray(I, 0) = data2(I - 8)
End If
keyArray(I, 1) = keys(I, 0)
keyArray(I, 2) = keys(I, 1)
keyArray(I, 3) = keys(I, 2)
dict.Add keys(I, 0) & " " & keys(I, 1) & " " & keys(I, 2), keyArray(I)
Next I
1 ответ
Лучший ответ
Пропуск нижнего индекса в 2D-массиве не приводит к автоматическому разделению его на одномерный массив. Используйте эту функцию, если вам нужна полная эффективность и функция, которую можно использовать вне Excel. Эта функция использует функцию рабочего листа Index
для выполнения нарезки, поэтому не будет работать вне Excel (и тоже менее эффективен).
Допустим, вы назвали свою функцию Slice
, это должно сработать (при условии, что keyArray
начинается с 0):
dict.Add keys(I, 0) & " " & keys(I, 1) & " " & keys(I, 2), Slice(keyArray, 0)(I)
0
Mathieu Guindon
12 Июл 2018 в 21:56