Consulting

Results 1 to 12 of 12

Thread: Solved: Filling the array

  1. #1
    VBAX Regular
    Joined
    Dec 2004
    Posts
    93
    Location

    Question Solved: Filling the array

    I am trying to fill the array by looping through the range and selecting values by parameter. The code that I am trying does not make the job. Can you help fixing the code, please?

    [VBA] For i = 1 To r_Major.Count
    If Me.cmbRegion.Value = r_rgn(i).Value And _
    Me.cmbPeriod.Value = r_Date(i).Value And _
    Me.lstDetails.Value = Rg(i).Value And _
    Me.cmbMajor.Value = r_Major(i) And _
    Me.cmbMinor.Value = r_Minor(i) Then
    k = k + 1
    ReDim Arr(k - 1, 1)
    Arr(k - 1, 0) = Rg(i)
    Arr(k - 1, 1) = r_rgn(i).Offset(0, 1)
    End If
    Next i
    lstSubDetails.List = Arr[/VBA]

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    If you don't use Preserve, you clear the array when you redim

    [vba]
    ReDim Preserve Arr(k - 1, 1)

    [/vba]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    VBAX Regular
    Joined
    Dec 2004
    Posts
    93
    Location
    Quote Originally Posted by mdmackillop
    If you don't use Preserve, you clear the array when you redim

    [vba]
    ReDim Preserve Arr(k - 1, 1)

    [/vba]
    When I use Preserve I get Subscript out of range error and ReDim Preserve Arr(k - 1, 1) is highlighted.

  4. #4
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Can you post your workbook?
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi Kaiser,
    I've looked a little more closely.
    You cannot redim the first dimension of an array, only the last one. A workaround for this is to set your initial array to (1,100) instead of (100,1) for example, redim it to (1,k-1) and then transpose it for use in filling your listbox. Clear???
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  6. #6
    VBAX Regular
    Joined
    Dec 2004
    Posts
    93
    Location
    Quote Originally Posted by mdmackillop
    Hi Kaiser,
    I've looked a little more closely.
    You cannot redim the first dimension of an array, only the last one. A workaround for this is to set your initial array to (1,100) instead of (100,1) for example, redim it to (1,k-1) and then transpose it for use in filling your listbox. Clear???
    Yes, got it. Thanks a lot.

  7. #7
    VBAX Regular
    Joined
    Dec 2004
    Posts
    93
    Location
    Just noticed that if there is only one entry in the array then Transpose doesn't work. How to workaround this one?

  8. #8
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Can you simply add an If function if k=1 which presumably will be the case, and just write the result to the listbox using AddItem?
    Last edited by mdmackillop; 10-15-2006 at 02:38 AM. Reason: K value corrected.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  9. #9
    VBAX Regular
    Joined
    Dec 2004
    Posts
    93
    Location
    Quote Originally Posted by mdmackillop
    Can you simply add an If function if k=1 which presumably will be the case, and just write the result to the listbox using AddItem?
    I need help here. How do you tell which Index to use in the list so that it's filled horisontally? What I am doing is still filling the list vertically.
    [VBA]... if k>1 then
    lstSubDetails.list=Application.Function.Transpose (Arr)
    Else
    lstSubDetails.AddItem Arr(0,0)
    lstSubdetails.AddItem Arr(1,0)
    End If
    [/VBA]

  10. #10
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    What I am doing is still filling the list vertically
    It is, and I can't see the reason for it.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  11. #11
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Eventually found the solution!
    [vba]
    With ListBox1
    .AddItem
    .List(0, 0) = arr(0, 0)
    .List(0, 1) = arr(1, 0)
    End With

    [/vba]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  12. #12
    VBAX Regular
    Joined
    Dec 2004
    Posts
    93
    Location
    Thank you mdmckillop. That's what I need.

Posting Permissions

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