Results 1 to 4 of 4

Thread: Array(s) Resolved

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2003
    Location
    Los Angeles
    Posts
    126

    Array(s) Resolved

    Hi everyone ...
    brand new to this forum and just learning vb/vba.
    In Excel, i am trying to write a function.
    I have several named ranges and have figured out how to loop through the ranges and get a total of all cells in all ranges.
    Now what I am trying to figure out is ...
    I want to create 4 arrays. 1 for each named range in this spreadsheet(all the ranges are different lenghts and will be added to often). I then want to loop through each range and assign each cells value to one element of the array, reDim and Preserve the array, then get the next value. Once I have all the elements, I can (hopefully) perform some calculations based on certain criteria.
    I am calling the function from a command button click event.
    Any help, ideas, or direction to examples is appreciated.
    Thanks
    Last edited by IGBP; May 16th, 2003 at 09:10 AM.

  2. #2
    Fanatic Member WorkHorse's Avatar
    Join Date
    Jul 2002
    Location
    Where you live.
    Posts
    591
    To assign the values to an array, you need to Dim the array variable with parentheses () with nothing in them. That lets VB know the variable is an array but doesn't have a fixed number of elements. You must ReDim the array with the low-bound value before you can use it. As you loop through the cells in the range, ReDim Perserve the array, incrementing the the upper-bound index by one. Here's a funtion:

    VB Code:
    1. Function RangeToArray(ByVal strRangeName As String)
    2.  
    3.     Dim c As Range
    4.     Dim ReturnArray()
    5.     ReDim ReturnArray(0)
    6.    
    7.     For Each c In Range(strRangeName)
    8.         ReDim Preserve ReturnArray(UBound(ReturnArray) + 1)
    9.         ReturnArray(UBound(ReturnArray)) = c.Value
    10.     Next
    11.  
    12.     RangeToArray = ReturnArray
    13.  
    14. End Function
    You can call it like this:

    VB Code:
    1. Sub MyClickEvent()
    2.  
    3.     Dim i As Long
    4.    
    5.     Dim MyRangeValues()
    6.     ReDim MyRangeValues(0)
    7.  
    8.     MyRangeValues = RangeToArray("MyRangeName")
    9.  
    10.     For i = 1 To UBound(MyRangeValues)
    11.         Debug.Print MyRangeValues(i)
    12.     Next
    13.  
    14. End Sub
    If you want, you can use another variable in the Function to keep track of incrementing the upper-bound index of the array instead of checking the UBound each time.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    May 2003
    Location
    Los Angeles
    Posts
    126
    WorkHorse,
    Excellent Post. Thank-you very much.
    I will try it and let you know.

    IGBP

  4. #4

    Thread Starter
    Lively Member
    Join Date
    May 2003
    Location
    Los Angeles
    Posts
    126

    Resolved

    Thanks WorkHorse

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width