Fill selected cells based on listbox selection

mcfly

Board Regular
Joined
May 15, 2002
Messages
162
Ok I have been pulling sample code from all over the internet, but I cant figure this one part out.

I have a UserForm with a textbox that when entered text it finds the data on listbox1(pulling from sheet2). When I click on the selection on listbox1 it sorts and shows ONLY the matching criteria I selected in listbox1 onto listbox 2. I then select a command Button to fill the data onto a sheet in certain cells but if listbox2 doesnt have enough rows it will kick back an error.

Everything works exactly the way I need it to except for this final part.
Here is the code I am currently using to move the data to the spreadsheet.

Code:
Private Sub CommandButton1_Click()

Dim i   As Long, X
With Me.ListBox2

   For i = 0 To ListBox2.ListCount - 1
         If ListBox2.Selected(i) = True Then
            Worksheets("Sheet1").Cells(2, 3) = "S" & ListBox2.List(i, 0) & " - " & ListBox2.List(i, 1)
            Worksheets("Sheet1").Cells(1, 9) = "S" & ListBox2.List(i, 0)
            Worksheets("Sheet1").Cells(4, 8) = ListBox2.List(i, 3)
            Worksheets("Sheet1").Cells(5, 8) = ListBox2.List(i, 4)
            Worksheets("Sheet1").Cells(6, 8) = ListBox2.List(i, 5)
            Worksheets("Sheet1").Cells(7, 8) = ListBox2.List(i, 6)
            Worksheets("Sheet1").Cells(8, 8) = ListBox2.List(i, 7)
            Worksheets("Sheet1").Cells(8, 10) = ListBox2.List(i, 8)
            Worksheets("Sheet1").Cells(8, 11) = ListBox2.List(i, 9)
            Worksheets("Sheet1").Cells(12, 7) = ListBox2.List(i, 10)
            Worksheets("Sheet1").Cells(56, 2) = ListBox2.List(i, 2)
            Worksheets("Sheet1").Cells(56, 7) = ListBox2.List(i + 1, 2)
            Worksheets("Sheet1").Cells(66, 2) = ListBox2.List(i + 2, 2)
            Worksheets("Sheet1").Cells(66, 7) = ListBox2.List(i + 3, 2)
            Worksheets("Sheet1").Cells(76, 2) = ListBox2.List(i + 4, 2)
            Worksheets("Sheet1").Cells(76, 7) = ListBox2.List(i + 5, 2)
            Worksheets("Sheet1").Cells(86, 2) = ListBox2.List(i + 6, 2)
            Worksheets("Sheet1").Cells(86, 7) = ListBox2.List(i + 7, 2)
            Worksheets("Sheet1").Cells(96, 2) = ListBox2.List(i + 8, 2)
            Worksheets("Sheet1").Cells(96, 7) = ListBox2.List(i + 9, 2)
  ListBox2.Selected(i) = False
            Exit Sub
            
        End If
    Next
End With

End Sub

Here is where the problem is happening. I have to place the info into these exact cells which is 10 cells but I may only have 4 rows in listbox2.
Code:
            Worksheets("Sheet1").Cells(56, 2) = ListBox2.List(i, 2)
            Worksheets("Sheet1").Cells(56, 7) = ListBox2.List(i + 1, 2)
            Worksheets("Sheet1").Cells(66, 2) = ListBox2.List(i + 2, 2)
            Worksheets("Sheet1").Cells(66, 7) = ListBox2.List(i + 3, 2)
            Worksheets("Sheet1").Cells(76, 2) = ListBox2.List(i + 4, 2)
            Worksheets("Sheet1").Cells(76, 7) = ListBox2.List(i + 5, 2)
            Worksheets("Sheet1").Cells(86, 2) = ListBox2.List(i + 6, 2)
            Worksheets("Sheet1").Cells(86, 7) = ListBox2.List(i + 7, 2)
            Worksheets("Sheet1").Cells(96, 2) = ListBox2.List(i + 8, 2)
            Worksheets("Sheet1").Cells(96, 7) = ListBox2.List(i + 9, 2)

Thanks
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Well apparently all I needed was to add this one line

On Error Resume Next

doh!!!
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,719
Members
452,939
Latest member
WCrawford

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top