473,385 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Passing an array name to a function

Hi-
I am working on a project in VBA in Excel 2000. I have a loop in which I want to append a certain (previously defined) array to another array.

Expand|Select|Wrap|Line Numbers
  1.  
  2. example:
  3.  
  4. array1 = array("a","b")  'these are in reality longer than this
  5. array2 = array("c","d")
  6.  
  7. array3 = array("1","2","3","4")
  8.  
  9. for j = 0 to Somevar
  10. If flagarr(j) = 1 Then
  11.  
  12.             For i = 0 To UBound(array1)
  13.                 ReDim Preserve array3(UBound(array3) + 1)
  14.                 array3(UBound(array3)) = array1(i)
  15.  
  16.             Next i
  17.  
  18.  End If
  19. Next j
  20.  

My question is how to pass the names of array1 and array2 to this loop so I don't have to write out a for i = 0 to UBound(array1) loop for each individual array I want to add

Thanks for any help you can give
(there is probably a more effective way to do this as well, so if you have any input on that also.....)
Jan 30 '08 #1
11 2119
Expand|Select|Wrap|Line Numbers
  1. Sub Test()
  2.    Dim all() As Variant, array1() As Variant, array2() As Variant, array3() As Variant
  3.  
  4.    array1 = Array("a", "b")
  5.    array2 = Array("c", "d")
  6.    array3 = Array("1", "2", "3", "4")
  7.  
  8.    Call Merge_Arrays(all, array1, False)
  9.    Call Print_Array(all)
  10.  
  11.    Call Merge_Arrays(all, array2, False)
  12.    Call Print_Array(all)
  13.  
  14.    Call Merge_Arrays(all, array3, True)
  15.    Call Print_Array(all)
  16.  
  17.    On Error GoTo ERR_HANDLER
  18.    Debug.Print array2(0)
  19.    Debug.Print array3(0)
  20.  
  21.    Exit Sub
  22. ERR_HANDLER:
  23.    Debug.Print Err.Description
  24.    Resume Next
  25. End Sub
  26.  
  27. Private Sub Merge_Arrays(ByRef all As Variant, ByRef source_array As Variant, clear_source As Boolean)
  28.    Dim i As Integer, j As Integer
  29.  
  30.    On Error GoTo SET_INDEX
  31.  
  32.    j = UBound(all) + 1
  33.  
  34.    For i = 0 To UBound(source_array)
  35.       ReDim Preserve all(j)
  36.       all(j) = source_array(i)
  37.       j = j + 1
  38.    Next i
  39.  
  40.    If clear_source Then
  41.       Erase source_array
  42.    End If
  43.  
  44.    Exit Sub
  45. SET_INDEX:
  46.    j = 0
  47.    Resume Next
  48. End Sub
  49.  
  50. Private Sub Print_Array(ByVal arr As Variant)
  51.    Dim i As Integer
  52.  
  53.    For i = 0 To UBound(arr)
  54.       Debug.Print arr(i)
  55.    Next i
  56.  
  57.    Debug.Print "-----------------------"
  58. End Sub
Jan 31 '08 #2
Killer42
8,435 Expert 8TB
One general suggestion - use LBound() function rather than assuming all arrays start at zero. Some of us prefer to use numbers that make sense to people rather than computers.
Jan 31 '08 #3
One general suggestion - use LBound() function rather than assuming all arrays start at zero.
That's a great suggestion, and for some reason, I have never even thought of using LBound. Thanks.
Jan 31 '08 #4
kadghar
1,295 Expert 1GB
Other general suggestion:
Try to avoid using ReDim inside a FOR, it's not always possible, but you can always try.
e.g.
Instead of:

Expand|Select|Wrap|Line Numbers
  1.    j = UBound(all) + 1
  2.  
  3.    For i = 0 To UBound(source_array)
  4.       ReDim Preserve all(j)
  5.       all(j) = source_array(i)
  6.       j = j + 1
  7.    Next i
something like this will avoid using REDIM inside the FOR, and would be more general for any lbound, ubound of the arrays:

Expand|Select|Wrap|Line Numbers
  1. j= ubound(all) + 1 - lbound(source_array) 
  2. redim preserve all (j + ubound(source_array))
  3. for i = lbound(source_array) to ubound(source_array)
  4.     all(i + j) = sourcearray(i)
  5. next
note: it's not necessary to use j, but it makes the code easier to read. It'll be the index that source_array(0) would use in "all", a little bit confusing, but believe me, it works for any lbound of the source array, even if its not zero.
Jan 31 '08 #5
Killer42
8,435 Expert 8TB
Thanks kadghar.

Yeah, I seem to recall ReDim is a pretty expensive statement. Better to do it once up front.
Jan 31 '08 #6
Thanks for all the helpful replies. I do have one little twist on the problem, though, and perhaps it is best approached by the merge arrays function written by WinB: I want to be able to either append an array or not depending on a condition that is set in a dialogbox, so I would be choosing from a number of them, but not necessarily all of them.

For example (in some pcode):


a = {1,2,3}, b = {4,5,6}, c = {a,b,c}

if condition 1 = true and condition 2 =false and condition3 = false Then
array = {1,2,3}
if condition 1 = true and condition2=false and condition3=true Then
array = {1,2,3,a,b,c}

etc. (so my original motivation was to be able to step through a list of the arrays and pass them to the merging function)

Perhaps I'm making life unnecessarily difficult..... lol I might revert back to the old inputbox.
Feb 1 '08 #7
bump

---------------->
Feb 5 '08 #8
Please forgive the double post, but it seems my ability to edit has disappeared (or been taken away?).

Let me delineate my problem in a different way:

how can I reference elements of an array whose name is defined as a string elsewhere in the program?

for example, str = "x"

then later str = Array(1,2,3) ( I know that this won't work in reality)

and be able to reference say x(1) after that
Feb 5 '08 #9
Killer42
8,435 Expert 8TB
I've been thinking about this for a while. The only thing I can come up with is to try checking out the Microsoft Scripting Runtime (or script control). I believe it allows you to execute code dynamically from a string. Whether it will provide the ability to dynamically identify your arrays in the way you describe, I have no idea.
Feb 6 '08 #10
I had played around with the Application.Eval() function, but it seems to have different implementations in different dialects and changes with every other new release lol.... I thought about building a 2d array with Array(old,new) and then "flattening" it out to a 1D in the end.

My final solution was less than elegant, but it gets the job done. I have a large constant array in the code and the portions of the array that are desired are "searched" for and placed in a new array.

Anyway, thanks much for thinking about it.
Feb 6 '08 #11
Killer42
8,435 Expert 8TB
Oh well, as long as you've got it working.

Oh, and by the way. You can only edit messages for a limited time after posting them. The limit used to be 5 minutes, but I think it's one hour now.
Feb 6 '08 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: Kevin Lyons | last post by:
Hello, I am trying to get my select options (courses) passed correctly from the following URL: http://www.dslextreme.com/users/kevinlyons/selectBoxes.html I am having difficulty getting the...
3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
8
by: kalinga1234 | last post by:
there is a problem regarding passing array of characters to another function(without using structures,pointer etc,).can anybody help me to solve the problem.
11
by: truckaxle | last post by:
I am trying to pass a slice from a larger 2-dimensional array to a function that will work on a smaller region of the array space. The code below is a distillation of what I am trying to...
11
by: John Pass | last post by:
Hi, In the attached example, I do understand that the references are not changed if an array is passed by Val. What I do not understand is the result of line 99 (If one can find this by line...
6
by: ged | last post by:
Hi, i am a oo (c#) programmer, and have not used javascript for a while and i cant work out how javascript manages its references. Object References work for simple stuff, but once i have an...
0
by: kencana | last post by:
hi All, I got problem in passing data (more than one) from soap client to the soap server. but if i only passing one data at a time, it works successfully.. The following is the error message i...
20
by: jason | last post by:
Hello, I'm a beginning C programmer and I have a question regarding arrays and finding the number of entries present within an array. If I pass an array of structures to a function, then...
8
by: S. | last post by:
Hi all, Can someone please help me with this? I have the following struct: typedef struct { char *name; int age; } Student;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.