Consulting

Results 1 to 4 of 4

Thread: Redim Preserve array problem

  1. #1
    VBAX Regular
    Joined
    Feb 2014
    Posts
    9
    Location

    Redim Preserve array problem

    Hi I have a multicolumn multiselect listbox. I would like to transfer the selected items in this listbox to an array, which I will then use to populate another listbox.

    It keeps coming up with subscript out of range and i cant figure out why?!?!?!?

    Doing my head in. Please help....NB this is the first time ive used Redim Preserve.

    Sub listboxlist()
    
    
    i = 0
    j = 0
    Z = 0
    
    
    Dim ar_temp()
    
    
    For i = 0 To SortReg.Controls("Listbox2").ListCount - 1
        If SortReg.Controls("Listbox2").Selected(i) = True Then
        Z = Z + 1
        ReDim Preserve ar_temp(0 To Z - 1, 0 To 7)
            For j = 0 To 7
                ar_temp(Z - 1, j) = SortReg.Controls("Listbox2").List(i, j)
            Next
        End If
    Next
    
    
    SortReg.Controls("Listbox8").List = ar_temp
    
    
    End Sub

  2. #2
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    If you use Preserve, you can only resize the last dimension, not the first. Try this:
    [vba]
    Sub listboxlist()


    i = 0
    j = 0
    Z = 0


    Dim ar_temp()


    For i = 0 To SortReg.Controls("Listbox2").ListCount - 1
    If SortReg.Controls("Listbox2").Selected(i) = True Then
    Z = Z + 1
    Redim Preserve ar_temp(0 To 7, 0 To Z - 1)
    For j = 0 To 7
    ar_temp(j, Z - 1) = SortReg.Controls("Listbox2").List(i, j)
    Next
    End If
    Next


    SortReg.Controls("Listbox8").Column = ar_temp


    End Sub[/vba]
    Last edited by Aflatoon; 02-12-2014 at 10:08 AM.
    Be as you wish to seem

  3. #3
    VBAX Regular
    Joined
    Feb 2014
    Posts
    9
    Location
    Quote Originally Posted by Aflatoon View Post
    If you use Preserve, you can only resize the last dimension, not the first. Try this:
    [vba]
    Sub listboxlist()


    i = 0
    j = 0
    Z = 0


    Dim ar_temp()


    For i = 0 To SortReg.Controls("Listbox2").ListCount - 1
    If SortReg.Controls("Listbox2").Selected(i) = True Then
    Z = Z + 1
    Redim Preserve ar_temp(0 To 7, 0 To Z - 1)
    For j = 0 To 7
    ar_temp(j, Z - 1) = SortReg.Controls("Listbox2").List(i, j)
    Next
    End If
    Next


    SortReg.Controls("Listbox8").Column = ar_temp


    End Sub[/vba]
    AHHHHH YES....I love you aflatoon. just spent 2 hours trying to figure that one out. I came to the realisation that you can only resize the last dimension, however was trying transpose methids without success. Your solution basically is a tranpose without the mess isnt it? Much more elegant. Thankyou so much

  4. #4
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    or

    Private Sub UserForm_Initialize()
        ListBox1.List = Cells(1).CurrentRegion.Value
    End Sub
    
    private sub Commandbutton1_Click()
        ListBox2.List = ListBox1.List
    
        For j = ListBox1.ListCount - 1 To 0 Step -1
            If Not ListBox1.Selected(j) Then ListBox2.RemoveItem j
        Next
    End Sub

Tags for this Thread

Posting Permissions

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