Thread: Array element
View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
Alan Beban[_2_] Alan Beban[_2_] is offline
external usenet poster
 
Posts: 783
Default Array element

Andrea wrote:
hello,
Someone could tell me which is the instruction to delete
an Array element ? It could seems a silly question, but I
didn't find it inside the VB manual.
I submit a simply example to explain myself better:

For primat = 1 To 50
if newarr(primat) = 15 then "delete element"
Next primat

here I have a "newarr" Array and verifying its elements, I
want to delete all 15 values.
Many thanks.
Andrea.

I don't know what you mean by "verifying its elements"; but in any
event, in your workbook VB Editor click on Tools|References and check
Microsoft Scripting Runtime. Then, assuming you have a 1-based,
N-element, 1-D array named "newarr", call the following function with

DeleteElems newarr, 15

to delete the elements equal to 15 (if any) and reduce the upper bound
from N to N-the number of occurrences of 15:

Function DeleteElems(InputArray, ElemValue) As Boolean
Dim x As Dictionary, Elem As Variant
Dim Num As Long, i As Long
Set x = New Dictionary
Num = 0
For Each Elem In InputArray
x.Add CStr(Num), Elem
If Elem = 15 Then x.Remove (CStr(Num))
Num = Num + 1
Next
InputArray = x.Items
ReDim Preserve InputArray(1 To UBound(InputArray) + 1)
End Function

Alan Beban