РЕДАКТИРОВАТЬ: Так что это было действительно очевидно, мне нужен модификатор «Сохранить» для оператора Redim.
Я пытаюсь написать UDF в Excel VBA, который берет столбец цен и выбирает n самые высокие / самые низкие покупки / продажи (отмечены в соседнем столбце) и выводит «да» или ничего, в зависимости от того, соответствует ли данная ячейка этим критериям.
Например, для n = 2 вход может выглядеть примерно так:
Цена Тип Результат
150 купить да
170 продать
146 продать да
167 купить
125покупка
164 продажа да
Итак, я просматриваю столбец цен, помещая каждую цену в массив «купить» или «продать», но по какой-то причине, когда смотрю на результаты, толькопоследние записи отображаются правильно, а остальные записи = 0.
Вот код, который я получил:
Function included(prices As Range, entry As Range)
n = 2
bc = 1 'counter for the buys
sc = 1 'counter for the sells
Ub = 1 'upper bound for buys
Us = 1 'upper bound for sells
Dim b() As Double 'dynamic array for buys
Dim s() As Double 'dynamic array for sells
'collect the buys and the sells into two arrays
For i = 1 To prices.Rows.Count
amt = prices(i).Value
If prices(i).Offset(0, 1) = "buy" Then 'add to buy list
ReDim b(1 To Ub) 'reapply length
b(bc) = amt 'add the entry
Ub = UBound(b) + 1 'add one to the length
bc = bc + 1 'increase the counter by 1
ElseIf prices(i).Offset(0, 1) = "sell" Then
'add to s
ReDim s(1 To Us)
s(sc) = amt
Us = UBound(s) + 1
sc = sc + 1
Else
MsgBox "nothing"
End If
Next
'check the resulting arrays (only the last value in b() and s() print out)
For i = 1 To UBound(b)
If b(i) <> 0 Then
MsgBox b(i)
End If
Next
For i = 1 To UBound(s)
If s(i) <> 0 Then
MsgBox s(i)
End If
Next
'still to do:
' sort the buy and sell arrays in ascending and descending order respectively
' truncate the arrays to length n
' check if entry is in one of the resulting arrays
End Function
Я действительно новичок в VBA (и простоНачинающий Python), так может это что-то очевидное?Заранее благодарю за любую помощь!