Consulting

Results 1 to 6 of 6

Thread: Help transfer from ListBox to worksheet non ajacent columns

  1. #1
    VBAX Regular
    Joined
    Jul 2011
    Posts
    13
    Location

    Help transfer from ListBox to worksheet non ajacent columns

    I have a listbox that contains a multi column list, which is setup to transfer selected items to worksheet. It work fine by the way.The current procedure transfers item to worksheet with adjacent columns. I'm looking for a way to change code to transfer to non-adjacent columns. see my code

    [VBA]Private Sub cmdOK_Click() ' Multi-Column ListBox Transfer
    Dim iListCount As Integer, iColCount As Integer
    Dim iRow As Integer
    Dim rStartCell As Range

    Set rStartCell = Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
    For iListCount = 0 To ListBox1.ListCount - 1
    If ListBox1.Selected(iListCount) = True Then 'User has selected
    ListBox1.Selected(iListCount) = False
    iRow = iRow + 1
    For iColCount = 0 To Range("PARTS").Columns.Count - 1
    rStartCell.Cells(iRow, iColCount + 1).Value = _
    ListBox1.List(iListCount, iColCount)
    Next iColCount
    End If
    Next iListCount

    Set rStartCell = Nothing

    Unload Me
    Range("D6").Select
    End Sub
    [/VBA]

  2. #2
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,875
    Quote Originally Posted by librkvn
    to transfer to non-adjacent columns
    Are these columns alternating columns, in that there's just one column between each? If so
    a change from:[VBA]rStartCell.Cells(iRow, iColCount + 1).Value = ListBox1.List(iListCount, iColCount)
    [/VBA]to something along the lines of:[VBA]rStartCell.Cells(iRow, 2 * iColCount + 1).Value = ListBox1.List(iListCount, iColCount)
    [/VBA]might be all that's needed, however, if not then more info is needed; is there a pattern of distribution of the non-adjacent columns? If not then it might be a case of copying using multiple lines:[VBA]rStartCell.Cells(iRow, "H").Value = ListBox1.List(iListCount, 2)
    rStartCell.Cells(iRow, "K").Value = ListBox1.List(iListCount, 3)
    rStartCell.Cells(iRow, "Z").Value = ListBox1.List(iListCount, 4)
    [/VBA]
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  3. #3
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    [VBA]
    Private Sub cmdOK_Click() ' Multi-Column ListBox Transfer
    For j=0 to to ListBox1.ListCount - 1
    If ListBox1.Selected(j) Then sheets(1).Cells(Rows.Count, 1).End(xlUp).Offset(1).resize(,ubound(Listbox1.List,2)+1)=application.index (Listbox1.List,j,0)
    Next
    End Sub
    [/VBA]

  4. #4
    VBAX Regular
    Joined
    Jul 2011
    Posts
    13
    Location

    Help transfer from ListBox to worksheet non ajacent columns

    I have 3 columns of data in the listbox, "partno", "description", "unitcost" this from my source data, which via the listbox transfer i'm writing to destination sheet that performs calculation with the data. I want to shift "unit price" over one column for "qty" column which is on the destination sheet. Check out the workbook.
    Attached Files Attached Files

  5. #5
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,875
    [VBA]
    Private Sub cmdOK_Click() ' Multi-Column ListBox Transfer
    Dim iListCount As Integer, iColCount As Integer
    Dim iRow As Integer
    Dim rStartCell As Range

    Set rStartCell = Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)

    For iListCount = 0 To ListBox1.ListCount - 1
    If ListBox1.Selected(iListCount) = True Then 'User has selected
    ListBox1.Selected(iListCount) = False
    iRow = iRow + 1
    rStartCell.Cells(iRow, "A").Value = ListBox1.List(iListCount, 0)
    rStartCell.Cells(iRow, "B").Value = ListBox1.List(iListCount, 1)
    rStartCell.Cells(iRow, "D").Value = ListBox1.List(iListCount, 2)
    End If
    Next iListCount

    Set rStartCell = Nothing
    Unload Me
    Range("C6").Select
    End Sub
    [/VBA](like third snippet in msg#2)
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  6. #6
    VBAX Regular
    Joined
    Jul 2011
    Posts
    13
    Location

    SOLVED: Help transfer from ListBox to worksheet non ajacent columns

    Thats did it thank a lot p45cal

    Quote Originally Posted by p45cal
    [vba]
    Private Sub cmdOK_Click() ' Multi-Column ListBox Transfer
    Dim iListCount As Integer, iColCount As Integer
    Dim iRow As Integer
    Dim rStartCell As Range

    Set rStartCell = Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)

    For iListCount = 0 To ListBox1.ListCount - 1
    If ListBox1.Selected(iListCount) = True Then 'User has selected
    ListBox1.Selected(iListCount) = False
    iRow = iRow + 1
    rStartCell.Cells(iRow, "A").Value = ListBox1.List(iListCount, 0)
    rStartCell.Cells(iRow, "B").Value = ListBox1.List(iListCount, 1)
    rStartCell.Cells(iRow, "D").Value = ListBox1.List(iListCount, 2)
    End If
    Next iListCount

    Set rStartCell = Nothing
    Unload Me
    Range("C6").Select
    End Sub
    [/vba](like third snippet in msg#2)

Posting Permissions

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