keezymeezy
New Member
- Joined
- Jun 17, 2014
- Messages
- 13
I have a userform with three textboxes that collect a search term: search1, search 2, search3. The search value runs through a function generating a list which populates listboxes: listbox1, listbox2, listbox3. If an item in the listbox is doubleclicked then it runs an Instr function selecting only the text I need and copies it to a textbox: result1, result2, result3.
I have the following code which is just repeating itself with the different but related names
The above code works. What I'm trying to do is condense this code so it isn't so repetitive. I was thinking of something like this (which doesn't seem to work.
It fails with "Compile Error. Method or data member not found" and highlights the first occurrence of 'lb' in Private Sub transferText(lb, sch).
Can anyone help me?
I have the following code which is just repeating itself with the different but related names
Code:
Private Sub ListBox1_DblClick(ByVal cancel As MSForms.ReturnBoolean)
With searchForm.ListBox1
For c = 0 To .ListCount - 1
If ListBox1.Selected(c) = True Then
If InStr(1, ListBox1.List(c), ".") <> 0 Then
searchForm.search1.Value = Left(ListBox1.List(c), InStr(1, ListBox1.List(c), ".") + 1)
'
Else
searchForm.search1.Value = Left(ListBox1.List(c), InStr(1, ListBox1.List(c), " ") - 1)
End If
lenItem = Len(ListBox1.List(c)) 'length of the item
pos1space = InStr(1, ListBox1.List(c), " ") 'position of the first space
Exit For
End If
Next
End With
End Sub
Private Sub ListBox2_DblClick(ByVal cancel As MSForms.ReturnBoolean)
With searchForm.ListBox2
For c = 0 To .ListCount - 1
If ListBox2.Selected(c) = True Then
If InStr(1, ListBox2.List(c), ".") <> 0 Then
searchForm.search2.Value = Left(ListBox2.List(c), InStr(1, ListBox2.List(c), ".") + 1)
'
Else
searchForm.search2.Value = Left(ListBox2.List(c), InStr(1, ListBox2.List(c), " ") - 1)
End If
lenItem = Len(ListBox2.List(c)) 'length of the item
pos1space = InStr(1, ListBox2.List(c), " ") 'position of the first space
Exit For
End If
Next
End With
End Sub
Private Sub ListBox3_DblClick(ByVal cancel As MSForms.ReturnBoolean)
With searchForm.ListBox3
For c = 0 To .ListCount - 1
If ListBox3.Selected(c) = True Then
If InStr(1, ListBox3.List(c), ".") <> 0 Then
searchForm.search3.Value = Left(ListBox3.List(c), InStr(1, ListBox3.List(c), ".") + 1)
'
Else
searchForm.search3.Value = Left(ListBox3.List(c), InStr(1, ListBox3.List(c), " ") - 1)
End If
lenItem = Len(ListBox3.List(c)) 'length of the item
pos1space = InStr(1, ListBox3.List(c), " ") 'position of the first space
Exit For
End If
Next
End With
End Sub
The above code works. What I'm trying to do is condense this code so it isn't so repetitive. I was thinking of something like this (which doesn't seem to work.
Code:
Dim lb as String
Dim sch as String
Private Sub ListBox1_DblClick(ByVal cancel As MSForms.ReturnBoolean)
lb = ListBox1
sch = Search1
transferText (lb, sch)
End Sub
Private Sub ListBox2_DblClick(ByVal cancel As MSForms.ReturnBoolean)
lb = ListBox2
sch = Search2
transferText (lb, sch)
End Sub
Private Sub ListBox3_DblClick(ByVal cancel As MSForms.ReturnBoolean)
lb = ListBox3
sch = Search3
transferText (lb, sch)
End Sub
Private Sub transferText(lb, sch)
With searchForm.lb
For c = 0 To .ListCount - 1
If lb.Selected(c) = True Then
If InStr(1, lb.List(c), ".") <> 0 Then
searchForm.sch.Value = Left(lb.List(c), InStr(1, lb.List(c), ".") + 1)
'
Else
searchForm.sch.Value = Left(lb.List(c), InStr(1, lb.List(c), " ") - 1)
End If
lenItem = Len(lb.List(c)) 'length of the item
pos1space = InStr(1, lb.List(c), " ") 'position of the first space
Exit For
End If
Next
End With
End Sub
It fails with "Compile Error. Method or data member not found" and highlights the first occurrence of 'lb' in Private Sub transferText(lb, sch).
Can anyone help me?