PDA

View Full Version : [SOLVED:] Loop through listbox to insert to sheet



av8tordude
05-07-2020, 09:35 PM
Can someone assist with a VBA code that will loop through a Userform Listbox to insert Columns 1 (Quantity), 2 (Price), 3 (Broker Fee) into the spreadsheet column T - AH of the Active Row?
The code only puts the last row into the first 3 columns (T,U,V). I'm trying to put each row (Lot #) into the corresponding column. Here's my attempt at it


Private Sub cbSubmit_Click()Dim LRow As Long, lCol2 As Long, i As Long
Dim dQty As Double, dPrice As Double, dFee As Double


LRow = ActiveCell.Row
lCol2 = 20




With lbxLots
dQty = CDbl(.List(.ListCount - 1, 1))
dPrice = CDbl(.List(.ListCount - 1, 2))
dFee = CDbl(.List(.ListCount - 1, 3))
End With


With lbxLots
For i = 0 To .ListCount - 1
Cells(LRow, lCol2).Resize(, 3) = Array(dQty, dPrice, dFee)
Next i
End With


End Sub

Graciously appreciate your help. Thx

snb
05-08-2020, 12:54 AM
Sub M_snb()
if lbxLots.listindex>-1 then activecell.resize(,4)=application.index(lbxLots.list,lbxLots.listindex+1,0)
End Sub

or


Sub M_snb()
With lbxLots
if .listindex>-1 then activecell.resize(,ubound(.list,2)+1)=application.index(.list,.listindex+1, 0)
end with
End Sub

av8tordude
05-08-2020, 05:59 AM
I tried both and nothing happens. Can you suggest an alternative?

snb
05-08-2020, 06:03 AM
Posting your file, because the information you provided isn't correct.
The oneliner should be part of the Userform's macromodule.

av8tordude
05-08-2020, 08:00 AM
See attached

The code should go in the Submit button

snb
05-08-2020, 08:40 AM
The listbox doesn't contain any data.
No data can't be written into anything at all.

av8tordude
05-08-2020, 08:59 AM
1. fill the textboxes, click enter to fill the listboxes
2. Repeat 2 more times so there are more items in the list box.

Once finish, i would like to click the submit button to accomplish my original request.