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.
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.
Thanks
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