combining two listbox codes as one

kangaruz

Board Regular
Joined
Apr 6, 2007
Messages
56
Hi Guys,
I have a Userform with a listbox that when an item is double clicked it will do a search of a sheet and return the value into another listbox.
I also have another code for the same listbox that will do a search of a different sheet and return the value to another list box on the same userform.
My question is, is it possible to join these two codes together so when the listbox is double clicked it will return the values into their respective listboxes?

Thanx, Mark

Code 1:
Private Sub ListBox3_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Dim r As Range, ff As String, a(), n As Long, i As Integer
If Len(Me.ListBox3.Text) = 0 Then Exit Sub
With Sheets("Orders")
Set r = .Columns("f").Find(Me.ListBox3.Text, , , xlWhole)
If r Is Nothing Then
Exit Sub
End If
ff = r.Address
Do
n = n + 1
ReDim Preserve a(1 To 6, 1 To n)
For i = 1 To 6
a(i, n) = r.Offset(, i - 6).Value
Next
Set r = .Columns("f").FindNext(r)
Loop Until ff = r.Address
End With
With Me.ListBox2
.ColumnCount = 3
.ColumnWidths = "49.95 pt;55 pt;55 pt"
.Column = a
End With
Frame13.Visible = False
Frame14.Visible = True
End Sub

Code 2:
Dim r As Range, ff As String, a(), n As Long, i As Integer
If Len(Me.ListBox3.Text) = 0 Then Exit Sub
With Sheets("JobChecklist")
Set r = .Columns("b").Find(Me.ListBox3.Text, , , xlWhole)
ff = r.Address
Do
n = n + 1
ReDim Preserve a(1 To 3, 1 To n)
For i = 1 To 3
a(i, n) = r.Offset(0, -1).Value
Next
Set r = .Columns("b").FindNext(r)
Loop Until ff = r.Address
End With
With Me.ListBox4
.ColumnCount = 1
.Column = a
End With
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Code:
Private Sub ListBox3_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Dim r As Range, ff As String, a(), n As Long, i As Integer
If Len(Me.ListBox3.Text) = 0 Then Exit Sub
With Sheets("Orders")
    Set r = .Columns("f").Find(Me.ListBox3.Text, , , xlWhole)
    If r Is Nothing Then
        Exit Sub
    End If
    ff = r.Address
    Do
        n = n + 1
        ReDim Preserve a(1 To 6, 1 To n)
        For i = 1 To 6
            a(i, n) = r.Offset(, i - 6).Value
        Next
        Set r = .Columns("f").FindNext(r)
    Loop Until ff = r.Address
End With
With Me.ListBox2
    .ColumnCount = 3
    .ColumnWidths = "49.95 pt;55 pt;55 pt"
    .Column = a
End With
With Sheets("JobChecklist")
    Set r = .Columns("b").Find(Me.ListBox3.Text, , , xlWhole)
    If r Is Nothing Then Exit Sub
    ff = r.Address : n = 0
    Do
        n = n + 1
        ReDim Preserve a(1 To 3, 1 To n)
        For i = 1 To 3
            a(i, n) = r.Offset(0, -1).Value
        Next
        Set r = .Columns("b").FindNext(r)
    Loop Until ff = r.Address
End With
With Me.ListBox4
    .ColumnCount = 1
    .Column = a
End With
Frame13.Visible = False
Frame14.Visible = True
End Sub
 
Upvote 0
Correction:
Rich (BB code):
With Sheets("JobChecklist")
    Set r = .Columns("b").Find(Me.ListBox3.Text, , , xlWhole)
    If r Is Nothing Then Exit Sub
    ff = r.Address : n = 0
    Do
        n = n + 1
        ReDim Preserve a(1 To 3, 1 To n)
        For i = 1 To 3
            a(i, n) = r.Offset(0, -1).Value
        Next
        Set r = .Columns("b").FindNext(r)
    Loop Until ff = r.Address
End With
With Me.ListBox4
    .ColumnCount = 1
    .Column = a
End With
Seems the following is OK
Rich (BB code):
With Sheets("JobChecklist")
    Set r = .Columns("b").Find(Me.ListBox3.Text, , , xlWhole)
    If r Is Nothing Then Exit Sub
    ff = r.Address : n = 0
    Do
        n = n + 1
        ReDim Preserve a(1 To n)
        a(n) = r.Offset(0, -1).Value
        Set r = .Columns("b").FindNext(r)
    Loop Until ff = r.Address
End With
Me.ListBox4.List = a
 
Upvote 0

Forum statistics

Threads
1,215,241
Messages
6,123,824
Members
449,127
Latest member
Cyko

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