PDA

View Full Version : Help transfer from ListBox to worksheet non ajacent columns



librkvn
07-17-2013, 05:30 AM
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

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

p45cal
07-17-2013, 05:48 AM
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:rStartCell.Cells(iRow, iColCount + 1).Value = ListBox1.List(iListCount, iColCount)
to something along the lines of:rStartCell.Cells(iRow, 2 * iColCount + 1).Value = ListBox1.List(iListCount, iColCount)
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: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)

snb
07-17-2013, 06:45 AM
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

librkvn
07-18-2013, 06:30 AM
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.

p45cal
07-18-2013, 07:14 AM
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
(like third snippet in msg#2)

librkvn
07-18-2013, 09:20 AM
Thats did it thank a lot p45cal



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
(like third snippet in msg#2)