Solved

Manipulating Arrays

Posted on 1998-11-23
4
170 Views
Last Modified: 2010-05-03
Please could someone offer the shortest code to do the following:

Delete an element in the middle of a dynamic array meaning all the elements above that one in the set are pushed down one.

Test whether a variable equals that of an element in an array.


Thanks
0
Comment
Question by:Jesus112398
  • 2
4 Comments
 

Expert Comment

by:atelford
ID: 1446408
It would be mouch easier if you used a collection , then you would have no need to resize the array after a delete , but thats just my opinion
0
 
LVL 3

Accepted Solution

by:
CesarGon earned 70 total points
ID: 1446409
For the first problem: the fastest way to do that is to iterate the array from the element deleted up to the last minus one, assigning to each element the value of the next. After that, just ReDim the array to its original length minus one. Example:

  Dim a(1 To 10) As String
  ...

  'let's delete the 4th element.
  Dim l As Long
  For l = 4 To 9
    a(l) = a(l + 1)
  Next l
  ReDim a(1 To 9) As String

For the second problem: iterate the array checking one element at a time.

These are the fastest ways, as measured by me. Nevertheless, collections and/or dictionaries could do better in some cases.

Regards,
César.

0
 
LVL 1

Expert Comment

by:yfang
ID: 1446410
Comments to the proposed answer:
Redim will clear the contents of the array and you will lose the values of all the elements.  You have to use keyword PRESERVE to keep the contents of the elements:

ReDim preserve a(1 To 9) As String

Good luck

0
 
LVL 3

Expert Comment

by:CesarGon
ID: 1446411
Of course you have to use Preserve!. I forgot writing it in my code. Sorry about my mistake and thanks for pointing it out.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Window placement 17 65
MsgBox 2 38
bit defender blocks good applications 2 55
Windows Script Host failed (Access is denied.) error 6 85
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now