Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default Array element

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.

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8
Default Array element

if newarr(primat) = 15 then "delete element"
becomes
if newarr(primat) = 15 then newarr(primat) = 0
Pascal


"Andrea" wrote in message
...
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.



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Array element

a P Daulton has implied, there is no command to delete an element of an
array.

j = 1
for primat = 1 to 50
if newarr(primat) < 15 then
newarr(j) = newarr(primat)
j = j + 1
end if
Next
for i = j to 50
newarr(i) = 0
Next

might be what you want.

Sub tester1()
Dim newarr(1 To 50)
For i = 1 To 50
If Rnd() < 0.25 Then
newarr(i) = 15
Else
newarr(i) = i
End If
Next
Range("A1:A50").Value = Application.Transpose(newarr)
j = 1
For primat = 1 To 50
If newarr(primat) < 15 Then
newarr(j) = newarr(primat)
j = j + 1
End If
Next

For i = j To 50
newarr(i) = 0
Next
Range("B1:B50").Value = Application.Transpose(newarr)

End Sub

--
Regards,
Tom Ogilvy

"Andrea" wrote in message
...
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.



  #4   Report Post  
Posted to microsoft.public.excel.programming
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
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 783
Default Array element

Tom Ogilvy wrote:
a P Daulton has implied, there is no command to delete an element of an
array.

j = 1
for primat = 1 to 50
if newarr(primat) < 15 then
newarr(j) = newarr(primat)
j = j + 1
end if
Next
for i = j to 50
newarr(i) = 0
Next

might be what you want.


If you substitute the following for the second loop you probably have it:

ReDim Preserve newarr(1 To j - 1)

Alan Beban


  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Array element

Depends on whether i want to resize the array or not. Delete would imply
that but is not definitive. Resizing, as you know, but maybe the OP
doesn't, would require that the array be dynamic. Looping with hard coded
bounds would imply fixed - but again, specifics were not provided.

--
Regards,
Tom Ogilvy

"Alan Beban" wrote in message
...
Tom Ogilvy wrote:
a P Daulton has implied, there is no command to delete an element of an
array.

j = 1
for primat = 1 to 50
if newarr(primat) < 15 then
newarr(j) = newarr(primat)
j = j + 1
end if
Next
for i = j to 50
newarr(i) = 0
Next

might be what you want.


If you substitute the following for the second loop you probably have it:

ReDim Preserve newarr(1 To j - 1)

Alan Beban



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Counting Array element asingh Excel Worksheet Functions 4 April 12th 10 03:30 PM
Can one store a string in a Array element? [email protected] Excel Discussion (Misc queries) 1 April 17th 07 12:53 PM
Search array and return element No Ron Excel Worksheet Functions 7 May 17th 06 05:27 AM
Permutations of an array element < to a value Bruce Excel Worksheet Functions 3 January 31st 06 05:00 PM
deleting array element michael Excel Programming 0 December 18th 03 09:55 PM


All times are GMT +1. The time now is 10:10 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"