Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 265
Default Working with arrays

Hi, I have some operations that i think VB has some easier way to accomplish
or may be a function:

1)The sum of all the array elements
2)Get just the array elements different of "" or 0
3)Get juste the array elements that they are not repeated (i mean
arr1(1,1,2,z,z,3,3,4,4,4,1,1,4,5) to arr1(1,2,3,4,5,z)

TIA
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Working with arrays

The easy one first:

#1:
Dim myArr As Variant
myArr = Array(1, 1, 2, "z", "z", 3, 3, 4, 4, 4, 1, 1, 4, 5)
MsgBox Application.Sum(myArr)

#2. I think that you'll have to loop through the array.

Option Explicit
Sub testme02()

Dim myArr1 As Variant
Dim myArr2() As Variant
Dim iCtr As Long
Dim eCtr As Long

myArr1 = Array(1, 1, 2, "z", "z", 3, 3, 4, 4, 4, 1, 1, 4, 5)

eCtr = 0
For iCtr = LBound(myArr1) To UBound(myArr1)
If Application.IsNumber(myArr1(iCtr)) Then
eCtr = eCtr + 1
ReDim Preserve myArr2(1 To eCtr)
myArr2(eCtr) = myArr1(iCtr)
End If
Next iCtr

If eCtr = 0 Then
MsgBox "No elements"
Else
For iCtr = LBound(myArr2) To UBound(myArr2)
MsgBox iCtr & "--" & myArr2(iCtr)
Next iCtr
End If

End Sub

#3. You can build the array, but check using application.match(). If that
returns an error, then the value isn't in the second array.

Option Explicit
Sub testme03()

Dim myArr1 As Variant
Dim myArr2() As Variant
Dim iCtr As Long
Dim eCtr As Long

myArr1 = Array(1, 1, 2, "z", "z", 3, 3, 4, 4, 4, 1, 1, 4, 5)

ReDim myArr2(1 To 1)
myArr2(1) = myArr1(1)

eCtr = 1
For iCtr = LBound(myArr1) + 1 To UBound(myArr1)
If IsError(Application.Match(myArr1(iCtr), myArr2, 0)) Then
eCtr = eCtr + 1
ReDim Preserve myArr2(1 To eCtr)
myArr2(eCtr) = myArr1(iCtr)
End If
Next iCtr

For iCtr = LBound(myArr2) To UBound(myArr2)
MsgBox iCtr & "--" & myArr2(iCtr)
Next iCtr

End Sub

There are lots of other techniques. You could use a Collection or a Dictionary
object. If you want to try the collection, you may want to look at John
Walkenbach's site:

http://j-walk.com/ss/excel/tips/tip47.htm

His routine has an option sort, too.


filo666 wrote:

Hi, I have some operations that i think VB has some easier way to accomplish
or may be a function:

1)The sum of all the array elements
2)Get just the array elements different of "" or 0
3)Get juste the array elements that they are not repeated (i mean
arr1(1,1,2,z,z,3,3,4,4,4,1,1,4,5) to arr1(1,2,3,4,5,z)

TIA


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 783
Default Working with arrays

filo666 wrote:
Hi, I have some operations that i think VB has some easier way to accomplish
or may be a function:

1)The sum of all the array elements
2)Get just the array elements different of "" or 0
3)Get juste the array elements that they are not repeated (i mean
arr1(1,1,2,z,z,3,3,4,4,4,1,1,4,5) to arr1(1,2,3,4,5,z)

TIA


If the functions in the freely downloadable file at
http://home.pacbell.net/beban are available to your workbook

For 2)

myArr = Application.Transpose(myArr)
myArr = ArrayRowFilter1(ArrayRowFilter1(myArr, 1, "", "<"), 1, 0, "<")
myArr = Application.Transpose(myArr)

for 3)

myArr = ArrayUniques(myArr, , "1horiz", False)

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
Working with arrays as arguments HB Excel Worksheet Functions 6 September 30th 09 02:48 PM
SUMPRODUCT with 3 arrays not working Kierano Excel Worksheet Functions 1 October 16th 06 03:37 PM
Working with Arrays Judy Excel Worksheet Functions 1 January 11th 06 12:22 AM
Excel2000: Need help on UDF (working with arrays) Arvi Laanemets Excel Programming 2 April 22nd 05 05:51 PM
working on 2 different worksheets....arrays? monika Excel Programming 6 February 10th 04 11:40 AM


All times are GMT +1. The time now is 03:33 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"