Iteratively removing data from a VBA array

K

Kostas

I initially wanted to be able to truncate an array as needed by the
code. An answer along with the initial code was found in a GoogleGroup.


When though an array that has stored numeric values is truncated, the
nature of the data stored is changed. For example, the values of the
array prior to truncation would be plain numbers e.g. 1, 2, 3 etc
whereas after running the truncation section, the remaining values
change into "1", "2", "3", etc.

I have attempted to convert to an Integer value but as shown below, it
was ineffective. Comparisons of data derived from the array with
distinctly defined numbers numbers are possible. Comparisons with
variables to which values have been pre-assigned are unfortunately not
possible.

As this does not allow any further array manipulation & valid data
extraction, I have reached a standstill and would appreciate any
suggestions anyone would have to offer.

Many thanks

Kostas

PS. VBA code example of array truncation as follows :



Sub Array_truncation()


vv = Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)


var1 = 2

Debug.Print ""
Debug.Print "New Run"

For Each x In vv
Debug.Print x

Next x

If vv(2) = var1 Then Debug.Print "2=2 ok" Else Debug.Print "problem at
stage 1"

Debug.Print (2)
Dim Unique
Unique = Chr(255)


' This example is "Zero" based, so index is 4 & 8
vv(4) = Unique

'Now, delete them...
vv = Filter(vv, Unique, False)

Debug.Print ""

For Each x In vv

Debug.Print x

Next x

If vv(2) = var1 Then Debug.Print "2=2 ok" Else Debug.Print "problem at
stage 2"


vv(4) = Unique

'Now, delete them...
vv = Filter(vv, Unique, False)

Debug.Print ""

Dim x2 As Integer


For Each x In vv
x = x2
Debug.Print x

Next x

If vv(2) = var1 Then Debug.Print "2=2 ok" Else Debug.Print "problem at
stage 3"



End Sub
 
G

Guest

Look at the help on the Filter function. It works on an array of strings, so
your values are converted to strings when you use the filter function.

Your problem on stage 3 is you set all your array values to the integer 0
and then compare them, i.e. element vv(2) (now zero) you compare it to
the number 2 and naturally that fails.

here is the output modified to show the type of the value:

New Run
0 Integer
1 Integer
2 Integer
3 Integer
4 Integer
5 Integer
6 Integer
7 Integer
8 Integer
9 Integer
2=2 ok
2

0 String
1 String
2 String
3 String
5 String
6 String
7 String
8 String
9 String
problem at stage 2

0 Integer
0 Integer
0 Integer
0 Integer
0 Integer
0 Integer
0 Integer
0 Integer
problem at stage 3

Not sure your problem is any more serious than not understanding that filter
works with strings.
 
K

Kostas

The problem is that the filter function produces a string based array.
Therefore, Once any array has been filtered it is converted from
numeric to string-based.

I believe that the easiest way around my problem would be to re-convert
the truncated array from string based to numeric-based array, every
time it is truncated. Unfortunately though, I have been unable to track
down the code that would allow me to do that.

Any help appreciated

Many thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top